标签:for eof mon write 树状数组 gb2312 sam init class
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 17766 | Accepted: 6674 |
Description
Input
Output
Sample Input
1 2 10 C 2 1 2 2 Q 2 2 C 2 1 2 1 Q 1 1 C 1 1 2 1 C 1 2 1 2 C 1 1 2 2 Q 1 1 C 1 1 2 1 Q 2 1
Sample Output
1 0 0 1
#include <stdio.h>
#include <string.h>
#define maxn 1002
int size, tree[maxn][maxn];
int lowBit(int x){ return x & (-x); }
//向下更新表示A[1]...A[i]每一个元素都要 += val,推广到二维同理
void update(int x, int y, int val)
{
int temp;
while(x > 0){
temp = y;
while(temp > 0){
tree[x][temp] += val;
temp -= lowBit(temp);
}
x -= lowBit(x);
}
}
int query(int x, int y)
{
int sum = 0, temp;
while(x <= size){
temp = y;
while(temp <= size){
sum += tree[x][temp];
temp += lowBit(temp);
}
x += lowBit(x);
}
return sum;
}
int main()
{
//freopen("stdin.txt", "r", stdin);
int cas, q, a, b, c, d;
char com[2];
scanf("%d", &cas);
while(cas--){
scanf("%d%d", &size, &q);
memset(tree, 0, sizeof(tree));
while(q--){
scanf("%s%d%d", com, &a, &b);
if(com[0] == 'C'){
scanf("%d%d", &c, &d);
update(c, b - 1, -1);
update(a - 1, d, -1);
update(a - 1, b - 1, 1);
update(c, d, 1);
}else printf("%d\n", query(a, b) & 1);
}
if(cas) printf("\n");
}
return 0;
}POJ2155 Matrix 【二维树状数组】+【段更新点查询】
标签:for eof mon write 树状数组 gb2312 sam init class
原文地址:http://www.cnblogs.com/mthoutai/p/7140542.html