Hdu1166: enemy troops

Source: Internet
Author: User
Tags tidy
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 Input

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

Sample output

Case 1:63359
 

 

This is the line segment tree question I made in my first question. I also typed it out with reference to the code of other gods.

No way. I have no clue for the first time. I will post two types of code below.

 

1. Moderate

It's easy to understand.

 

#include"stdio.h"#include"string.h"struct seg{int l;int r;int n;}T[150011];void build(int l,int r,int k){int mid;if(l==r){T[k].l=l;T[k].r=r;T[k].n=0;return ;}mid=(l+r)/2;T[k].l=l;T[k].r=r;T[k].n=0;build(l,mid,2*k);build(mid+1,r,2*k+1);}void insert(int n,int d,int k){int mid;if(T[k].l==T[k].r&&T[k].l==d){T[k].n+=n;return ;}mid=(T[k].l+T[k].r)>>1;if(d<=mid)insert(n,d,2*k);elseinsert(n,d,2*k+1);T[k].n=T[2*k].n+T[2*k+1].n;}int ans;void search(int l,int r,int k){int mid;//printf("l = %d,r = %d\n",l);if(T[k].l==l&&T[k].r==r){ans+=T[k].n;return ;}mid=(T[k].l+T[k].r)>>1;if(r<=mid)search(l,r,2*k);else if(l>mid)search(l,r,2*k+1);else{search(l,mid,2*k);search(mid+1,r,2*k+1);}}int main(){int Case,T;int n;int i;int temp;char str[11];int a,b;scanf("%d",&T);for(Case=1;Case<=T;Case++){scanf("%d",&n);build(1,n,1);for(i = 1;i<=n;i++){    printf("t.l = %d,t.r = %d\n",T[i].l,T[i].r);}for(i=1;i<=n;i++){scanf("%d",&temp);insert(temp,i,1);}printf("Case %d:\n",Case);while(scanf("%s",str),strcmp(str,"End")){scanf("%d%d",&a,&b);if(strcmp(str,"Add")==0)insert(b,a,1);else if(strcmp(str,"Sub")==0)insert(-b,a,1);else{ans=0;search(a,b,1);printf("%d\n",ans);}}}return 0;}

 

 

Type 2: elegant

It involves many bitwise operations, which makes my understanding of bitwise operations more difficult than the first code.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; const int maxn = 55555; int sum [maxn <2]; // a left-side bitwise value equals to multiplying 2, and an offset of 2 equals to multiplying 2 by the square void pushup (int rt) {sum [RT] = sum [RT <1] + sum [RT <1 | 1]; // This bitwise OR is + 1} void build (int l, int R, int RT) // create a line segment tree {If (L = r) {scanf ("% d", & sum [RT]); return;} int M = (L + r)> 1; // The one-bit bitwise is equal to dividing by 2 B. Uild (L, M, RT <1); Build (m + 1, R, RT <1 | 1); pushup (RT);} void Update (int p, int add, int L, int R, int RT) {If (L = r) {sum [RT] + = add; return ;} int M = (L + r)> 1; if (P <= m) Update (p, add, L, M, RT <1); else Update (p, add, m + 1, R, RT <1 | 1); pushup (RT);} int query (int ll, int RR, int L, int R, int RT) // query the line segment tree {If (LL <= L & RR> = r) return sum [RT]; int M = (L + r)> 1; int ret = 0; If (LL <= m) RET + = query (LL, RR, L, M, RT <1); If (RR> m) RET + = query (LL, RR, m + 1, R, RT <1 | 1); return ret;} int main () {int T, C; char d [10]; scanf ("% d", & T); For (C = 1; C <= T; C ++) {printf ("case % d: \ n ", c); int N; scanf (" % d ", & N); Build (1, n, 1); While (scanf (" % s ", d )! = EOF) {If (d [0] = 'E') break; int X, Y; scanf ("% d", & X, & Y ); if (d [0] = 'q') {int ans = query (X, Y, 1, n, 1); printf ("% d \ n ", ans);} If (d [0] ='s ') Update (x,-y, 1, n, 1 ); if (d [0] = 'A') Update (X, Y, 1, n, 1) ;}} 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.