HDU 1892 See you~

Problem Description
Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. I want to say sorry to Mr, Yin, I must leave now ~ ~>.<~ ~. I am very sorry, we could not advanced to the World Finals last year.
When coming into our training room, a lot of books are in my eyes. And every time the books are moving from one place to another one. Now give you the position of the books at the early of the day. And the moving information of the books the day, your work is to tell me how many books are stayed in some rectangles.
To make the problem easier, we divide the room into different grids and a book can only stayed in one grid. The length and the width of the room are less than 1000. I can move one book from one position to another position, take away one book from a position or bring in one book and put it on one position.


【题目分析】
特别萌的一道题目。题目名字~~~,符号表情也特别萌。
扯远了(尴尬)。
这道题目是要区间求和,利用容斥原理分割成四个部分很容易地求解。二位的数据结构其实和一维的数据结构相差并不是很大。只需要维护两个下标就好了。方法和原理都是同样的。而且每一个维度的下标都要从1开始(树状数组必须的,如果是线段树的话完全没有必要,个人觉得线段树会被卡掉的,常数略大)。


【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1002;
int a[maxn][maxn];
int tt,kase=0,n;
inline int lowbit(int x){return x&(-x);}
inline void add(int x,int y,int op)
{
    while (x<maxn)
    {
        int k=y;
        while (k<maxn)
        {
            a[x][k]+=op;
            k+=lowbit(k);
        }
        x+=lowbit(x);
    }
}
inline int sum(int x,int y)
{
    int res=0;
    while(x>0)
    {
        int k=y;
        while(k>0)
        {
            res+=a[x][k];
            k-=lowbit(k);
        }
        x-=lowbit(x);
    }
    return res;
}
inline int find(int x,int y)
{
    return sum(x,y)-sum(x-1,y)-sum(x,y-1)+sum(x-1,y-1);
}
int main()
{
    cin>>tt;
    while (tt--)
    {
        cout<<"Case "<<++kase<<":"<<endl;
        cin>>n;
        memset(a,0,sizeof a);
        for (int i=1;i<=maxn;++i)
            for (int j=1;j<maxn;++j)
                add(i,j,1);
        char ch[6];
        while (n--)
        {
            scanf("%s",ch);
            if (ch[0]=='A'||ch[0]=='D')
            {
                int x,y,op;
                cin>>x>>y>>op;
                if (ch[0]=='D') op=-min(op,find(x+1,y+1));
                add(x+1,y+1,op);
                continue;
            }
            if (ch[0]=='M')
            {
                int x1,y1,x2,y2,op;
                cin>>x1>>y1>>x2>>y2>>op;
                int tmp=min(op,find(x1+1,y1+1));
                add(x1+1,y1+1,-tmp),add(x2+1,y2+1,tmp);
                continue;
            }
            else
            {
                int x1,y1,x2,y2;
                cin>>x1>>y1>>x2>>y2;
                if (x1>x2) swap(x1,x2); if (y1>y2) swap(y1,y2);
                int tmp=sum(x2+1,y2+1)-sum(x1,y2+1)-sum(x2+1,y1)+sum(x1,y1);
                cout<<tmp<<endl;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值