?http://acm.hdu.edu.cn/showproblem.php?pid=1754
http://acm.zstu.edu.cn:8080/JudgeOnline/showproblem?problem_id=3121
(1)線段樹的基本操作:建樹,查詢,更新。
(2)重新寫一遍時,發現有個比較神奇的超時:假如主函數中少寫了:
for(i=1;i<=m;i++)
案例是可以過的,提交狀態為TLE。。(這種情況與輸入的結構有關,起初還以為是其它函數功能沒寫好)
(3)又寫了一遍,把操作次數的循環寫成了(RE):
for(i=1;i<=n;i++)
?
具體代碼:


#include<stdio.h> #include<algorithm> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; const int N=201000<<2; int n, m; int data[N], Max[N]; void pushback(int rt) {Max[rt]=max(Max[rt<<1], Max[rt<<1|1]); } void build(int l, int r, int rt) {if(l==r){scanf("%d", &data[l]);Max[rt]=data[l];return ;}int m=l+r>>1;build(lson);build(rson);pushback(rt); } int query(int L, int R, int l, int r, int rt) {if(L<=l&&r<=R){return Max[rt];}int m=l+r>>1;int ret=0;if(L<=m) ret=max(ret, query(L, R, lson));if(R>m) ret=max(ret, query(L, R, rson));return ret; } void update(int p, int ne, int l, int r, int rt) {if(l==r){Max[rt]=ne;return ;}int m=l+r>>1;if(p<=m) update(p, ne, lson);else update(p, ne, rson);pushback(rt); } int main() {int i, j;while(scanf("%d%d", &n, &m)!=EOF){build(1, n, 1);for(i=1;i<=m;i++){int a, b;char c;scanf(" %c%d%d", &c, &a, &b);if(c=='Q'){printf("%d\n", query(a, b, 1, n, 1));}else if(c=='U'){update(a, b, 1, n, 1);}}}return 0; }
?