hdu1166 Classic Line Entry

Source: Internet
Author: User
Tags tidy

Enemy soldiers

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 68824 Accepted Submission (s): 28945


Problem Description C State of the death of a country this period is a military exercise, so the C state spy chief Derek and his men tidy again began busy. Country A has a line of N engineering camps along the coastline, and Derek and tidy are tasked with monitoring the activities of these engineering camps. As a result of the adoption of some advanced monitoring tools, the number of people in each engineering camp, C, is well-informed and the number of personnel in each engineering camp is likely to change, possibly by increasing or reducing the number of staff, but none of them can escape the surveillance of State C.
The CIA wants to study what tactics the enemy is practicing, so Tidy to report to Derek at any time about how many people there are in a succession of engineering camps, such as Derek asking: "Tidy, Report to the 3rd camp to the 10th camp." Tidy is about to start calculating the total number of people in this section and report it immediately. But the number of enemy barracks is constantly changing, and Derek Each asked the paragraph is not the same, so tidy had to every time a camp to count, quickly exhausted, Derek to tidy's calculation speed more and more dissatisfied: "You a dead Fat boy, calculate so slow, I fire you squid!" Tidy thought, "You can count on yourself, it's a tiring job!" I wish you would fire me! " Helpless under, tidy had to call computer experts windbreaker for help, Windbreaker said: "Die fat, you usually do more than a point of ACM and see more Algorithms book, now taste the bitter pill!" Tidy said: "I know the wrong ... "But Windbreaker already hung up the phone." Tidy is very distressed, so he really will collapse, smart Reader, can you write a program to help him finish the work? But if your program is not efficient enough, tidy will still be scolded by Derek.

Input the first line of an integer t, indicating that there is a T group of data.
The first line of each group of data is a positive integer n (n<=50000), which indicates that the enemy has N engineering camps, followed by n positive integers, and the second positive integer AI represents the first AI individual (1<=ai<=50) in the field of the I barracks.
Next, there is one command in each line, with 4 forms of command:
(1) Add I j,i and J are positive integers, indicating that I camp increase J person (J not more than 30)
(2) Sub i J, I and J are positive integers, indicating that the I camp reduced J person (j not more than 30);
(3) Query i J, I and J are positive integers, i<=j, which indicate the total number of persons who have inquired about the first and the J camps;
(4) End indicates that the command ends in each group of data;
Up to 40,000 commands per set of data

Output to group I data, first outputs "case I:" and carriage return,
For each query, output an integer and enter it to indicate the total number of people in the segment you are asking for, which remains within Int.

Sample Input1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 2Add 6 3Query 3 10End

Sample outputcase 1:63,359 topic: give you an array, A[i] represents the number of soldiers in the camp I, now need to implement the following functions, give you any one of the interval [a, b], let you quickly output these barracks a total number of soldiers, There is also the possibility of carrying out a battalion of soldiers to increase or decrease the instructions, instructions very many, 40,000, the number of barracks is also very large, violent do not die, this time to use an efficient data structure-line tree, you can efficiently query, change, this problem with the basic line tree can be passed, yesterday just learned the line segment tree, So the use of the more fixed template, is not very skilled, tomorrow morning to knock again. (Bared teeth) code: #include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace Std;
const int maxn=50000+5;
int A[MAXN],TREE[3*MAXN];
int kase=0;
void build (int p,int l,int R)//contribution function, p for node number, L for left border, R for right border
{
if (l==r) {Tree[p]=a[l];return;}
int mid= (L+R)/2;
Build (P<<1,l,mid);//Build Left sub-tree first
Build ((p<<1) +1,mid+1,r);//post-built right sub-tree
tree[p]=tree[p<<1]+tree[(p<<1) +1];//gives the relationship between the child node and the parent node, depending on the specific problem;
}
void change (int p,int l,int r,int x,int num)//p,l,r meaning ditto, X represents the position of the number to be changed, and num is the changed value
{
if (l==r) {Tree[p]+=num;return;} Find the leaf node, change the value, end the function
int mid= (L+R)/2;//Two-point lookup
if (x<=mid) change (p<<1,l,mid,x,num);//Search Zuozi
else Change ((p<<1) +1,mid+1,r,x,num);
tree[p]=tree[p<<1]+tree[(p<<1) +1];//The value of the leaf node changes, and the value of the root node on it will also change when backtracking
}
the int find (int p,int l,int r,int x,int y)//x represents the left boundary of the lookup interval, y represents the right boundary of the lookup interval, and the Find function is to obtain a value
{
if (x<=l&&r<=y) return tree[p];
int mid= (L+R)/2;
if (Y<=mid) return find (P<<1,l,mid,x,y);
if (X>mid) return find ((p<<1) +1,mid+1,r,x,y);
else return (Find (P<<1,l,mid,x,mid) +find ((p<<1) +1,mid+1,r,mid+1,y));
}
int main ()
{
int t,i;
scanf ("%d", &t);
Char s[10];
int c,d;
while (t--)
{
int n;
scanf ("%d", &n);
for (i=1;i<=n;i++)
scanf ("%d", &a[i]);
Build (1,1,n);
printf ("Case%d:\n", ++kase);
while (1)
{
scanf ("%s", s);
if (!strcmp (S, "End")) break;
scanf ("%d%d", &c,&d);
if (!strcmp (S, "ADD"))
{
Change (1,1,N,C,D);
}
if (!strcmp (S, "Sub"))
{
Change (1,1,n,c,-d);
}
if (!strcmp (S, "Query"))
{
printf ("%d\n", Find (1,1,n,c,d));
}
}
}
return 0;
}

hdu1166 Classic Segment Entry

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.