poj-1656-Counting Black-(树状数组)
来源:互联网 发布:年轻人理财 知乎 编辑:程序博客网 时间:2024/04/18 20:50
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.
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 <= 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
5BLACK 1 1 2BLACK 2 2 2TEST 1 1 3WHITE 2 1 1TEST 1 1 3
Sample Output
76
题目给出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 105int 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 105int 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;}
阅读全文
0 0
- poj-1656-Counting Black-(树状数组)
- POJ-1656 Counting Black (二维树状数组 入门题)
- Counting Black(树状数组)
- POJ 1656 Counting Black
- poj 1656 Counting Black
- POJ 1656 Counting Black
- poj 1656 Counting Black
- poj 1656 Counting Black
- poj 1656 Counting Black
- poj 1656 Counting Black
- poj 1656 Counting Black
- POJ 1656 Counting Black
- Poj 1656 Counting Black
- poj 1656 Counting Black 水题
- POJ-1656-Counting Black- 四分树
- POJ 1656 Counting Black(水~)
- POJ-1565-Counting Black
- POJ 1442 Black Box treap || 树状数组 || 优先队列
- 恋爱协议(七七条约)
- 分治、归并、快排
- CentOS系统的YUM在线升级机制
- 微信小程序 wx.uploadFile 的编码坑
- 灰度算法
- poj-1656-Counting Black-(树状数组)
- h5兼容ie9以下版本
- Time类
- CCF 学生排队 C语言版
- Tcp协议简读
- eclipse 遍历mysql数据加查寻功能
- 您,适合Linux吗?
- 设置文字自动换行
- vim查找与替换