线段树
单点更新
View Code
#include <iostream> #include <cstdio> #include <cmath> using namespace std; #define lson l,m,root<<1 #define rson m+1,r,root<<1|1 const int MAX = 250000; int arr[MAX<<2]; int n; void pushup(int root){ arr[root] = max(arr[root<<1],arr[root<<1|1]); } void build(int l, int r, int root){ if (l == r){ scanf("%d",&arr[root]); return; } int m = (l+r)>>1; build(lson); build(rson); pushup(root); } void update(int id, int x, int l, int r, int root){ if (l == r){ arr[root] = x; return; } int m = (l+r)>>1; if (id<=m) update(id,x,lson); else update(id,x,rson); pushup(root); } int query(int x, int y, int l, int r, int root){ if (x<=l && r<=y) return arr[root]; int m = (l+r)>>1; int ans = 0; if (x<=m)ans = max(ans, query(x,y,lson)); if (y>m)ans = max(ans, query(x,y,rson)); return ans; } int main(){ // freopen("in.txt","r",stdin); int i; int t; char ch; int a,b; while (scanf("%d %d",&n,&t)!=EOF){ build(1, n, 1); for (i=0; i<t; i++){ getchar(); scanf("%c %d %d",&ch,&a,&b); if (ch == 'U') update(a,b,1,n,1); else printf("%d\n",query(a,b,1,n,1)); } } return 0; }
posted on 2013-04-05 15:55 shijianupc 阅读(104) 评论(0) 收藏 举报

浙公网安备 33010602011771号