Codeforces295A Greg and Array 数据结构+两次区间更新+点查询
来源:互联网 发布:网络棋牌游戏广告语 编辑:程序博客网 时间:2024/06/07 02:25
Codeforces295A Greg and Array
Time Limit:1500MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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
3 3 31 2 31 2 11 3 22 3 41 21 32 3
9 18 17
1 1 111 1 11 1
2
4 3 61 2 3 41 2 12 3 23 4 41 21 32 31 21 32 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|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;}
- Codeforces295A Greg and Array 数据结构+两次区间更新+点查询
- CF A. Greg and Array (区间离线处理)
- cf295A. Greg and Array
- CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用
- Codeforces 295A Greg and Array 线段树区间累加,单点询问
- codeforces 295A - Greg and Array
- CodeForces 295A Greg and Array
- Codeforces 295A Greg and Array
- CodeForces 295A Greg and Array
- 【线段树】295A Greg and Array
- Codeforces 296C Greg and Array【思维】
- CodeForces 438D The Child and Sequence 数据结构+点更新(赋值)+区间求和
- HDU3966-树链剖分(区间更新,点查询)
- Codeforces Round #179 (Div. 2) C. Greg and Array
- Codeforces Round #179 (Div. 2) C Greg and Array
- Codeforces Round#179(Div 2)C Greg and Array
- Codeforces Round #179 (Div. 1)-A. Greg and Array
- Codeforces Round #179 (Div. 1) A. Greg and Array
- 【Redis】基本类型
- c++命名规范
- HashMap工作原理分析
- 码神之体会
- 《Windows核心编程》之“内存映射文件”
- Codeforces295A Greg and Array 数据结构+两次区间更新+点查询
- 【Linux全面学习】14.虚拟机VMware中Linux联网问题
- CGAffineTransform回顾
- Android-打开系统相机并拍照两种显示方式。
- Deep Learning(深度学习)学习笔记整理系列之(一)
- PAT 1008. 数组元素循环右移问题
- N皇后问题
- Android自定义WebView实现Youtube网络视频播放控件
- IONIC----09.route1