Luo Gu P1531 I Hate It solution to a problem

Topic background

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.

Title Description

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 Format

The first row, 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, if the current A student's score is lower than B, B to put the ID change for students A grades, or do not change.

Output Format

For each query operation, which outputs the highest score in a row

Sample input and output

Input # 1 replication

5 6

1 2 3 4 5

Q 1 5

In 3 6

Q 3 4

Q 4 5

In 2 9

Q 1 5

Copy Output # 1

5

6

5

9

Resolution:

A single point of modification, the query interval.

Significant segment tree.

#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <map>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define re register
#define Max 400100
#define gc getchar
#define lson x<<1
#define rson x<<1|1
int max(int a, int b) {return a > b ? a : b;}
struct HATE {
    int l, r, max;
}t[Max << 1];
int m, n, pts[Max >> 1];
void up(int x) {t[x].max = max(t[lson].max, t[rson].max);}
void build(int l, int r, int x) {
    t[x].l = l; t[x].r = r;
    if(l == r) {
        t[x].max = pts[l];
        return ;
    }
    int mid = l + r >> 1;
    build(l, mid, lson); build(mid+1, r, rson);
    up(x);
}
void data(int f, int x, int k) {
    if(t[x].l == f && t[x].r == f) {
        if(t[x].max < k) t[x].max = k;
        return ;
    }
    int mid = t[x].l + t[x].r >> 1;
    if(f <= mid) data(f,lson,k);
    if(f > mid) data(f,rson,k);
    up(x);
}
int Ask(int l, int r, int x) {
    if(t[x].l >= l && t[x].r <= r)
        return t[x].max;
    int mid = t[x].l + t[x].r >> 1;
    int ans = - 9999999;
    if(l <= mid) ans = max(ans, Ask(l,r,lson));
    if(r > mid) ans = max(ans, Ask(l,r,rson));
    return ans;
}
int main() {
    scanf("%d%d",&n,&m);
    for(re int i = 1; i <= n; ++ i) scanf("%d", &pts[i]);
    build(1, n, 1); int l, r; char opt;
    for(re int i = 1; i <= m; ++ i) {
        std :: cin >> opt;
        scanf("%d %d",&l,&r);
        if(opt == 'U') data(l,1,r);
        else printf("%d\n",Ask(l,r,1));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/handsomegodzilla/p/11622333.html
Recommended