1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include<iostream> #include<algorithm> using namespace std; const int maxn = 2000010; int n, m, l, r; int a[maxn]; char od[5]; struct node { int l, r; int dat, add; #define l(x) tree[x].l #define r(x) tree[x].r #define dat(x) tree[x].dat }tree[maxn << 2]; inline int Read() { int x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void update(int p) {dat(p) = max(dat(p<<1), dat(p<<1|1));} void build(int p, int l, int r) { l(p) = l; r(p) = r; if (l == r) {dat(p) = a[l];return;} int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); update(p); } void init() { for (int i=1;i<=n;++i) a[i] = Read(); build(1, 1, n); } void change(int p, int x, int v) { if (l(p) == r(p)) { dat(p) = v; return; } int mid = (l(p) + r(p)) >> 1; if (x <= mid) change(p << 1, x, v); if (x > mid) change(p << 1 | 1, x, v); update(p); } int ask(int p, int l, int r) { if (l <= l(p) && r(p) <= r) return dat(p); int mid = (l(p) + r(p)) >> 1; int res = 0; if (l <= mid) res = max(res, ask(p << 1, l, r)); if (r > mid) res = max(res, ask(p << 1 | 1, l, r)); return res; } int main() { while(scanf("%d%d", &n,&m) != EOF) { init(); for (int i=1;i<=m;++i) { scanf("%s", od); l = Read(); r = Read(); if (od[0] == 'Q') printf("%d\n", ask(1, l, r)); else change(1, l, r); } } return 0; }
|