Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33726 Accepted Submission(s): 13266
1 #include <stdio.h>
2 #define MAXSIZE 200000
3 struct Node{ 4 int left,right; 5 int n; 6 }; 7 Node a[MAXSIZE*3+1]; 8 void Init(Node a[],int L,int R,int d) //初始化线段树
9 { 10 if(L==R){ //当前节点没有儿子节点,即递归到叶子节点。递归出口
11 a[d].left = L; 12 a[d].right = R; 13 a[d].n = 0; 14 return ; 15 } 16
17 int mid = (L+R)/2; //初始化当前节点
18 a[d].left = L; 19 a[d].right = R; 20 a[d].n = 0; 21
22 Init(a,L,mid,d*2); //递归初始化当前节点的儿子节点
23 Init(a,mid+1,R,d*2+1); 24
25 } 26 void Update(Node a[],int L,int R,int d,int x) //对区间[L,R]插入值x,从节点d开始更新。
27 { 28 if(L==a[d].left && R==a[d].right){ //插入的区间匹配,则直接修改该区间值
29 a[d].n = x; 30 return ; 31 } 32 if(x>a[d].n) //沿路节点比该值小的都重新赋值
33 a[d].n = x; 34 int mid = (a[d].left + a[d].right)/2; 35 if(R<=mid){ //中点在右边界R的右边,则应该插入到左儿子
36 Update(a,L,R,d*2,x); 37 } 38 else if(mid<L){ //中点在左边界L的左边,则应该插入到右儿子
39 Update(a,L,R,d*2+1,x); 40 } 41 else { //否则,中点在待插入区间的中间
42 Update(a,L,mid,d*2,x); 43 Update(a,mid+1,R,d*2+1,x); 44 } 45 } 46 int Query(Node a[],int L,int R,int d) //查询区间[L,R]的值,从节点d开始查询
47 { 48 if(L==a[d].left && R==a[d].right){ //查找到区间,则直接返回该区间值
49 return a[d].n; 50 } 51 int mid = (a[d].left + a[d].right)/2; 52 if(R<=mid){ //中点在右边界R的右边,则应该查询左儿子
53 return Query(a,L,R,d*2); 54 } 55 else if(mid<L){ //中点在左边界L的左边,则应该查询右儿子
56 return Query(a,L,R,d*2+1); 57 } 58 else { //中点在待查询区间的中间,左右孩子都查找
59 int A = Query(a,L,mid,d*2); 60 int B = Query(a,mid+1,R,d*2+1); 61 return A>B?A:B; 62 } 63 } 64 int main() 65 { 66 int i,n,m,A,B; 67 char c; 68 while(scanf("%d%d",&n,&m)!=EOF){ 69 Init(a,1,n,1); //初始化
70
71 for(i=1;i<=n;i++){ //输入
72 int t; 73 scanf("%d",&t); 74 Update(a,i,i,1,t); 75 } 76
77 for(i=1;i<=m;i++){ //m次查询
78 scanf("%*"); //读掉空格
79 scanf("%c%d%d",&c,&A,&B); 80 switch(c){ 81 case 'Q': 82 printf("%d\n",Query(a,A,B,1)); 83 break; 84 case 'U': 85 Update(a,A,A,1,B); 86 break; 87 default:break; 88 } 89 } 90 } 91 return 0; 92 }
Freecode : www.cnblogs.com/yym2013