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
5 6 5 9HintHuge input,the C function scanf() will work better than cin
#include <stdio.h> #include <string.h> #define max(a,b) ((a)>(b)?(a):(b)) const int N = 200005; int n, m, bit[N], num[N]; void add(int x, int v) { num[x] = v; while (x <= n) { bit[x] = max(bit[x], v); x += (x&(-x)); } } int get(int l, int r) { int ans = num[r]; while (l != r) { for (r -= 1; r - (r&(-r)) >= l; r -= (r&(-r))) ans = max(ans, bit[r]); ans = max(ans, num[r]); } return ans; } void init() { memset(bit, 0, sizeof(bit)); for (int i = 1; i <= n; i++) { scanf("%d", &num[i]); add(i, num[i]); } } void solve() { char s[10]; int a, b; while (m--) { scanf("%s%d%d", s, &a, &b); if (s[0] == ‘U‘) add(a, b); else printf("%d\n", get(a, b)); } } int main() { while (~scanf("%d%d", &n, &m)) { init(); solve(); } return 0; }
原文:http://blog.csdn.net/accelerator_/article/details/18677155