I Hate It HDU - 1754 (segment tree to find the maximum range)

I Hate It HDU - 1754

Topic links: https://vjudge.net/problem/HDU-1754

topic:

A more popular habit of many schools. The teachers really like to ask, so and so to so and so from among the highest score is.
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 ideas: simple tinkering tree line to modify a single point, will seek the maximum sum to be replaced max, update still press the left and right node node step by step update



// 
// Created by HJYL on 2019/9/4.
//
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include<math.h>
#include<map>
using namespace std;
const int maxn=1e6+7;
int input[maxn];

struct Node{
    int left;
    int right;
    int maxx;
}tree[maxn*4];

void pushup(int root)
{
    tree[root].maxx=max(tree[root<<1].maxx,tree[root<<1|1].maxx);
}

void build(int root,int l,int r)
{
    tree[root].left=l;
    tree[root].right=r;
    if(l==r)
    {
        tree[root].maxx=input[l];
        return;
    }
    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    pushup(root);
}
int maxxx;
void change(int root,int dis,int k)
{
    if(tree[root].left==tree[root].right)
    {
        tree[root].maxx=k;
        return;
    }
    if(tree[root<<1].right>=dis)
        change(root<<1,dis,k);
    else
        change(root<<1|1,dis,k);
    pushup(root);
}

int search(int root,int l,int r)
{
    int maxxx=0;
    if(l<=tree[root].left&&tree[root].right<=r)
    {
        return tree[root].maxx;
    }
    if(l<=tree[root<<1].right)
        maxxx=max(maxxx,search(root<<1,l,r));
    if(r>=tree[root<<1|1].left)
        maxxx=max(maxxx,search(root<<1|1,l,r));
    return maxxx;
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)) {
        for (int i = 1; i <= n; i++)
            scanf("%d", &input[i]);
        build(1, 1, n);
        char ch[10];
        // cout<<"m="<<m<<endl;
        while (m--) {
            scanf("%s", ch);
            //getchar();
            if (ch[0] == 'Q') {
                int ll, rr;
                // cout<<"hhh"<<endl;
                scanf("%d%d", &ll, &rr);
                printf("%d\n", search(1, ll, rr));
            } else {
                int p, q;
                scanf("%d%d", &p, &q);
                change(1, p, q);
            }
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/11462741.html