Explain the tree line of https://www.bilibili.com/video/av47331849?from=search&seid=16724415639232651392
A - enemy soldiers lineup
topic:
CIA to study enemy tactics exactly what exercises, so Derek Tidy to keep the report a certain period of continuous engineering camp a total of how many people, such as Derek asked: "Tidy, immediately report the first three camps to many people of a total of 10 camps ! "Tidy will immediately begin to calculate the total number of this paragraph and report. But the number of enemy camps often change, and Derek asked each segment is different, so every time a Tidy had to go to a number of camps, soon exhausted, Derek to calculate the speed of the Tidy increasingly dissatisfied: "! you a dead fat boy, considered so slow, I fire you" Tidy thought: "you do the math yourself, which is really a tiring job I wish you fire me too!! "In desperation, Tidy had to call a computer expert to help Windbreaker, Windbreaker, said:"! dead fat boy, do you usually called multi-point acm title and see multi-point arithmetic book, now tasted the bitter fruit of it "Tidy said:" I admitted the mistake ... "but Windbreaker has hung up the phone. Tidy very upset, so he really will count crash, the intelligent reader, you can write a program to help him finish the job? But if your program efficiency is not high enough, Tidy will still be scolded Derek's.
Input a first line integer T, T set of data expressed.
Data of the first line of each a positive integer N (N <= 50000), there are N represents the enemy camps engineer, the next positive integers with N, has the i ai positive integer representing the i-th ai start engineering camp personal (1 <= ai <= 50 ).
Next, each row having a command, command has four forms:
(. 1) ij of the Add, i, and j is a positive integer, denotes the i th camp increase personal j (j is not more than 30)
(2) Sub ij of, i and j It is a positive integer, denotes the i th camps reduce j individuals (j does not exceed 30);
(. 3) Query ij of, i and j are positive integers, i <= j, presents to ask the total number of i-th to j-th camps;
(4) end indicates the end, the last command that appears in each set of data;
up to 40,000 commands each data
output of the i-th group of data, first outputs "Case i:" and enter,
for each query Query output the total number of integer and a carriage return, indicating an inquiry segment, this number remains 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
. 1 Case: . 6 33 is 59
that Italy:
questions used in the query, add, aub main application for the segment tree
#include<bits/stdc++.h> using namespace std; int q[100000]; int su; //创建线段树 struct node { int l,r,sum; }t[140000]; void build(int l,int r,int num) { t[num].l=l; t[num].r=r; if(l==r) t[num].sum=q[l]; else { int mid=(l+r)/2;// separate Build (L, MID, 2 * NUM); // left Build (MID + . 1 , R & lt, 2 * + NUM . 1 ); // the right T [NUM] .sum T = [ 2 * NUM] + T .sum [ 2 * + NUM . 1 ] .sum; // node and a } } void Query ( int A, int B, int K) { IF (A <= T [K] .L && B> = T [K] .r) SU = + T [K] .sum; the else { int MID = (T [K] .L + T [K] .r) /2 ; IF (A> = MID + . 1 ) // interval total on the right Query (A, B, 2 * K + . 1 ); the else IF (B <= MID) // interval total on the left Query (A, B, 2 * K); the else { Query (A, B, K * 2 ); Query (A, B, K * 2 + . 1 ); } } } void the Add ( int A, int B, int K) { T [K] .sum= + B; IF (T [K] .L == T [K] .r) return ; int MID = (T [K] .L + T [K] .r) / 2 ; IF (A> MID) the Add (a, B, K * 2 + . 1 ); // complete binary tree, the node is the left child node of the node * 2, for the right child. 1 + 2 * node IF (a <= MID) the Add (a, B, K * 2 ); } void Sub ( int A, int B, int K) { T [K] .sum - = B; IF (T [K] .L == T [K] .r) return; int mid=(t[k].l+t[k].r)/2; if(a>mid) Sub(a,b,k*2+1); if(a<=mid) Sub(a,b,k*2); } int main() { char s[20]; int t,n,a,b,x=1; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) cin>>q[i]; build(1,n,1); printf("Case %d:\n",x++); while(1) { scanf("%s",s); if(s[0]=='E') break; if(s[0]=='A') { scanf("%d%d",&a,&b); Add(a,b,1); } if(s[0]=='S') { scanf("%d%d",&a,&b); Sub(a,b,1); } if(s[0]=='Q') { scanf("%d%d",&a,&b); su=0; Query(a,b,1); printf("%d\n",su); } } } return 0; }
B - I Hate It
This allows many students are disgusted.
Whether you like it or not, now you need to do is, it is in accordance with the requirements of the teacher, to write a program to simulate the teacher asked. Of course, teachers sometimes need to update certain students achievements.
Input this title has multiple sets of test, to deal with the end of the file.
In the first line of each test, there are two positive integers N and M (0 <N <= 200000,0 <M <5000), represents the number of operations and the number of students.
Student ID numbers are compiled from 1 to N.
The second line contains N integers, representing the N initial student grades, where the i-th student ID is representative of the results i.
The next M rows. Each line has a character C (just take 'Q' or 'U'), and two positive integers A, B.
When C is the 'Q' time, that this is a query operation, it queries the ID from A to B (including A, B) among the students, the highest score is.
When C is the 'U' time, that this is an update operation, the ID required to change grades A student B.
Output For each query operation, which outputs the highest score in a row. Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
The problem is almost a template tree line problem, with the structure to do some good
#include<bits/stdc++.h> using namespace std; struct node { int l,r,mid; int maxn; }t[4*200000+10]; int q[2000010]; //jianshu int build(int a,int b,int k) { if(a==b) { t[k].l=t[k].r=a; t[k].maxn=q[a]; return t[k].maxn; } t[k].l=a; t[k].r=b; t[k].mid=(a+b)/2; t[k].maxn=max(build(a,t[k].mid,2*k),build(t[k].mid+1,b,2*k+1)); return t[k].maxn; } //xunwen int query(int a,int b,int k) { if(t[k].l==a&&t[k].r==b) return t[k].maxn; else if(a>t[k].mid) return query(a,b,2*k+1); else if(b<=t[k].mid) return query(a,b,2*k); else return max(query(a,t[k].mid,2*k),query(t[k].mid+1,b,2*k+1)); } //gengxin void update(int a,int b,int k) { if(t[k].l==a&&t[k].r==a) { t[k].maxn=b; return; } if(a>t[k].mid) { update(a,b,2*k+1); t[k].maxn=max(t[2*k].maxn,t[2*k+1].maxn); } else { update(a,b,2*k); t[k].maxn=max(t[2*k].maxn,t[2*k+1].maxn); } } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++) scanf("%d",&q[i]); build(1,n,1); while(m--) { int a,b; char s[2]; scanf("%s",s); scanf("%d%d",&a,&b); if(s[0]=='Q') printf("%d\n",query(a,b,1)); if(s[0]=='U') update(a,b,1); } } return 0; }