Codeforces Round #179 (Div. 2) C. Greg and Array
来源:互联网 发布:小漠淘宝店服装店 编辑:程序博客网 时间:2024/05/24 01:48
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.
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.
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 thecin,cout streams of the%I64d specifier.
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开始写成了0,结果浪费了5分多钟的时间找bug,结果比赛就结束了,遗憾啊。
考察点:树状数组
思路:建立两个树状数组,分别统计操作实行的次数和每次操作数值的变化
#include <stdio.h>#include <string.h>#include <math.h>__int64 tree1[110000],tree2[110000];struct num{ int l,r,val;}a[110000];__int64 b[1000000],sum[110000];__int64 n,m;int main(){ void build(__int64 k,__int64 val); void build2(__int64 k,__int64 val); __int64 search(__int64 k); __int64 search2(__int64 k); __int64 i,j,s,t; __int64 x,y,k; while(scanf("%I64d %I64d %I64d",&n,&m,&k)!=EOF) { for(i=1;i<=n;i++) { scanf("%I64d",&b[i]); } for(i=1;i<=m;i++) { scanf("%d %d %d",&a[i].l,&a[i].r,&a[i].val); } memset(tree2,0,sizeof(tree2)); memset(tree1,0,sizeof(tree1)); for(i=1;i<=k;i++) { scanf("%I64d %I64d",&x,&y); build(y,1); build(x-1,-1); } memset(sum,0,sizeof(sum)); for(i=1;i<=m;i++) { t=search(i); sum[i]=sum[i]+t; } for(i=1;i<=m;i++) { build2(a[i].r,sum[i]*a[i].val); build2(a[i].l-1,-1*sum[i]*a[i].val); } for(i=1;i<=n;i++) { t=search2(i); b[i]+=t; if(i==1) { printf("%I64d",b[i]); }else { printf(" %I64d",b[i]); } } printf("\n"); } return 0;}int f(int k){ return (k&-k);}void build(__int64 k,__int64 val){ while(k>0) { tree2[k]+=val; k-=f(k); }}__int64 search(__int64 k){ __int64 s=0; while(k<=m) { s+=tree2[k]; k+=f(k); } return s;}void build2(__int64 k,__int64 val){ while(k>0) { tree1[k]+=val; k-=f(k); }}__int64 search2(__int64 k){ __int64 s=0; while(k<=n) { s+=tree1[k]; k+=f(k); } return s;}
- 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. 2)---C. Greg and Array
- Codeforces Round #179 (Div. 2) C Greg and Array 两个线段树
- Codeforces #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
- Codeforces Round #179 (Div. 2)---D. Greg and Graph(离线+floyd)
- Codeforces 296C Greg and Array【思维】
- CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用
- CodeForces 296C Greg and Array (线段树)
- Codeforces Round #373 (Div. 1) C.Sasha and Array
- Codeforces Round #156 (Div. 2)---A. Greg's Workout
- Codeforces Round #251 (Div. 2) C Devu and Partitioning of the Array
- Codeforces Round #251 (Div. 2) 439C Devu and Partitioning of the Array(脑洞)
- Codeforces Round #251(Div. 2) 439C. Devu and Partitioning of the Array 构造
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- linux中断那些事
- vbs删除前一天数据库备份文件
- vvvvvvvvvvvv
- php中的时间戳与javascript中的时间戳的比较
- 黑马程序员_函数的应用和重载
- Codeforces Round #179 (Div. 2) C. Greg and Array
- Export 3D model to JSON in Blender on Fedora
- 在线广告的exploration/exploitation trade-off(勘探和开采问题)
- 各大推荐引擎资料汇总
- 单机模式处理大数据,搜集一些好用的开源利器
- Android 编译脚本小解析。(include-path-for 解析)
- sencha 对齐文字
- 前端神器-网站图片抓取精灵V1.0正式发布
- codeforces Round #179 (Div. 2) 296A 296B 295A 295B 295C