http://acm.hdu.edu.cn/showproblem.php?pid=1754
讨论里面的一个数据没有过,结果就wrong了两次,很是悲剧啊,
5 3
1 2 3 4 5
Q 1 5
U 5 0
Q 1 5
就是这组该死的数据,以后还是要多测试两组数据的。
#include <stdio.h> #define MAX 200010 typedef struct _node { int left; int right; int high; }node; node no[MAX*3]; int n, m; int t, grade[MAX]; int maxh(int a, int b) { return a > b ? a : b; } void initTree(int l, int r, int i) { no[i].left = l; no[i].right = r; if(l==r){ no[i].high = grade[t ++];//这里有点儿问题,当为grade[l]时有组测试数据错误 return ; } int mid = (l+r)/2; initTree(l, mid, i*2); initTree(mid+1, r, i*2+1); no[i].high = maxh(no[i*2].high, no[i*2+1].high); } //修改 void modify(int l, int r, int i, int tmp, int value) { if(no[i].left==no[i].right){ no[i].high = value; return ; } int mid = (no[i].left+no[i].right)/2; if(tmp <= mid){ modify(l, r, i*2, tmp, value); } else { modify(l, r, i*2+1, tmp, value); } no[i].high = maxh(no[i*2].high, no[i*2+1].high); } int tmpgd; //查询 void query(int start, int end, int i) { if(no[i].left==start&&no[i].right==end){ if(tmpgd<no[i].high)tmpgd = no[i].high; return ; } int mid = (no[i].left+no[i].right)/2; if(end <= mid){ query(start, end, i*2); } else if(start > mid){ query(start, end, i*2+1); } else { query(start, mid, i*2); query(mid+1, end, i*2+1); } } int main() { // freopen("input.txt","r",stdin); int i; char c[2]; int a, b; while(scanf("%d%d", &n, &m)!=EOF){ for(i = 0; i < n; i ++){ scanf("%d", &grade[i]); } t = 0; initTree(1, n, 1); while(m--){ scanf("%s%d%d", c, &a, &b); if(c[0] == 'U')modify(1, n, 1, a, b); else { tmpgd = 0; query(a, b, 1); printf("%d\n", tmpgd); } } } return 0; }