您的位置:首页 > 其它

hdu 1892 二维树状数组

2015-01-29 12:14 309 查看
#include<stdio.h>

#include<string.h>

#include<iostream>

usingnamespacestd;


#defineMAX1000+10

#defineMAXN1002

intc[MAX][MAX],list[MAX][MAX];


intlowbit(intx)

{

returnx&(-x);

}


voidmodify(intx,inty,intnum)

{

for(inti=x;i<=MAXN;i+=lowbit(i))

for(intj=y;j<=MAXN;j+=lowbit(j))

c[i][j]+=num;

}


intsum(intx,inty)

{

intall=0;

for(inti=x;i>0;i-=lowbit(i))

for(intj=y;j>0;j-=lowbit(j))

all+=c[i][j];

returnall;

}


voidmswap(int&a,int&b)

{

if(a<b)

{

inttp=a;a=b;b=tp;

}

}


intmain()

{

//freopen("read.txt","r",stdin);


intT;

scanf("%d",&T);

intco=1;

while(T--)

{

printf("Case%d:\n",co++);

memset(c,0,sizeof(c));		//初始化啊!!!!

for(inti=1;i<=MAXN;i++)

for(intj=1;j<=MAXN;j++)

	{

list[i][j]=1;

modify(i,j,1);

}


intn;

scanf("%d",&n);

while(n--)

{

intx1,y1;

charstr;


cin>>str>>x1>>y1;


x1++;y1++;	//题目的坐标是从0开始的,树状数组要求从1开始,


if(str=='S')

	{

	intx2,y2;

	scanf("%d%d",&x2,&y2);

	x2++,y2++;

	mswap(x2,x1);	mswap(y2,y1);//果然,(x2,y2)可能比(x1,y1)要小……

	inttp=sum(x2,y2)+sum(x1-1,y1-1)-sum(x1-1,y2)-sum(x2,y1-1);

	printf("%d\n",tp);

}


elseif(str=='A')

	{

	inttp;		scanf("%d",&tp);

	modify(x1,y1,tp);


	list[x1][y1]+=tp;//

}


elseif(str=='D')

	{

	inttp;		scanf("%d",&tp);

	if(list[x1][y1]<tp)	tp=list[x1][y1];

	modify(x1,y1,-tp);

	list[x1][y1]-=tp;

}


elseif(str=='M')

	{

	intx2,y2,tp;

	scanf("%d%d%d",&x2,&y2,&tp);

	x2++;y2++;


	if(list[x1][y1]<tp)	tp=list[x1][y1];


	list[x1][y1]-=tp;

	list[x2][y2]+=tp;

	

	modify(x1,y1,-tp);

	modify(x2,y2,tp);

}

}

}

return0;

}


ProblemDescription

NowIamleavinghustacm.Inthepasttwoandhalfyears,IlearnedsomanyknowledgeaboutAlgorithmandProgramming,andImetsomanygoodfriends.IwanttosaysorrytoMr,Yin,Imustleavenow~~>.<~~.Iamverysorry,wecouldnotadvancedtotheWorldFinalslastyear.

Whencomingintoourtrainingroom,alotofbooksareinmyeyes.Andeverytimethebooksaremovingfromoneplacetoanotherone.Nowgiveyouthepositionofthebooksattheearlyoftheday.Andthemovinginformationofthebookstheday,yourworkistotellmehowmanybooksarestayedinsomerectangles.

Tomaketheproblemeasier,wedividetheroomintodifferentgridsandabookcanonlystayedinonegrid.Thelengthandthewidthoftheroomarelessthan1000.Icanmoveonebookfromonepositiontoanotherposition,takeawayonebookfromapositionorbringinonebookandputitononeposition.



Input

InthefirstlineoftheinputfilethereisanIntegerT(1<=T<=10),whichmeansthenumberoftestcasesintheinputfile.ThenNtestcasesarefollowed.

Foreachtestcase,inthefirstlinethereisanIntegerQ(1<Q<=100,000),meansthequeriesofthecase.ThenfollowedbyQqueries.

Thereare4kindofqueries,sum,add,deleteandmove.

Forexample:

Sx1y1x2y2meansyoushouldtellmethetotalbooksoftherectangleused(x1,y1)-(x2,y2)asthediagonal,includingthetwopoints.

Ax1y1n1meansIputn1booksontheposition(x1,y1)

Dx1y1n1meansImoveawayn1booksontheposition(x1,y1),iflessthann1booksatthatposition,moveawayallofthem.

Mx1y1x2y2n1meansyoumoven1booksfrom(x1,y1)to(x2,y2),iflessthann1booksatthatposition,moveawayallofthem.

Makesurethatatfirst,thereisonebookoneverygridand0<=x1,y1,x2,y2<=1000,1<=n1<=100.



Output

Atthebeginningofeachcase,output"CaseX:"whereXistheindexofthetestcase,thenfollowedbythe"S"queries.

Foreach"S"query,justprintoutthetotalnumberofbooksinthatarea.



SampleInput

2

3

S1111

A112

S1111

3

S1111

A112

S1112



SampleOutput

Case1:

1

3

Case2:

1

4

*/

[/code]

来自为知笔记(Wiz)

附件列表

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: