HDU 2642 二维树状数组

简介:

题意很明确 给你一个图某坐标上的星星亮 暗 条件 求出当前区间内所有亮星星的总数 

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1005;
int tree[maxn + 2][maxn + 2];
inline int Lowbit(int x)
{
    return x&(-x);
}
inline void Update(int x,int y,int v)
{
    for(int i=x; i<=maxn; i+=Lowbit(i))
        for(int j=y; j<=maxn; j+=Lowbit(j))
            tree[i][j]+=v;
}
inline int Query(int x,int y)
{
    int sum=0;
    for(int i=x; i>0; i-=Lowbit(i))
        for(int j=y; j>0; j-=Lowbit(j))
            sum+=tree[i][j];
    return sum;
}
bool map[1005][1005];
int main()
{
    int n;
    char c[3];
    memset(tree,0,sizeof(tree));
    memset(map,0,sizeof(map));
    cin>>n;
    while(n--)
    {
        scanf("%s",c);
        if(c[0]=='B')
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(!map[x+1][y+1])
                map[x+1][y+1]=1,Update(x+1,y+1,1);
        }
        else if(c[0]=='D')
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(map[x+1][y+1]==1)
                map[x+1][y+1]=0,Update(x+1,y+1,-1);
        }
        else if(c[0]=='Q')
        {
            int x1,x2,y1,y2;
            scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
            if(x1>x2) swap(x1,x2);
            if(y1>y2) swap(y1,y2);
            int ans=Query(x2+1,y2+1)-Query(x1,y2+1)-Query(x2+1,y1)+Query(x1,y1);
            printf("%d\n",ans);
        }
    }
    return 0;
}


目录
打赏
0
1
1
0
1
分享
相关文章
hdu 2503 a/b + c/d
hdu 2503 a/b + c/d
61 0
畅通工程 HDU - 1232
畅通工程 HDU - 1232
94 0
HDU2203亲和串
博客水平见水平......目前阶段就是这么菜,我会好好努力的!毕业直接拿到阿里offer!
1245 0
HDU 2549 壮志难酬
壮志难酬 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12541    Accepted Submission(s): 4166 Problem Description 话说MCA山上各路豪杰均出山抗敌,去年曾在江湖威名显赫的,江湖人称的甘露也不甘示弱,“天将降大任于斯人也,必先劳其筋骨,饿其体肤,空乏其身”他说。
1047 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等