Hdoj 1754 I Hate It

Source: Internet
Author: User

Title:
Problem Description
Many schools are popular for a comparative habit. Teachers like to ask, from XXX to XXX, the highest score is how much.
This makes many students very disgusted.

Whether you like it or not, now you need to do is to follow the teacher's request, write a program, mock teacher's inquiry. Of course, teachers sometimes need to update a student's grades.

Input
This topic contains multiple sets of tests, please handle to the end of the file.
On the first line 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 numbered from 1 to N, respectively.
The second line contains n integers representing the initial scores of the N students, of which the number of I represents the student's score for ID i.
Then there's M-line. Each line has a character C (only ' Q ' or ' U '), and two positive integers, A/b.
When C is ' Q ', it indicates that this is a query operation, which asks for the highest number of students whose IDs are from a to B (including A, a).
When C is ' U ', it indicates that this is an update operation that requires the student with ID A to change the grade to B.

Output
For each query operation, output the highest score in one line.

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

Hint
Huge input,the C function scanf () would work better than CIN

#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <algorithm> using
namespace Std;
const int maxn=200001;
        int segtree[4*maxn+1];//save segment Tree int mark[maxn];//mark as input data void build (int node,int beg,int e)//contribute to the process {if (beg==e)
    Segtree[node]=mark[beg];
        else {build (2*node,beg, (beg+e)/2);
        Build (2*node+1, (beg+e)/2+1,e);
    Segtree[node]=max (Segtree[2*node+1],segtree[2*node]);
    }} int query (int node,int beg,int e,int le,int ri)//interval query Beg and e are the ranges of the original input data respectively, and Le and RI are the segment tree interval {int p1,p2; if (le>e| |
    Ri<beg) return 0;
    if (Beg>=le&&e<=ri) return Segtree[node];
    P1=query (2*node,beg, (beg+e)/2,le,ri);
    P2=query (2*node+1, (beg+e)/2+1,e,le,ri);
Return Max (P1,P2);
        } void Update (int node,int beg,int e,int ind,int a)//Add the value on Mark[ind] a {if (beg==e) {segtree[node]=a;
    Return
    } int m= (beg+e)/2;
 if (ind<=m) update (NODE*2,BEG,M,IND,A);   else update (NODE*2+1,M+1,E,IND,A);
    Segtree[node]=max (segtree[node*2],segtree[node*2+1]);//Minimum value} int main () {//Ios::sync_with_stdio (FALSE);
    int n,m;
        while (~SCANF ("%d%d", &n,&m)) {for (int i=1;i<=n;i++) scanf ("%d", &mark[i]);
        Build (1,1,n);
            for (int i=1;i<=m;i++) {char c[2];
            int A, B;
            scanf ("%s%d%d", c,&a,&b);
            if (c[0]== ' Q ') printf ("%d\n", Query (1,1,n,a,b));
        else update (1,1,N,A,B);
}} return 0;
 }

Answer:
Template title, line tree single point modification can ~

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.