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++)
具体代码:
View Code
#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; }
