HDU 1166 enemy deployment (tree array)

Source: Internet
Author: User
Tags tidy

Following the training program of freshman year, I saw a tree array and learned it a year ago. I took a look and understood it right away.

 

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 39849 accepted submission (s): 16819


Problem descriptionc's dead enemy country A is conducting military exercises during this time, so Derek and tidy of country C are busy again. Country A has deployed n barracks along the coastline. Derek and tidy are responsible for monitoring the activities of these barracks. Some advanced monitoring means have been adopted, so the number of people in each engineer camp is clearly understood by country C. The number of people in each engineer camp may change, and the number of workers may increase or decrease, but none of these can escape the surveillance of country C.
The Central Intelligence Agency needs to study what tactics the enemy actually exercises, so tidy must report to Derek at any time the total number of people in a continuous barracks. For example, Derek asked: "tidy, report on the total number of people from 3rd camps to 10th camps!" Tidy is about to calculate the total number of people in this section and report it immediately. However, the number of people in the enemy barracks often changes, and Derek asks for different segments each time. Therefore, tidy has to go to the camp one by one each time and is exhausted soon, derek is getting less and more dissatisfied with tidy's computing speed: "You are a fat boy, it's so slow. I'll fry you!" Tidy thought, "you can calculate it yourself. This is really a tiring job! I wish you fired my squid !" In desperation, tidy had to call windbreaker, a computer expert, for help. Windbreaker said: "Fat Boy, I want you to do more ACM questions and read more algorithm books. Now I have a bitter taste !" Tidy said, "I know the error... "However, windbreaker is disconnected. Tidy is very upset. In this case, he will actually crash. Smart readers, can you write a program to help him complete the job? However, if your program is not efficient enough, tidy will still be scolded by Derek.

 

The first line of input is an integer T, indicating that T groups of data exist.
The first line of each group of data is a positive integer n (n <= 50000), indicating that the enemy has n barracks, followed by n positive integers, the I positive integer AI represents an AI individual (1 <= AI <= 50) at the beginning of the I barracks ).
Next, each line contains a command in four forms:
(1) Add I j, I and j are positive integers, indicating that J individuals are added to camp I (J cannot exceed 30)
(2) sub I j, I and j are positive integers, indicating that J individuals are reduced in camp I (J cannot exceed 30 );
(3) query I j, I and j are positive integers. I <= J indicates the total number of camp I to J;
(4) end indicates the end. This command appears at the end of each group of data;
Each group of data can contain a maximum of 40000 commands.

 

Output for group I data, first output "case I:" And press enter,
For each query, output an integer and press enter to indicate the total number of the queried segments, which is kept within Int.

 

Sample input1101 2 3 4 5 6 7 8 9 10 query 1 3add 3 6 query 2 7sub 10 2Add 6 3 query 3 10end

 

Sample outputcase 1: 63359 ============================================== ============================================================ like most tree arrays, in this figure, C1 = A1; C3 = A3; C5 = A5; C7 = A7; C2 = A1 + A1; C4 = A1 + A2 + A3 + A4; c6 = A5 + A6; C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8
int Lowbit(int x){    return x&(-x);}

I printed the output result of this function from 1 to 10.

1 2 1 4 1 2 1 8 1 2

Is how many items are added

N 1 2 3 4 5 6 7 8 9 10

Lowbit 1 2 1 4 1 2 1 8 1 2

C1 = A1 C2 = C3 = C4 = C5 = C6 = C7 = C8 = ......

A1 + A2 A3 A1 + A2 + A5 A5 + A6 A7 A1 + A2 +

A3 + A4 A3 + A4 + A5 + A6 + A7 + A8

 

The lowbit solution calculates the number of items that the CI should add.

void Update(int x,int i){    while(x<=n)    {        c[x]+=i;        x+=Lowbit(x);    }}

The update function updates the value of a node and updates all X-related values.

For example:

When I enter 10 numbers, the corresponding C [I] value changes:

Input 1 1 1 0 1 0 0 0 1 0 0 // corresponding C [1], C [2], C [4], C [8] Change

Input 2 1 3 0 3 0 0 0 3 0 0

Input 3 1 3 3 6 0 0 6 0 0

Input 4 1 3 3 10 0 0 0 0 0

Input 5 1 3 3 10 5 5 0 15 0 0

......

 

int Sum(int x){    int s=0;    while(x>0)    {        s+=c[x];        x-=Lowbit(x);    }    return s;}

The addition operation uses the previously obtained C [I] to quickly obtain the sum value in the interval.

Perform step-by-step simulation on the paper ......

 

Paste your code

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#define Maxx 50005int c[Maxx];int n;int Lowbit(int x){    return x&(-x);}void Update(int x,int i){    while(x<=n)    {        c[x]+=i;        x+=Lowbit(x);    }}int Sum(int x){    int s=0;    while(x>0)    {        s+=c[x];        x-=Lowbit(x);    }    return s;}int main(){    int t,i,j,m,a,b;    scanf("%d",&t);    int cas=1;    while(t--)    {        memset(c,0,sizeof(c));        //cas++;        printf("Case %d:\n",cas++);        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&m);            Update(i,m);//for(int j=1;j<=n;j++)printf("%d  ",c[j]);        }        char str[10];        while(scanf("%s",str)!=EOF&&str[0]!=‘E‘)        {            scanf("%d%d",&a,&b);            if(str[0]==‘Q‘)            {                printf("%d\n",Sum(b)-Sum(a-1));            }            else if(str[0]==‘A‘)            {                Update(a,b);            }            else if(str[0]==‘S‘)            {                Update(a,-b);            }        }    }    return 0;}

  

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.