HDU 2642 Stars(二维树状数组)

Problem Description
Yifenfei is a romantic guy and he likes to count the stars in the sky.
To make the problem easier,we considerate the sky is a two-dimension plane.Sometimes the star will be bright and sometimes the star will be dim.At first,there is no bright star in the sky,then some information will be given as "B x y" where 'B' represent bright and x represent the X coordinate and y represent the Y coordinate means the star at (x,y) is bright,And the 'D' in "D x y" mean the star at(x,y) is dim.When get a query as "Q X1 X2 Y1 Y2",you should tell Yifenfei how many bright stars there are in the region correspond X1,X2,Y1,Y2.

There is only one case.

Input
The first line contain a M(M <= 100000), then M line followed.
each line start with a operational character.
if the character is B or D,then two integer X,Y (0 <=X,Y<= 1000)followed.
if the character is Q then four integer X1,X2,Y1,Y2(0 <=X1,X2,Y1,Y2<= 1000) followed.

Output
For each query,output the number of bright stars in one line.

Sample Input
  
  
5 B 581 145 B 581 145 Q 0 600 0 200 D 581 145 Q 0 600 0 200

Sample Output
  
  
1 0
题目大意:给出一个坐标系,这个坐标系里面有很多星星,(一开始都是灭的)。可以令某个星星亮或者灭。还可以求(x1,y1)到(x2,y2)之间亮星星的个数。
思路:要注意,当输入B,让星星亮的时候,得考虑星星以前的状态,当输入D,如果星星本来就灭了呢?所以得开一个数组纪录所有星星的状态。如果能想到这点就没问题了。这题还有个大坑。(WA好几次,参考别人的才知道)当输入Q查询时,不一定从小坐标到大坐标,没准大坐标在前面。。。所以也要处理。其余细节见代码乐...
#include<stdio.h>
#include<string.h>
#define MAX 1005
#define max(a,b) a>b?a:b;
#define min(a,b) a>b?b:a;
int c[MAX][MAX];
int a[MAX][MAX];         //记录所有星星状态
int lowbit(int x)
{
    return x&(-x);
}
void updata(int x,int y,int d)
{
    for(int i=x;i<MAX;i=i+lowbit(i))
        for(int j=y;j<MAX;j=j+lowbit(j))
        {
            c[i][j]=c[i][j]+d;
        }
}
int getsum(int x,int y)
{
    int res = 0;
    for(int i=x;i>0;i=i-lowbit(i))
        for(int j=y;j>0;j=j-lowbit(j))
        res=res+c[i][j];
    return res;
}
int main()
{
    int t;
    int x,y;
    int x1,x2,y1,y2;
    int m1,m2,n1,n2;
    char s[10];
    int ans;
    memset(c,0,sizeof(c));
    memset(a,0,sizeof(a));
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%s",s);
        if(s[0]=='B')
        {
            scanf("%d%d",&x,&y);
            x++;y++;                //数组数组下标从1开始。。。
            if(a[x][y]==0)          //只有灭的星星被点亮
            {
            a[x][y]=1;
            updata(x,y,1);
            }
        }
        if(s[0]=='D')
        {
            scanf("%d%d",&x,&y);
            x++;y++;
           if(a[x][y])              //只有星星亮了,才能灭
           {
            a[x][y]=0;
            updata(x,y,-1);
           }
        }
        if(s[0]=='Q')
        {   //这是一个坑,他只说x1,x2为横坐标,y1,y2为纵坐标。
            //得从新排序, X1<=X2 Y1<=Y2 
            scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
            x1++;x2++;y1++;y2++;
            m1=max(x1,x2);m2=min(x1,x2);n1=max(y1,y2);n2=min(y1,y2);
            ans=getsum(m1,n1)+getsum(m2-1,n2-1)-getsum(m1,n2-1)-getsum(m2-1,n1);
            //这个求和是容斥原理。。。百度就知道
            printf("%d\n",ans);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值