NUAA 1004 Prince Ray’s Puzzle
Prince Ray’s Puzzle
Total Submit:698 Accepted:113
Description
Prince Ray wants to marry his girl friend, beautiful Princess Amy. Amy loves Ray, and is very willing to take him. However, Amy’s father, King StreamSpeed, insists that his son in law should be talented enough, so that he could let him be the King of this country after King StreamSpeed retired. So he gives Ray a Test.
Ray is given a large brick of size n*n*n, which contains n*n*n grids of size 1. Every grid can be represent as a triple(x, y, z) (1<=x, y, z<=n). There are numbers in every grid. All the numbers are initially set to zero.
King StreamSpeed will do three operations to the brick.
1. Add a number to a given grid’s number
2. Subtract a number from a given grid’s number
3. Query the sum of total number from(x1,y1,z1) to (x2,y2,z2)
(x1 <= x2, y1 <= y2, z1 <= z2)
As Ray ‘s best friend and most excellent coder, you are required to write a program to answer all the queries to help Ray marry with her valentine.
Input
The first line of the input contains an integer n(1<=n<=100), the size of the brick. Then follow several lines in the following format:
A x y z num : Add num to (x, y, z)
S x y z num: Subtract num from (x, y, z)
Q x1 y1 z1 x2 y2 z2 : Query the sum of numbers from (x1, y1 , z1) to (x2 , y2 , z2)
The number of all the queries will be no more than 1000000.Input file is ended by a zero and should not be processed.
Output
For every query in the input file, your program should output the corresponding result -- the sum of numbers in the range (x1, y1, z1) ~(x2, y2, z2). The result does not exceed 10^9.
Sample Input
10
A 1 1 4 5
A 2 5 4 5
Q 1 1 1 10 10 10
S 3 4 5 34
Q 1 1 1 10 10 10
0
Sample Output
10
-24
Hint
Huge input file, scanf is recommended.
Source
Narashy
TIPS:
树状数组
http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees#prob
http://wtommy.ycool.com/post.766159.html
Code
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
//#define Max_N 101
//int c[Max_N][Max_N][Max_N],n;
#define lowbit(t) (t&-t)
int ***c,n;
int summ(int x,int y,int z)
{
int sum=0,y1,z1;
while (x>0)
{
y1=y;
while (y1>0)
{
z1=z;
while (z1>0)
{
sum+=c[x][y1][z1];
z1-=lowbit(z1);
}
y1 -= lowbit(y1);
}
x -= lowbit(x);
}
return sum;
}
void update(int x , int y , int z ,int val){
int y1,z1;
while (x <= n){
y1 = y;
while (y1 <= n){
z1=z;
while (z1<=n)
{
c[x][y1][z1] += val;
z1+=lowbit(z1);
}
y1 += lowbit(y1);
}
x += lowbit(x);
}
}
int main()
{
int x,y,z,val,x1,y1,z1;
char ch;
scanf("%d",&n);
// memset(c,0,sizeof(c));
n++;
c=(int ***)malloc(n*sizeof(int**));
for (x=0;x<n;x++)
{
c[x] = (int**)malloc(n*sizeof(int*));
for (y=0;y<n;y++)
{
c[x][y] = (int*)malloc(n*sizeof(int));
for (z=0;z<n;z++)
{
c[x][y][z]=0;
}
}
}
n--;
getchar();
while (scanf("%c",&ch),ch!='0')
{
getchar();
switch(ch)
{
case 'A':scanf("%d %d %d %d",&x,&y,&z,&val);update(x,y,z,val);break;
case 'S':scanf("%d %d %d %d",&x,&y,&z,&val);val=-val;update(x,y,z,val);break;
case 'Q':scanf("%d %d %d %d %d %d",&x1,&y1,&z1,&x,&y,&z);
x1--;
y1--;
z1--;
printf("%d\n",summ(x,y,z)-summ(x,y,z1)-summ(x,y1,z)-summ(x1,y,z)
+summ(x,y1,z1)+summ(x1,y,z1)+summ(x1,y1,z)-summ(x1,y1,z1)); break;
}
getchar();
}
}
浙公网安备 33010602011771号