# Segment tree of a histogram (HDU 1754)

Source: Internet
Author: User

The first line of a line segment tree. If you forget one day, you can make yourself look for memories.

Line Segment tree operation:

Build: Build achievements;

Update: click Modify:

Query: Query

Input

In the first row of each test, there are two positive integers n and M (0 <n <= 200000,0 <m <5000), representing the number of students and the number of operations respectively.
Student ID numbers are separated from 1 to n.
The second row contains N integers, indicating the initial score of the N students. The number of I represents the score of the students whose ID is I.
Next there are m rows. Each line has a character C (only 'q' or 'U'), and two positive integers A and B.
When C is 'Q', it indicates that this is a query operation. It asks the students whose ID ranges from A to B (including a and B) about the highest score.
When C is 'U', it indicates that this is an update operation. You must change the score of students whose ID is A to B.

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

Output

Output the highest score in one row for each query operation.

5

6

5

9

`#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#define INF 0x3fffffff#define bug(a) cout<<a<<"  ----->\n"using namespace std;const int N=200010;int maxt[4*N];int a[N];int n,q;void build(int o,int L,int R){    int m;    if(L==R) { maxt[o]=a[L];return;}    m=(L+R)/2;//bug(o);    build(2*o,L,m);    build(2*o+1,m+1,R);    maxt[o]=max(maxt[2*o],maxt[2*o+1]);}int  query(int ql,int qr,int o,int L,int R){    int m=(L+R)/2,ans=-INF;    if(ql<=L&&R<=qr) return maxt[o];    if(ql<=m) ans=max(ans,query(ql,qr,2*o,L,m));    if(m<qr) ans=max(ans,query(ql,qr,2*o+1,m+1,R));    return ans;}void update(int p,int v,int o,int L,int R){    if(L==R){maxt[o]=v;return;}    int m=(L+R)>>1;    if(p<=m) update(p,v,2*o,L,m);    else update(p,v,2*o+1,m+1,R);    maxt[o]=max(maxt[2*o],maxt[2*o+1]);}int main(){    char c[2];    int d,b;    while(scanf("%d%d",&n,&q)!=EOF)    {        for(int i=1;i<=n;i++)            scanf("%d",a+i);        //bug(1);        build(1,1,n);        for(int i=1;i<=q;i++)        {             scanf("%s%d%d",c,&d,&b);             if(c[0]=='Q')                 printf("%d\n",query(d,b,1,1,n));             else                 update(d,b,1,1,n);        }    }    return 0;}`

Related Keywords:

### 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.