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;
    }
  • 相关阅读:
    第十周课程总结
    第九周课程总结&实验报告(七)
    第八周课程总结&实验报告(六)
    第七周课程总结&实验报告(五)
    第六周&java实验报告四
    第五周课程总结&试验报告(三)
    课程总结
    第十四周课程总结
    第十三周学习总结
    第十二周编程总结
  • 原文地址:https://www.cnblogs.com/mltang/p/8987050.html
Copyright © 2011-2022 走看看