"Segment Tree" Hdu 1754 I Hate it topic Links: Hdu 1754 I Hate it topics to the effect
N A student's initial score is known, the Operation M, each time the first student will either update the results, or find the interval "x, y" the maximum score.
It is clear that this is a line segment tree, point modification, interval query, the author of the third line segment tree, completely own knock, direct ac~ (≧▽≦)/~ la la La.
If you simply look for the interval maximum, the time complexity O (N), and the Segment Tree O (Logn), when the number of queries is very long, obviously the latter more efficient!
Talk about the idea.
- Dot Tree Point Modification "point Update": Directly update the leaf nodes into the input data, against the "enemy army" the problem increases or decreases, slightly modified can
- Interval query for segment tree "interval Maximum": In the build, update process, pay attention to maintaining the num of each node, its value depends on the left child segment minimum and the right child segment minimum value of the smaller, query () when the scope of the discussion range of direct recursion can
Reference Code
/*author: hacker_vision*/#include <bits/stdc++.h>Using namespace Std;constint_max =2e5 +Ten;intNm, A[_max],x,y; chars[ -];struct segtree{int LC, RC;intNum;} segtree[_max<<2];void Build (intRootintLintR) {//Establish the line segment tree O (logn) segtree[root].LC=l; Segtree[root].rc=r;if(L==R) {segtree[root].num = a[l];return; }intMid = (L + R) >>1; Build (root<<1, L,mid);//Build left subtree Build (root<<1|1, mid+1, R);//Build right subtree Segtree[root].num=max (segtree[root<<1].num,segtree[root<<1|1].num);//Update parent Node}void Update (intRootint POS,intData) {//Point modification O (logn)if(Segtree[root].LC==POS&&segtree[root].rc==POS) {segtree[root].num = data;//Direct updatereturn; }intMID = (Segtree[root].LC+ segtree[root].rc) >>1;if(POS<= mid) Update (root<<1,POS, data);ElseUpdate (root<<1|1,POS, data); Segtree[root].num=max (segtree[root<<1].num,segtree[root<<1|1].num);//Backtracking Update parent Node}intQueryintRootintLintR) {//Interval query (min. query value)if(Segtree[root].LC= = L&&segtree[root].rc==r)returnSegtree[root].num;intMID = (Segtree[root].LC+ segtree[root].rc) >>1;if(R <= mid)returnQuery (root<<1, l,r);Else if(Mid < L)returnQuery (root<<1|1, l,r);Else returnMax (Query (root<<1, L,mid), query (root<<1|1, mid+1, R));}intMain () {//Freopen ("Input.txt","R", stdin); while(SCANF ("%d%d",&n,&m)==2){ for(inti =1; I <= N; + + i) scanf ("%d", a+i); Build1,1, n); for(inti =0; I <m; + + i) {scanf ("%s%d%d",s,&x,&y);if(s[0]==' U ') Update (1,x,y);Else printf("%d\ n", Query (1,x,y)); } }return 0;}
- Bold
Ctrl + B
- Italic Body
Ctrl + I
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Segment Tree" Hdu 1754 I Hate It