标签:des java os io for ar div 代码
2 3 S 1 1 1 1 A 1 1 2 S 1 1 1 1 3 S 1 1 1 1 A 1 1 2 S 1 1 1 2
Case 1: 1 3 Case 2: 1 4
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int n=1010;
int c[n+1][n+1];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
for(int j=y;j<=n;j+=lowbit(j))
{
c[i][j]+=val;
}
}
}
int getsum(int x,int y)
{
int cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
for(int j=y;j>=1;j-=lowbit(j))
{
cnt+=c[i][j];
}
}
return cnt;
}
int val[n+1][n+1];
int main()
{
int ci;scanf("%d",&ci);
int pl=1;
while(ci--)
{
memset(c,0,sizeof(c));
printf("Case %d:\n",pl++);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
update(i,j,1);
val[i][j]=1;
}
}
int sa;scanf("%d",&sa);
while(sa--)
{
char ch;cin>>ch;
if(ch==‘S‘)
{
int xx1,yy1,xx2,yy2;
scanf("%d%d%d%d",&xx1,&yy1,&xx2,&yy2);
xx1++,xx2++,yy1++,yy2++;//从1开始
int x1,x2,y1,y2;
x1=min(xx1,xx2);x2=max(xx1,xx2);
y1=min(yy1,yy2);y2=max(yy1,yy2);
int cnt=getsum(x2,y2)-getsum(x1-1,y2)-getsum(x2,y1-1)+getsum(x1-1,y1-1);
printf("%d\n",cnt);
}
else if(ch==‘A‘)
{
int x,y,l;
scanf("%d%d%d",&x,&y,&l);
x++,y++;
update(x,y,l);
val[x][y]+=l;
}
else if(ch==‘D‘)
{
int x,y,l;
scanf("%d%d%d",&x,&y,&l);
x++,y++;
if(l>val[x][y]) l=val[x][y];//important
update(x,y,-l);
val[x][y]+=-l;
}
else
{
int x1,x2,y1,y2,l;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&l);
x1++,x2++,y1++,y2++;
if(l>val[x1][y1]) l=val[x1][y1];//important
update(x1,y1,-l);
val[x1][y1]+=-l;
update(x2,y2,l);
val[x2][y2]+=l;
}
}
}
return 0;
}
hdoj 1892(二维树状数组),布布扣,bubuko.com
标签:des java os io for ar div 代码
原文地址:http://www.cnblogs.com/weiyikang/p/3900651.html