HDU 2642 Stars [二维树状数组]【数据结构】

前端之家收集整理的这篇文章主要介绍了HDU 2642 Stars [二维树状数组]【数据结构】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2642

—————————————————————————————–.
Stars

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1675 Accepted Submission(s): 706

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,Y2(0 <=X1,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

Author
teddy

Source
百万秦关终属楚

—————————————————————————————–.

题目大意:
就是一共有M个操作
操作一共有3种
1)B x y (x,y)位置上出现星星
2)D x y (x,y)位置的星星消失了
3)Q x1 x2 y1 y1 查询矩形范围内的星星有多少个

解题思路:

思路上没有什么可说的,因为是动态的 所以必须是二维树状数组,维护即可

注意的是 ,同一坐标下最多只能有一个星星,删除的话这个坐标就没有星星了

注意 做题的时候注意下坐标的范围 ,因为是从0开始那么计算的时候所有坐标都要加1 否则的话会无限TLE

附本题代码
———————————————————————————————.

#include <bits/stdc++.h>

#define abs(x) (((x)>0)?(x):-(x))
#define lalal puts("*********")
#define Rep(a,b,c) for(int a=(b);a<=(c);a++)
#define Req(a,c) for(int a=(b);a>=(c);a--)
#define Rop(a,c) for(int a=(b);a<(c);a++)
#define s1(a) scanf("%d",&a)
typedef long long int LL;
using namespace std;
/**************************************/
const int N = 1000+5;
#define lowbit(x) (x&-x)
LL sum[N][N];
bool h[N][N];

void update(int xi,int yi,int val){
    for(int i=xi;i<=N;i+=lowbit(i))
        for(int j=yi;j<=N;j+=lowbit(j))
            sum[i][j]+=val;
    return;
}

int getSum(int xi,int yi){
    int ans = 0;
    for(int i=xi;i>0;i-=lowbit(i))
        for(int j=yi;j>0;j-=lowbit(j))
            ans+=sum[i][j];
    return ans ;
}

int main(){
    int n;
    while(~s1(n)){
        memset(sum,0,sizeof(sum));
        memset(h,sizeof(h));
        char ch;
        int x1,y1,x2,y2;
        int tem;
        while(n--){
            getchar();
            scanf("%c",&ch);
            if('B'==ch){
                s1(x1),s1(y1);
                x1++,y1++;
                if(h[x1][y1]) continue;
                update(x1,1);
                h[x1][y1]=1;
            }
            else if('D'==ch){
                s1(x1),y1++;
                if(h[x1][y1]) update(x1,-1);
                h[x1][y1]=0;
            }
            else {
                s1(x1),s1(x2),s1(y1),s1(y2);
                x1++,y1++,x2++,y2++;
                if(x1>x2) swap(x1,x2);
                if(y1>y2) swap(y1,y2);
                tem=getSum(x2,y2)-getSum(x2,y1-1)-getSum(x1-1,y2)+getSum(x1-1,y1-1);
                printf("%d\n",tem);
            }
        }
    }
    return 0;
}

猜你在找的数据结构相关文章