poj-1656-Counting Black-(树状数组)
2017-08-25 21:12
309 查看
There is a board with 100 * 100 grids as shown below. The left-top gird is denoted as (1, 1) and the right-bottom grid is (100, 100).
We may apply three commands to the board:
In the beginning, all the grids on the board are white. We apply a series of commands to the board. Your task is to write a program to give the numbers of black grids within a required region when a TEST command is applied.
Input
The first line of the input is an integer t (1 <=
4000
t <= 100), representing the number of commands. In each of the following lines, there is a command. Assume all the commands are legal which means that they won't try to paint/test the grids outside the board.
Output
For each TEST command, print a line with the number of black grids in the required region.
Sample Input
5
BLACK 1 1 2
BLACK 2 2 2
TEST 1 1 3
WHITE 2 1 1
TEST 1 1 3
Sample Output
7
6
题目给出n组数据,每组形式为x1,y1,l,每组数据形成一个矩阵(x1,y1)为左上角,(x1+l-1,y1+l-1)为右上角,给出3种操作,1.把矩阵内方块全部变白,2.全部变黑,3.统计黑块个数,
题目可以用树状数组做,但是看到这么小的数据,还是暴力解更合适,运行时间也说明了一切
代码(树状数组)(625 MS ,4708 KB):
代码(0 MS ,748 KB):
We may apply three commands to the board:
1. WHITE x, y, L // Paint a white square on the board, // the square is defined by left-top grid (x, y) // and right-bottom grid (x+L-1, y+L-1) 2. BLACK x, y, L // Paint a black square on the board, // the square is defined by left-top grid (x, y) // and right-bottom grid (x+L-1, y+L-1) 3. TEST x, y, L // Ask for the number of black grids // in the square (x, y)- (x+L-1, y+L-1)
In the beginning, all the grids on the board are white. We apply a series of commands to the board. Your task is to write a program to give the numbers of black grids within a required region when a TEST command is applied.
Input
The first line of the input is an integer t (1 <=
4000
t <= 100), representing the number of commands. In each of the following lines, there is a command. Assume all the commands are legal which means that they won't try to paint/test the grids outside the board.
Output
For each TEST command, print a line with the number of black grids in the required region.
Sample Input
5
BLACK 1 1 2
BLACK 2 2 2
TEST 1 1 3
WHITE 2 1 1
TEST 1 1 3
Sample Output
7
6
题目给出n组数据,每组形式为x1,y1,l,每组数据形成一个矩阵(x1,y1)为左上角,(x1+l-1,y1+l-1)为右上角,给出3种操作,1.把矩阵内方块全部变白,2.全部变黑,3.统计黑块个数,
题目可以用树状数组做,但是看到这么小的数据,还是暴力解更合适,运行时间也说明了一切
代码(树状数组)(625 MS ,4708 KB):
#include<iostream> #include<string> #include<cstdio> #include<algorithm> #include<cmath> #include<iomanip> #include<queue> #include<cstring> #include<map> #include<vector> using namespace std; typedef long long ll; #define M 105 int tree[M][M],vis[M][M]; inline int lowbit(int i) { return i&(-i); } void add(int x,int y,int v) { int i,j; for(i=x;i<M;i+=lowbit(i)) for(j=y;j<M;j+=lowbit(j)) { tree[i][j]+=v; } } int sum(int x,int y) { int i,j,res=0; for(i=x;i>0;i-=lowbit(i)) for(j=y;j>0;j-=lowbit(j)) { res+=tree[i][j]; } return res; } int main() { int T,x1,y1,x2,y2,l,i,j; char str[10]; scanf("%d",&T); while(T--) { getchar(); scanf("%s",str); scanf("%d%d%d",&x1,&y1,&l); x2=x1+l-1; y2=y1+l-1; if(str[0]=='B') { for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) { if(vis[i][j]==0) {add(i,j,1);vis[i][j]=1;} } } else if(str[0]=='W') { for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) { if(vis[i][j]==1) {add(i,j,-1); vis[i][j]=0;} } } else { printf("%d\n",sum(x2,y2)+sum(x1-1,y1-1)-sum(x2,y1-1)-sum(x1-1,y2)); } } return 0; }
代码(0 MS ,748 KB):
#include<iostream> #include<string> #include<cstdio> #include<algorithm> #include<cmath> #include<iomanip> #include<queue> #include<cstring> #include<map> #include<vector> using namespace std; typedef long long ll; #define M 105 int tree[M][M]; int main() { int n,i,j,x1,y1,l,x2,y2; char str[10]; scanf("%d",&n); while(n--) { getchar(); scanf("%s",str); scanf("%d%d%d",&x1,&y1,&l); x2=x1+l-1; y2=y1+l-1; if(str[0]=='B') { for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) { tree[i][j]=1; } } else if(str[0]=='W') { for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) { tree[i][j]=0; } } else { int num=0; for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) { if(tree[i][j]==1) num++; } printf("%d\n",num); } } return 0; }
相关文章推荐
- poj 1656(二维树状数组解法)
- POJ 1656 二维树状数组简单应用
- poj 1656(二维树状数组解法)
- POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】
- POJ 2352 Stars(树状数组)
- HDU 1541 & POJ 2352 Stars (树状数组)
- POJ 3468 A Simple Problem with Integers(树状数组区间修改+区间查询)
- POJ 题目2182 Lost Cows(树状数组+二分)
- [树状数组] poj 1990 MooFest
- POJ 3067 Japan (树状数组 + 逆序数)
- POJ 3067 Japan(树状数组求逆序对)
- poj 2352 Stars (树状数组)
- POJ 1990 MooFest(树状数组)
- poj 2352 树状数组
- poj-2352 && HDU-1541 --Stars(树状数组)
- POJ 2481 Cows (树状数组)
- POJ 3468 的树状数组解法
- POJ 1990 MooFest(想法题&树状数组)
- poj 2155 Matrix 二维树状数组
- 【POJ】2352 - Stars(树状数组)