A. Greg and Array
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/295/problem/A
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 %I64dspecifier.
Sample Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Sample Output
9 18 17
HINT
题意
给你n个数,然后有m个命令,每个命令是将[l,r]中的数都增加d,有k次真正要执行的操作(。。),是将第[l,r]的命令执行一遍
然后问你,这个n个数,最后悔变成什么模样。
题解:
这道题扫一遍就好了,由于只有最后一次询问,那么我们可以离线去做,而不用去写什么线段树
首先我们离线处理操作,然后得到每一个命令都会执行多少次,然后我们再离线去处理命令就好了
离线处理,就是在起始端和结束段打上标记,然后扫一遍就好了
代码:
#include<iostream> #include<stdio.h> using namespace std; #define maxn 100005 long long a[maxn]; long long flag[maxn]; long long t[maxn]; struct OP { int x,y; long long val; }; OP op[maxn]; int main() { int n,m,k;scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<=m;i++) { int x,y;long long z; scanf("%d%d%lld",&op[i].x,&op[i].y,&op[i].val); } for(int i=1;i<=k;i++) { int x,y;scanf("%d%d",&x,&y); t[x]++; t[y+1]--; } long long sum = 0; for(int i=1;i<=m;i++) { sum+=t[i]; op[i].val*=sum; } for(int i=1;i<=m;i++) { flag[op[i].x]+=op[i].val; flag[op[i].y+1]-=op[i].val; } sum = 0; for(int i=1;i<=n;i++) { sum+=flag[i]; printf("%lld ",a[i]+sum); } printf(" "); }