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:
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;}