codeforces--707E. Garlands(二维树状数组)
2016-08-26 16:41
316 查看
cf 707E
题解
参考题解#include <bits/stdc++.h> using namespace std; const double eps = 1E-8; const int dx[4] = {1, 0, 0, -1}; const int dy[4] = {0, -1, 1, 0}; const int inf = 0x3f3f3f3f; const int N = 1E5 + 7; #define MS(a, x) memset(a, x, sizeof(a)) #define MP make_pair #define fst first #define sec second #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 typedef long long LL; const int maxn = 2000 + 3; // AC info: 1482 ms 72900 KB struct Cell{ int x, y; LL val; }; int n, m, k, q; vector<Cell> garland[maxn]; LL c[maxn][maxn]; bool on[maxn], last[maxn]; int lowbit(int i) { return i & (-i); } void add(int x, int y, int v){ for(int i = x; i <= n; i += lowbit(i)){ for(int j = y; j <= m; j += lowbit(j)) c[i][j] += v; } } LL sum(int x, int y){ LL s = 0; for(int i = x; i ; i -= lowbit(i)){ for(int j = y; j ; j -= lowbit(j)) s += c[i][j]; } return s; } int main(){ #ifdef LOCAL freopen("data.in", "r", stdin); #endif // LOCAL scanf("%d %d %d", &n, &m, &k); for(int i = 1; i <= k; ++i){ int lights; scanf("%d", &lights); for(int j = 0; j < lights; ++j){ int x, y, val; scanf("%d %d %d", &x, &y, &val); garland[i].push_back(Cell{x, y, val}); } // turn on on[i] = true; } scanf("%d", &q); char op[8]; int x1, y1, x2, y2; for(int cnt = 0; cnt < q; ++cnt){ scanf("%s", op); if(op[0] == 'S'){ int which; scanf("%d", &which); on[which] ^= 1; } else{ scanf("%d %d %d %d", &x1, &y1, &x2, &y2); // 每一次ASK,都要计算每条链的贡献 for(int i = 1; i <= k; ++i){ // 如果和上一次的状态不同,更新 if(last[i] != on[i]){ for(int j = 0; j < garland[i].size(); ++j){ // 第i条链的第j个灯 if(on[i]) add(garland[i][j].x, garland[i][j].y, garland[i][j].val); else add(garland[i][j].x, garland[i][j].y, -garland[i][j].val); } } // 记录上一次的状态 last[i] = on[i]; } LL ans = sum(x2, y2) - sum(x2, y1 - 1) - sum(x1 - 1, y2) + sum(x1 - 1, y1 - 1); printf("%I64d\n", ans); } } return 0; }
相关文章推荐
- Codeforces 707 E. Garlands (二维树状数组)
- Codeforces 707E Garlands(二维树状数组)
- 树状数组(Garlands,codeforces 707e)
- Codeforces Round #368 (Div. 2) E. Garlands(二维树状数组)
- cf#368-E. Garlands-(二维树状数组)
- codeforces 835C Star sky(二维树状数组)
- CodeForces 341D Iahub and Xors(二维树状数组)
- [CodeForces 677D] Vanya and Treasure (二维树状数组+静态前缀最小值)
- [Codeforces 341D] Iahub and Xors (二维树状数组)
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组
- 【codeforces 707E】Garlands
- Codeforces 869 E. The Untended Antiquity 二维树状数组
- poj 2155 二维树状数组
- poj 2029(二维树状数组)
- 二维树状数组
- hdu 5517 Triple(二维树状数组)
- POJ2155 Matrix(二维树状数组)
- 5.3.1 See you~ (二维树状数组)(UNSOLVED)
- POJ 2029 Get Many Persimmon Trees (二维树状数组)
- poj 2155(二维树状数组)