"Segment Tree" Hdu 1754 I Hate It

Source: Internet
Author: User

"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
    • ReferenceCtrl + Q
    • Insert LinkCtrl + L
    • Inserting codeCtrl + K
    • Insert PictureCtrl + G
    • Promote titleCtrl + H
    • Ordered listCtrl + O
    • Unordered listCtrl + U
    • LineCtrl + R
    • RevokeCtrl + Z
    • RedoCtrl + Y

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Segment Tree" Hdu 1754 I Hate It

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.