Codeforces295A Greg and Array
Time Limit:1500MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (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: xi, yi, (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 n, m, k (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: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).
Next k lines contain the queries, the query number i 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 in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Sample Input
3 3 3 1 2 3 1 2 1 1 3 2 2 3 4 1 2 1 3 2 3
9 18 17
1 1 1 1 1 1 1 1 1
2
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
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|1 const int maxn = 200005; int n,m,k; #define LL long long struct 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; }