CodeForces 295A Greg and Array

来源:互联网 发布:怎样才算掌握c语言 编辑:程序博客网 时间:2024/05/13 07:19

题目链接:http://codeforces.com/problemset/problem/295/A


Greg and Array

time limit per test:1.5 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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 integersa1, 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.

Examples

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

思路:先将m个操作中每个操作的执行次数算出来,然后一次性更新区间。在计算每个操作的执行次数时可以用线段树或者树状数组来实现,我用的是树状数组,因为写起来简便些。然后剩下的工作就是线段树的区间更新,区间求和问题了,直接套用模板就可以了。注意:题目数据太大,必要的地方用long long处理一下,以免中间结果溢出了。


附上AC代码:

#include <bits/stdc++.h>#define lrt rt<<1#define rrt rt<<1|1#define lson l, m, lrt#define rson m+1, r, rrtusing namespace std;typedef long long ll;const int maxn = 100005;ll sumv[maxn<<2], addv[maxn<<2];int l[maxn], r[maxn], d[maxn];int cnt[maxn], tree[maxn];int n, q, k;int lowbit(int x){return x&(-x);}void add(int p, int x){while (p <= q){tree[p] += x;p += lowbit(p);}}int sum(int p){int ans = 0;while (p > 0){ans += tree[p];p -= lowbit(p);}return ans;}void push_up(int rt){sumv[rt] = sumv[lrt]+sumv[rrt];}void build(int l, int r, int rt){if (l == r){int num;cin >> num;sumv[rt] = num;return ;}int m = (l+r)>>1;build(lson);build(rson);push_up(rt);}void push_down(int l, int r, int rt){if (addv[rt]){addv[lrt] += addv[rt];addv[rrt] += addv[rt];int m = (l+r)>>1;sumv[lrt] += (m-l+1)*addv[rt];sumv[rrt] += (r-m)*addv[rt];addv[rt] = 0;}}void update(int cl, int cr, ll val, int l, int r, int rt){if (cl<=l && r<=cr){addv[rt] += val;sumv[rt] += val*(r-l+1);return ;}push_down(l, r, rt);int m = (l+r)>>1;if (cl <= m)update(cl, cr, val, lson);if (cr > m)update(cl, cr, val, rson);push_up(rt);}ll query(int ql, int qr, int l, int r, int rt){if (ql<=l && r<=qr)return sumv[rt];push_down(l, r, rt);int m = (l+r)>>1;ll sumr = 0;if (ql <= m)sumr += query(ql, qr, lson);if (qr > m)sumr += query(ql, qr, rson);return sumr;}int main(){ios::sync_with_stdio(false);cin.tie(0);cin >> n >> q >> k;build(1, n, 1);for (int i=1; i<=q; ++i)cin >> l[i] >> r[i] >> d[i];int x, y;while (k--){cin >> x >> y;add(x, 1);add(y+1, -1);}for (int i=1; i<=q; ++i)cnt[i] = sum(i);for (int i=1; i<=q; ++i)if (cnt[i] && d[i])update(l[i], r[i], 1ll*d[i]*cnt[i], 1, n, 1);for (int i=1; i<=n; ++i){cout << query(i, i, 1, n, 1);if (i != n)cout << " ";elsecout << endl;}return 0;}


1 0