Please bypass during construction...
Original question stamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 83448 Accepted Submission(s): 35255
Problem Description
Country C's deadly rival, Country A, was conducting military exercises during this period, so Derek, the spy chief of Country C, and Tidy, his men, were busy again. The task of Derek and Tidy is to monitor the activities of N engineering camps along the coastline of State A. As a result of some advanced monitoring methods, the number of each engineer camp is well known in Country C. The number of each engineer camp may change, and it may increase or decrease a number of manpower, but these can not escape the surveillance of Country C.
The CIA wants to study what tactics the enemy is exercising, so Tidy needs to report to Derek at any time how many people there are in a successive engineering camp, such as Derek: "Tidy, report immediately how many people there are from the third camp to the tenth camp!" Tidy needs to start calculating the total number of people in this section and reporting immediately. But the number of enemy barracks often changes, and Derek asks for different sections, so Tidy has to count one camp at a time, and soon exhausted, Derek is increasingly dissatisfied with Tidy's calculation speed: "You are a fat boy, so slow, I fired you squid!" Tidy thought: "You can count it yourself, it's really a tiring job! I hate it!" You fired me! "In desperation, Tidy had to call computer expert Windbreaker for help. Windbreaker said,"Dead fat boy, ask you to do more acm problems and read more algorithmic books at ordinary times. Now you've tasted the bitter fruit!"Tidy said,"I know wrong." But Windbreaker has already hung up. Tidy is distressed, so he's really going to crash, smart reader. Can you write a program to help him do this? But if your program is not efficient enough, Tidy will still be scolded by Derek.
Input
The first line is an integer T, indicating that there are T groups of data.
A positive integer N (N <= 50000) in the first row of each set of data indicates that the enemy has N Engineer barracks, followed by N positive integers, and a positive integer ai in the second row represents an ai individual at the beginning of the first engineer barracks (1 <= ai <= 50).
Next, there is a command on each line. There are four forms of command:
(1) Add i j,i and j are positive integers, representing an increase of J individuals in the first camp (j does not exceed 30)
(2) Sub i j, i and j are positive integers, indicating that the number of J individuals in the first camp is reduced (j does not exceed 30);
(3) Query i j, i and j are positive integers, i <=j, which means asking about the total number of camps i to j;
(4)End means the end, and this command appears at the end of each set of data.
Up to 40,000 commands per set of data
Output
For group I data, first output "Case i:" and return.
For each Query query, output an integer and return to indicate the total number of people in the query section, which remains within int.
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59
Title Solution
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define lson l,m,rt<<1//(rt*2);
#define rson m+1,r,(rt<<1)|1//(rt*2+1);
int tree[50005<<2];
void pushup(int rt)//Maintain segment tree data (from bottom to top).
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int Query(int L,int R,int l,int r,int rt)//(L,R) interval acquisition sum
{
if(L<=l&&r<=R)//If an interval is satisfied
{
return tree[rt];//Returns the value of an interval.
}
else
{
int m=(l+r)>>1;
int ans=0;
if(L<=m)//If the leftmost camp of the goal interval is on the left of the penalty point
{
ans+=Query(L,R,lson);//Continue to search for the destination interval (or possibly the destination) in the left subtree.
}
if(m<R)
{
ans+=Query(L,R,rson);//Empathy
}
return ans;
}
}
void build( int l ,int r , int rt )
{
if( l == r )
{
scanf("%d",&tree[rt]);//Input data into leaves.
return ;
}
else
{
int m = (l+r)>>1 ;
build(lson) ;
build(rson) ;
pushup(rt) ;//Maintain data from bottom to top.
}
}
void update(int p,int c,int l,int r,int rt)//p camp c data.
{
//printf("%d %d %d %d %d\n",p,c,l,r,rt);
if(l==r)//Find the p camp
{
tree[rt]+=c;//Add c data
}
else
{
int m=(l+r)>>1;//m is a bifurcated penalty point.
if(p<=m) update(p,c,lson);//If the p camp is obviously in the left subtree. Look to the left
else update(p,c,rson);//Look for the right side
pushup(rt);//Of course, data needs to be maintained.
//printf("sum[%d]:%d\n",rt,tree[rt]);
}
}
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",++kase);
memset(tree,0,sizeof(tree));
int n;
scanf("%d",&n);
build(1,n,1);
char s[30];int a,b;
/*for(int i=0;i<30;i++)
printf("%d ",tree[i]);*/
while(~scanf("%s",s))//Corresponding operation.
{
if(s[0]=='E')break;
scanf("%d%d",&a,&b);
if(s[0]=='Q')
printf("%d\n",Query(a,b,1,n,1));
else if(s[0]=='S')
update(a,-b,1,n,1);
else
update(a,b,1,n,1);
}
}
}