HDU 1166 Enemy formation

enemy formation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 111372    Accepted Submission(s): 46682

Problem Description
Country C's nemesis, Country A, is currently conducting military exercises, so country C's spy chief Derek and his subordinate Tidy are busy again. Country A has arranged N engineer camps along the coastline. Derek and Tidy's task is to monitor the activities of these engineer camps. Due to the adoption of some advanced monitoring methods, country C has a clear grasp of the number of engineers in each engineer camp. The number of engineers in each engineer camp may change, and some personnel may be increased or decreased, but these cannot escape C. country surveillance.
The Central Intelligence Agency wants to study what tactics the enemy is practicing, so Tidy has to report to Derek how many people there are in a certain continuous engineer camp. For example, Derek asked: "Tidy, immediately report how many people there are in the third camp to the tenth camp !" Tidy was about to start counting the total for the segment and reporting back. But the number of enemy camps often changes, and Derek asks different segments each time, so Tidy has to count each camp one by one, and soon exhausted, the faster Derek calculates Tidy Lai is more and more dissatisfied: "You fat boy, you are so slow, I will fire you!" Tidy thought: "You can do the math yourself, this is really a tiring job! I wish you would fire me! In desperation, Tidy had to call Windbreaker, a computer expert, for help. Windbreaker said: "Damn fat boy, I told you to do more acm problems and read more algorithm books. Now you have tasted the bitter fruit!" Tidy said: " I was wrong..." But Windbreaker had hung up. Tidy is very distressed, so he will really collapse, smart reader, can you write a program to help him do this work? However, if your program is not efficient enough, Tidy will still be scolded by Derek.
Input
The first line contains an integer T, indicating that there are T groups of data.
The first line of each set of data has a positive integer N (N<=50000), indicating that the enemy has N engineer camps, followed by N positive integers, the i-th positive integer ai represents the i-th engineer camp with ai at the beginning Individual (1<=ai<=50).
Next, there is one command per line, and the command has 4 forms:
(1) Add ij, i and j are positive integers, indicating that j people are added to the i-th camp (j does not exceed 30)
(2) Sub ij, i and j It is a positive integer, which means that the i-th camp is reduced by j people (j does not exceed 30);
(3) Query ij , i and j are positive integers, i<=j, which means that the total number of people from the i-th to the j-th camp is asked;
(4) End means the end, this command appears at the end of each group of data;
each group of data has a maximum of 40,000 commands
Output
For the i-th group of data, first output "Case i:" and carriage return.
For each Query query, output an integer and carriage return, indicating the total number of people in the query segment, which is kept 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
Author
Windbreaker
 

 

Recommend
Eddy
Ideas: a board for single-point modification of line segment trees and interval query.
The reason for the error: But four, I don't know why, using string will compile errors, and changing to char array will pass.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 50001
using namespace std;
char s[101];
int t,n,tot;
struct nond{
    int l,r,sum;
}tree[MAXN*4];
void up(int now){
    tree[now].sum=tree[now*2].sum+tree[now*2+1].sum;
}
void build(int now,int l,int r){
    tree[now].l=l;tree[now].r=r;
    if(tree[now].l==tree[now].r){
        scanf("%d",&tree[now].sum);
        return ;
    }
    int mid=(tree[now].l+tree[now].r)/2;
    build(now*2,l,mid);
    build(now*2+1,mid+1,r);
    up(now);
}
void change(int now,int x,int k){
    if(tree[now].l==tree[now].r){
        tree[now].sum+=k;
        return ;
    }
    int mid=(tree[now].l+tree[now].r)/2;
    if(x<=mid)    change(now*2,x,k);
    else change(now*2+1,x,k);
    up(now);
}
int query(int now,int l,int r){
    if(tree[now].l==l&&tree[now].r==r)
        return tree[now].sum;
    int mid=(tree[now].l+tree[now].r)/2;
    if(r<=mid)    return query(now*2,l,r);
    else if(l>mid)    return query(now*2+1,l,r);
    else    return query(now*2,l,mid)+query(now*2+1,mid+1,r);
}
int main(){
    scanf("%d",&t);
    while(t--){
        tot++;printf("Case %d:\n",tot);
        scanf("%d",&n);
        build(1,1,n);
        int x,y;
        while(cin>>s&&s[0]!='E'){
            if(s[0]=='Q'){ scanf("%d%d",&x,&y);printf("%d\n",query(1,x,y)); }
            else if(s[0]=='A'){ scanf("%d%d",&x,&y);change(1,x,y); }
            else if(s[0]=='S'){ scanf("%d%d",&x,&y);change(1,x,-y); }
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066703&siteId=291194637