HDU1166 Enemy formation

enemy formation

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

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, and 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 CIA 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 to tenth camps. !" 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 sections 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 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 the computer expert Windbreaker 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 knew 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

meaning of the title

  Group example, n camps, Query represents the sum of the query interval [i, j], Add represents the ith camp plus j, Sub represents the ith camp minus j, and ends when it encounters End.

Problem solving ideas

  Construct the line segment tree, pay attention to the update.

code

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+5;

int arr[maxn<<2];
void pushup(int rt)
{
    arr[rt]=arr[rt<<1]+arr[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        cin>>arr[rt];
        return;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(rt);
}
void update(int L,int c,int l,int r,int rt)
{
    if(l==r) {arr[rt]+=c;return;}
    int m=(l+r)>>1;
    if(L<=m) update(L,c,l,m,rt<<1);
    else update(L,c,m+1,r,rt<<1|1);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r) return arr[rt];
    int m=(l+r)>>1;
    int ans=0;
    if(L<=m) ans+=query(L,R,l,m,rt<<1);
    if(R> m) ans+=query(L,R,m+1,r,rt<<1|1);
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    for(int k=1; k<=t; k++)
    {
        printf("Case %d:\n",k);
        int n;
        cin>>n;
        build(1,n,1);
        string str;
        int l,r;
        while(cin>>str)
        {
            if(str=="End") break;
            cin>>l>>r;
            if(str=="Query") printf("%d\n",query(l,r,1,n,1));
            else if(str=="Add") update(l,r,1,n,1);
            else update(l,-r,1,n,1);
        }
//        system("pause");
    }
    return 0;
}

Guess you like

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