zoukankan      html  css  js  c++  java
  • hdu 2642

    这题应该就是标准的二维树状数组,应该没什么难度

    处理一下x,y等于0的情况就过了

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    using namespace std;
    const int maxn = 1e3+10;
    int c[maxn][maxn];
    bool map[maxn][maxn];
    void add(int x,int y,int d);
    int getsum(int x,int y);
    int main()
    {
        int t,x1,y1,x2,y2;
        while(scanf("%d",&t) != EOF)
        {
            memset(c, 0, sizeof(c));
            memset(map, false, sizeof(map));
            while(t--)
            {
                string a;
                cin >> a;
                if(a == "B")
                {
                    scanf("%d%d",&x1,&y1);
                    x1 ++; y1 ++;
                    if(map[x1][y1])
                        continue;
                    add(x1, y1, 1);
                    map[x1][y1] = true;
                }
                else if(a == "D")
                {
                    scanf("%d%d",&x1,&y1);
                    x1 ++; y1 ++;
                    if(!map[x1][y1])
                        continue;
                    add(x1, y1, -1);
                    map[x1][y1] = false;
                }
                else
                {
                    scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
                    x1 ++; y1 ++; x2 ++; y2 ++;
                    if(x1>x2)swap(x1,x2);
                    if(y1>y2)swap(y1,y2);
                    int k = getsum(x2, y2) + getsum(x1-1, y1-1) - getsum(x1-1,y2) - getsum(x2, y1-1);
                    printf("%d
    ",k);
                }
            }
        }
        
    }
    int lowbit(int k)
    {
        return k&(-k);
    }
    void add(int x,int y,int d)
    {
        int i,j;
        for(i=x;i<maxn;i+=lowbit(i))
        {
            for(j=y;j<maxn;j+=lowbit(j))
            {
                c[i][j] += d;
            }
        }
    }
    int getsum(int x,int y)
    {
        int i,j;
        int sum = 0;
        for(i=x;i>0;i-=lowbit(i))
            for(j=y;j>0;j-=lowbit(j))
                sum += c[i][j];
        return sum;
    }
  • 相关阅读:
    BZOJ 3744 Gty的妹子序列
    BZOJ 3872 Ant colony
    BZOJ 1087 互不侵犯
    BZOJ 1070 修车
    BZOJ 2654 tree
    BZOJ 3243 向量内积
    1003 NOIP 模拟赛Day2 城市建设
    CF865D Buy Low Sell High
    CF444A DZY Loves Physics
    Luogu 4310 绝世好题
  • 原文地址:https://www.cnblogs.com/mltang/p/8987050.html
Copyright © 2011-2022 走看看