标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 45287 Accepted Submission(s): 17773
/*times memy 889ms 8584k by orc */ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int N = 200005; int n,m; struct node{ int lt,rt; int mx; }tree[4 * N]; int a[N]; void build(int l,int r,int v) { tree[v].lt = l; tree[v].rt = r; if(l == r) {tree[v].mx = a[l];return;} int m = (l + r) >> 1; build(l,m,2 * v); build(m + 1,r,2 * v + 1); tree[v].mx = max(tree[2 * v].mx,tree[2 * v + 1].mx); } void update(int v,int k,int val)//直接暴力更新 { if(tree[v].lt == tree[v].rt) { if(tree[v].lt == k) tree[v].mx = val; return; } int m = (tree[v].lt + tree[v].rt) >> 1; if(k <= m) update(v * 2,k,val);//更新左子树 else update(v * 2 + 1,k,val);//更新右子树 tree[v].mx = max(tree[v << 1].mx,tree[(v << 1) + 1].mx); //else tree[v >> 1].mx = max(tree[v].mx,tree[v + 1].mx); } int query(int v,int l,int r) { if(l == tree[v].lt && r == tree[v].rt) return tree[v].mx; int m = (tree[v].lt + tree[v].rt) >> 1; if(r <= m) query(v * 2,l,r); else if(l > m) query(v * 2 + 1,l ,r); else{ return max(query(v * 2,l,m),query(v * 2 + 1,m + 1,r)); } } void solve() { char C; int A ,B; for(int i = 1; i <= n; ++i) cin >> a[i]; build(1,n,1); while(m--) { cin >> C; cin >> A >> B; if(C == ‘Q‘) { cout << query(1,A,B) <<endl; continue; } else update(1,A,B); } } int main() { ios::sync_with_stdio(0); while(cin >> n >> m) solve(); return 0; }
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4436370.html