I Hate ItTime Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
Hint
Huge input,the C function scanf() will work better th
#include <cstdio> #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <cmath> using namespace std; char com[100]; int a[200010]; int n; struct Node{ int l, r, v; }tree[800050]; void Build(int pos, int l, int r){ tree[pos].l = l; tree[pos].r = r; if(l == r){ tree[pos].v = a[tree[pos].l]; return ; } int mid = (l + r) / 2; Build(pos * 2, l, mid); Build(pos * 2 + 1, mid + 1, r); tree[pos].v = max(tree[pos * 2].v, tree[pos * 2 + 1].v); } void Update(int pos, int idx, int val){ if(tree[pos].l == tree[pos].r){ tree[pos].v = val; return ; } int mid = (tree[pos].l + tree[pos].r) / 2; if(idx <= mid) Update(pos * 2, idx, val); else Update(pos * 2 + 1, idx, val); tree[pos].v = max(tree[pos * 2].v, tree[pos * 2 + 1].v); } int Query(int pos, int l, int r){ if(l <= tree[pos].l && tree[pos].r <= r){ return tree[pos].v; } int ret = 0, mid = (tree[pos].l + tree[pos].r) / 2; if(l <= mid){ ret =max(ret, Query(pos * 2, l, r)); } if(r > mid){ ret =max( ret, Query(pos * 2 + 1, l, r)); } return ret; } int main(){ int t, m; int x, y, i, j, k; int cnt = 0; while(~scanf("%d %d",&n ,&m)){ cnt ++; for(i = 1; i <= n; i++){ scanf("%d", &a[i]); } Build(1, 1, n); for(i = 0; i < m; i++){ scanf ("%s", com); if(com[0] == 'Q'){ //cout << com[0]; scanf("%d%d", &x, &y); printf("%d\n", Query(1, x, y)); } else if(com[0] == 'U'){ //cout << com[0]; scanf("%d%d", &x, &y); Update(1, x, y); } } } return 0; }