Title Link: Http://codeforces.com/problemset/problem/295/A
My approach, two-time segment tree
#include <cstdio> #include <cstring>const int N = 100005;long Long sumv[n * 3];long long add[n * 3];long long A [n];struct Opera {int L, R; Long Long D;} Op[n];void pushdown (int o, int l, int r) {int m = (L + r) >> 1; Sumv[o << 1] + = add[o] * (m-l + 1); Add[o << 1] + = Add[o]; Sumv[o << 1 | 1] + = add[o] * (R-M); Add[o << 1 | 1] + + add[o]; Add[o] = 0;} int QL, Qr;long long val;void update (int o, int l, int r) {if (QL <= l && qr >= r) {Sumv[o] + = (r -L + 1) * val; Add[o] + = val; return; } if (Add[o]) pushdown (O, L, R); int m = (L + r) >> 1; if (M < qr) Update (o << 1 | 1, M + 1, R); if (M >= ql) Update (o << 1, L, M); Sumv[o] = Sumv[o << 1] + sumv[o << 1 | 1];} int q;long long query (int o, int l, int r) {if (L = = r) return Sumv[o]; if (Add[o]) pushdown (O, L, R); int m = (L + r) >> 1; if (M < Q) return query (O << 1 | 1, M + 1, R); else return query (o << 1, L, m);} int main () {//freopen ("a.in", "R", stdin); int n, m, K; int i; scanf ("%d%d%d", &n, &m, &k); for (i = 1; I <= n; ++i) scanf ("%lld", A + i); for (i = 1; I <= m; ++i) {scanf ("%d%d%lld", &OP[I].L, &OP[I].R, &OP[I].D); } val = 1; for (i = 1; I <= K; ++i) {scanf ("%d%d", &QL, &QR); Update (1, 1, m); } for (i = 1; I <= m; ++i) {q = i; OP[I].D *= Query (1, 1, m); } memset (SUMV, 0, sizeof SUMV); memset (add, 0, sizeof add); for (i = 1; I <= m; ++i) {QL = OP[I].L; qr = OP[I].R; val = op[i].d; Update (1, 1, N); } for (i = 1; I <= n; ++i) {q = i; printf ("%lld", A[i] + query (1, 1, n)); if (i! = N) printf (""); } return 0;}
Later saw the code of seniors, and wrote again .... :
#include <cstdio>const int N = 100005;long long L[n];long long R[n];long long D[n];long long A[n];long long Op[n];lo ng Long B[n];main () { //freopen ("In.txt", "R", stdin); Long long N, M, K; scanf ("%lld%lld%lld", &n, &m, &k); for (int i = 1; I <= n; ++i) scanf ("%lld", &a[i]); for (int i = 1; I <= m; ++i) scanf ("%lld%lld%lld", &l[i], &r[i], &d[i]); Long long x, y; for (int i = 1; I <= K; ++i) { scanf ("%lld%lld", &x, &y); ++OP[X]; --OP[Y+1]; } for (int i = 1; I <= m; ++i) { Op[i] + = op[i-1]; D[i] *= op[i]; } for (int i = 1; I <= m; ++i) { B[l[i]] + = d[i]; B[R[I]+1]-= D[i]; } for (int i = 1; I <= n; ++i) { B[i] + = b[i-1]; A[i] + = B[i]; } printf ("%lld", a[1]); for (int i = 2; I <= n; ++i) printf ("%lld", A[i]);}
Codeforces Round #179 (295A)-Greg and Array