其他分享
首页 > 其他分享> > P1531 I Hate It

P1531 I Hate It

作者:互联网

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5 + 10;
int n, m;
int w[N];
struct node {
    int l, r;
    int Max;
}tr[N << 2];
void pushup(int u)
{
    tr[u].Max = max(tr[u << 1].Max,  tr[u << 1 | 1].Max);
}
void build(int u, int l, int r)
{
    if (l == r) {tr[u] = {l, r, w[l]}; return;}
    tr[u] = {l, r};
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
}
int query(int u, int l, int r)
{
    if (tr[u].l >= l && tr[u].r <= r)   return tr[u].Max;
    int Max = 0;
    int mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid)   Max = query(u << 1, l, r);
    if (r > mid)    Max = max(Max, query(u << 1 | 1, l, r));
    return Max;
}
void modify(int u, int x, int d)
{
    if (tr[u].l == x && tr[u].r == x) {
        if (tr[u].Max <= d)
            tr[u].Max = d;
        return;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    if (x <= mid)   modify(u << 1, x, d);
    else    modify(u << 1 | 1, x, d);
    pushup(u);
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ )  scanf("%d", &w[i]);
    build(1, 1, n);
    while (m -- ) {
        char op[2];
        scanf("%s", &op);
        if (op[0] == 'Q') {
            int l, r;
            scanf("%d%d", &l, &r);
            printf("%d\n", query(1, l, r));
        }
        else {
            int x, d;
            scanf("%d%d", &x, &d);
            modify(1, x, d);
        }
    }
    return 0;
}

标签:const,P1531,int,Max,tr,long,query,Hate
来源: https://www.cnblogs.com/Angels-of-Death/p/15859904.html