1014(暴力树状数组)
来源:互联网 发布:网络直播需要什么资质 编辑:程序博客网 时间:2024/06/06 02:10
Problem Description
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
题目大概:
对一个区间涂黑或涂白,问一个区间黑色块的数量。
思路:
由于数据量小,直接把区间更新暴力为单点更新并且记录颜色数据。
代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int n;int c[102][102];int vis[102][102];int lowbit(int x){ return x&(-x);}void add(int x,int y,int v){ int vv; if(vis[x][y]==v)return; vis[x][y]=v; if(v==1)vv=1; else vv=-1; for(int i=x;i<=101;i+=lowbit(i)) { for(int j=y;j<=101;j+=lowbit(j)) c[i][j]+=vv; }}long long sum(int x,int y){ long long su=0; for(int i=x;i>0;i-=lowbit(i)) { for(int j=y;j>0;j-=lowbit(j)) su+=c[i][j]; } return su;}int main(){ int t; scanf("%d",&t); memset(vis,0,sizeof(vis)); memset(c,0,sizeof(c)); while(t--) { char p[7]; scanf("%s",p); if(p[0]=='B') { int w1,w2,e1; scanf("%d%d%d",&w1,&w2,&e1); for(int i=w1;i<w1+e1;i++) { for(int j=w2;j<w2+e1;j++) { add(i,j,1); } } } else if(p[0]=='W') { int w1,w2,e1; scanf("%d%d%d",&w1,&w2,&e1); for(int i=w1;i<w1+e1;i++) { for(int j=w2;j<w2+e1;j++) { add(i,j,0); } } } else if(p[0]=='T') { int r1,r2,r3; scanf("%d%d%d",&r1,&r2,&r3); int sun=0; r3--; sun=sum(r1+r3,r2+r3)-sum(r1+r3,r2-1)-sum(r1-1,r2+r3)+sum(r1-1,r2-1); printf("%d\n",sun); } } return 0;}
阅读全文
0 0
- 1014(暴力树状数组)
- ssoj1046密码pasuwado(树状数组+暴力)
- 求逆序数(暴力,归并,树状数组)
- 【bzoj3300】 USACO2011Best Parenthesis 暴力+树状数组
- Query on a string 暴力+树状数组
- UVA 12647 Balloon (线段树) URAL 1915Titan Ruins: Reconstruction of Bygones (暴力,树状数组)
- 2007 Asia Regional Chengdu(树状数组神题+割点暴力+STL)
- Codeforces Round #368 (Div. 2) E Garlands(二维树状数组+暴力)
- Codeforces Round #368 (Div. 2) E Garlands(二维树状数组+暴力)
- HDU-1394-Minimum Inversion Number(暴力||归并排序||线段树||树状数组)
- HDU 1085(暴力;找规律;树状数组;母函数)
- POJ 2182 Lost Cows(树状数组,暴力解法)
- hdu 4031 Attack(树状数组区间更新单点求值&暴力)
- cf#10-B - Cinema Cashier-暴力+树状数组
- 【bzoj4551】[Tjoi2016&Heoi2016]树 暴力?树剖+树状数组+二分
- (转)树状数组
- 树状数组(转载)
- Stars(树状数组)
- 图像处理实例--Retinex增强处理
- 自定义Gradle插件
- 2017 Multi-University Training Contest
- Python 代码的准确性和可维护性
- LeetCode---twoSum
- 1014(暴力树状数组)
- canvas实现粒子星空连线
- HTML5基础篇
- Linux zip 与 unzip命令详解
- ORA-00911: invalid character 错误解决
- HDU 6162 Ch’s gift (树的路径问题 DFS LCA 17多校第九场第2题)
- POJ2437
- schost.exe总是占用内存
- Ubuntu 16.04 上的 NGINX Web 服务器!