CodeForces 296C Greg and Array (线段树)

Greg and Array
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: liridi(1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.

Greg wrote down k queries on a piece of paper. Each query has the following form: xiyi(1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.

Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.

Input

The first line contains integers nmk (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array.

Next m lines contain operations, the operation number i is written as three integers: liridi(1 ≤ li ≤ ri ≤ n)(0 ≤ di ≤ 105).

Next k lines contain the queries, the query number i is written as two integers: xiyi(1 ≤ xi ≤ yi ≤ m).

The numbers in the lines are separated by single spaces.

Output

On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cincout streams of the %I64dspecifier.

Sample test(s)
input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
output
9 18 17
input
1 1 1
1
1 1 1
1 1
output
2
input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
output
5 18 31 20

题意:长度为n的序列;m种操作:a b c表示第a~b个数的值加上c;k次执行:a b表示执行第a~b种操作。最后输出执行完所有操作后的数列。

先用线段树计算出每种操作执行的次数,将操作中要加的数乘以次数更新。再用线段树求出每个位置的数需要加的最终数值。感觉两个都是简单线段树,就习惯性套模板,而且两个线段树又是先后分开执行,所以写了一个线段树就够了。

#include <cstdio>
#include <iostream>
#include <algorithm>
#define ls node << 1
#define rs node << 1 | 1
#define lson l,mid,ls
#define rson mid + 1,r,rs
#define maxn 100005

using namespace std;

long long a[maxn];
int n,m,k,x,y;
long long cnt[maxn];

struct Operation {
    int l;
    int r;
    long long d;
} o[maxn];

struct Query {
    int l;
    int r;
    long long c;
    long long num;
} q[maxn << 2];

void build(int l, int r, int node)
{
    q[node].l = l;
    q[node].r = r;
    q[node].num = 0;
    q[node].c = 0;
    if(l == r)
        return;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
}

void pushdown(int node,int len)
{
    if(q[node].c) {
        q[ls].c += q[node].c;
        q[rs].c += q[node].c;
        q[ls].num += q[node].c * (len - (len >> 1));
        q[rs].num += q[node].c * (len >> 1);
        q[node].c = 0;
    }
}

void update(int x,int y,long long c,int l,int r,int node)
{
    if(x <= l && y >= r) {
        q[node].c += c;
        q[node].num += c * (r - l + 1);
        return;
    }
    pushdown(node, r - l + 1);
    int mid = (l + r) >> 1;
    if(x <= mid)
        update(x,y,c,lson);
    if(y > mid)
        update(x,y,c,rson);
}

long long findnum(int x, int l, int r, int node)
{
    if(x == l && x == r)
        return q[node].num;
    pushdown(node,r - l + 1);
    int mid = (l + r) >> 1;
    if(x > mid)
        return findnum(x,rson);
    if(x <= mid)
        return findnum(x,lson);
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&n,&m,&k) != EOF) {
        for(long long i = 1; i <= n; i++)
            scanf("%lld",&a[i]);
        for(long long i = 1; i <= m; i++)
            scanf("%d%d%lld",&o[i].l,&o[i].r,&o[i].d);
        build(1,m,1);
        for(long long i = 1; i <= k; i++) {
            scanf("%d%d",&x,&y);
            update(x,y,1,1,m,1);
        }
        for(int i = 1; i <= m; i++) {
            cnt[i] = findnum(i,1,m,1);
            //cout << cnt[i] << endl;
            o[i].d *= cnt[i];
        }
        build(1,n,1);
        for(int i = 1; i <= m; i++)
            update(o[i].l,o[i].r,o[i].d,1,n,1);
        for(int i = 1; i < n; i++)
            printf("%lld ",a[i] + findnum(i,1,n,1));
        printf("%lld\n",a[n] + findnum(n,1,n,1));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值