Codeforces295A Greg and Array 数据结构+两次区间更新+点查询

来源:互联网 发布:网络棋牌游戏广告语 编辑:程序博客网 时间:2024/06/07 02:25

Codeforces295A Greg and Array

Time Limit:1500MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

SubmitStatusPracticeCodeForces 295A

Description

Greg has an array a = a1, a2, ..., an andm operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operationi to the array means to increase all array elements with numbersli, li + 1, ..., ri by valuedi.

Greg wrote down k queries on a piece of paper. Each query has the following form:xi,yi,(1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbersxi, 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 n, m, k (1 ≤ n, m, k ≤ 105). The second line containsn integers: a1, a2, ..., an(0 ≤ ai ≤ 105) — the initial array.

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

Next k lines contain the queries, the query numberi is written as two integers: xi, yi, (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 inC++. It is preferred to use the cin, cout streams of the %I64d specifier.

Sample Input

Input
3 3 31 2 31 2 11 3 22 3 41 21 32 3
Output
9 18 17
Input
1 1 111 1 11 1
Output
2
Input
4 3 61 2 3 41 2 12 3 23 4 41 21 32 31 21 32 3
Output
5 18 31 20解题思路:1,题意说给你n个数m个操作,k个询问,回答在k个询问执行完之后数组的值都是多少2,k个询问其实就是说明执行m中的哪些操作比如1 3说明执行1-3号操作3,这题首先要做的就是对于m中每个操作,每个操作执行了多少次4,然后对于m个操作,每个操作具体去执行得到最后的数组5,然后输出每一个数就是对每一个点的点查询,n*logn的复杂度6,这题卡long long 中间的计算有可能会溢出,所以要用long long 我当时实在没找到我哪里溢出了,直接把所有的数据类型变成long long才过
#include<bits/stdc++.h>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 200005;int n,m,k;#define LL long longstruct node{    LL x;    LL y;    long long ad ;};long long add[maxn<<2];long long sum[maxn<<2];void PushUp(LL rt) {sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void PushDown(LL rt,LL m) {if (add[rt]) {add[rt<<1] += add[rt];add[rt<<1|1] += add[rt];sum[rt<<1] += add[rt] * (m - (m >> 1));sum[rt<<1|1] += add[rt] * (m >> 1);add[rt] = 0;}}void update(LL L,LL R,LL c,LL l,LL r,LL rt) {if (L <= l && r <= R) {add[rt] += c;sum[rt] += (LL)c * (r - l + 1);return ;}PushDown(rt , r - l + 1);LL m = (l + r) >> 1;if (L <= m) update(L , R , c , lson);if (m < R) update(L , R , c , rson);PushUp(rt);}LL query(LL L,LL R,LL l,LL r,LL rt) {if (L <= l && r <= R) {return sum[rt];}PushDown(rt , r - l + 1);LL m = (l + r) >> 1;LL ret = 0;if (L <= m) ret += query(L , R , lson);if (m < R) ret += query(L , R , rson);return ret;}node op[maxn] ;LL arry[maxn] ;LL kk[maxn] ;int main(){    //freopen("in.txt","r",stdin);    while(~scanf("%d%d%d",&n,&m,&k)){        for(LL i=0;i<n;i++){            scanf("%d",&arry[i]);        }        memset(sum,0,sizeof(sum));        memset(add,0,sizeof(add));        for(int i=1;i<=m;i++){            scanf("%I64d%I64d%I64d",&op[i].x,&op[i].y,&op[i].ad);        }        for(LL i=0;i<k;i++){            int a,b;            scanf("%d%d",&a,&b) ;            update(a,b,1,1,m,1) ;        }        for(LL i=1;i<=m;i++){            kk[i] = query(i,i,1,m,1) ;            //op[i].ad=op[i].ad*kk ;        }        memset(sum,0,sizeof(sum));        memset(add,0,sizeof(add));        for(LL i=0;i<n;i++){            update(i+1,i+1,arry[i],1,n,1);        }        for(LL i=1;i<=m;i++){            update(op[i].x,op[i].y,op[i].ad*kk[i],1,n,1) ;        }        for(LL i=1;i<n;i++){            printf("%I64d ",query(i,i,1,n,1));        }printf("%I64d\n",query(n,n,1,n,1)) ;    }    return 0;}


0 0
原创粉丝点击