hdu 1892 See you~

6002 ワード

長い間acと書いていたが、ずっとreだった.主に2 Dツリー配列
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int c[2000][2000];
int lowbit(int x)
{
    return x&(-x);
}

int sum(int x,int y)
{
    int ret=0,t=y;
    while(x>0)
    {
        y=t;
        while(y>0)
        {
            ret+=c[x][y];
            y-=lowbit(y);
        }
        x-=lowbit(x);
    }
    return ret;
}

void add(int x,int y,int d)
{
    int t=y;
    while(x<=1002)
    {
        y=t;
        while(y<=1002)
        {
            c[x][y]+=d;
            y+=lowbit(y);
        }
        x+=lowbit(x);
    }
}

int main()
{
    int T,n,i,j,k,p,ans,t,a1,a2,b1,b2,n1;
    char s[10];
    scanf("%d",&T);
    for(k=1; k<=T; k++)
    {
        printf("Case %d:
"
,k); memset(c,0,sizeof(c)); for(i=1; i<=1002; i++) for(j=1; j<=1002; j++) add(i,j,1); scanf("%d",&n); for(p=1; p<=n; p++) { scanf("%s",s); if(s[0]=='S') { scanf("%d%d%d%d",&a1,&b1,&a2,&b2); //a1++;b1++;a2++;b2++; if(a1>a2) swap(a1,a2); if(b1>b2) swap(b1,b2); ans=sum(a2+1,b2+1)+sum(a1,b1)-sum(a1,b2+1)-sum(a2+1,b1); printf("%d
"
,ans); } else if(s[0]=='A') { scanf("%d%d%d",&a1,&b1,&n1); //a1++;b1++; add(a1+1,b1+1,n1); } else if(s[0]=='D') { scanf("%d%d%d",&a1,&b1,&n1); //a1++;b1++; t=sum(a1+1,b1+1)+sum(a1,b1)-sum(a1+1,b1)-sum(a1,b1+1); if(t>n1) t=n1; add(a1+1,b1+1,-t); } else { scanf("%d%d%d%d%d",&a1,&b1,&a2,&b2,&n1); //a1++;b1++;a2++;b2++; t=sum(a1+1,b1+1)+sum(a1,b1)-sum(a1+1,b1)-sum(a1,b1+1); if(t>n1) t=n1; add(a1+1,b1+1,-t); add(a2+1,b2+1,t); } } } return 0; }