线段树(单点修改)

#include <bits/stdc++.h>

using namespace std;

inline int read() {
    int x = 0,ff = 1; char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') ff = -1;
        ch = getchar();
    } 
    while(isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * ff;
}

inline void write(int x) {
    if(x < 0) putchar('-'),x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

const int INF = 0x3f3f3f3f;
const int MAXN = 5e5 + 100;
const int MAXM = 3e3 + 10;

int n,a[MAXN];
struct Tree {
    int left,right;
    int dat;
}t[MAXN];

void build(int p,int l,int r) {
    t[p].left = l; t[p].right = r;
    if(l == r) { t[p].dat = a[l]; return ; }
    int mid = (l + r) / 2;
    build(p * 2,l,mid);
    build(p * 2 + 1,mid + 1,r);
    t[p].dat = max(t[p * 2].dat,t[p * 2 + 1].dat);
}

void change(int p,int x,int v) {
    if(t[p].left == t[p].right) { t[p].dat = v; return ; }
    int mid = (t[p].left + t[p].right) / 2;
    if(x <= mid) change(p * 2,x,v);
    else change(p * 2 + 1,x,v);
    t[p].dat = max(t[p * 2].dat,t[p * 2 + 1].dat);
}

int ask(int p,int l,int r) {
    if(l <= t[p].left && r >= t[p].right) return t[p].dat;
    int mid = (t[p].left + t[p].right) / 2;
    int val = -INF;
    if(l <= mid) val = max(val,ask(p * 2,l,r));
    if(r > mid) val = max(val,ask(p * 2 + 1,l,r));
    return val;
}

int main() {
    n = read();
    build(1,1,n);
    for(int i = 1; i <= n; ++i) {
        int op;
        op = read();
        if(op == 1) {
            int x,v;
            x = read(); v = read();
            change(1,x,v);
        }
        else {
            int l,r;
            l = read(); r = read();
            write(ask(1,l,r));
            putchar('\n');
        }
    }
    return 0;
}

 

posted @ 2019-03-27 20:47  海边微风起  阅读(187)  评论(0编辑  收藏  举报