您的位置:首页 > 理论基础 > 数据结构算法

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息