【算法系列学习】线段树 单点覆盖,区间查询最大值 [kuangbin带你飞]专题七 线段树 B - I Hate It
https://vjudge.net/contest/66989#overview
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7 #define lson (i<<1) 8 #define rson (i<<1|1) 9 10 using namespace std; 11 const int maxn=2e5+5; 12 int val[maxn]; 13 14 struct Seg 15 { 16 int l,r,lazy,val; 17 }tree[maxn<<2]; 18 19 void push_up(int i) 20 { 21 tree[i].val=max(tree[lson].val,tree[rson].val); 22 } 23 24 void push_down(int i) 25 { 26 if(tree[i].lazy==-1) 27 { 28 return; 29 } 30 tree[lson].lazy=tree[lson].val=tree[i].lazy; 31 tree[rson].lazy=tree[rson].val=tree[i].lazy; 32 tree[i].lazy=-1; 33 } 34 35 void Build(int l,int r,int i=1) 36 { 37 tree[i].l=l; 38 tree[i].r=r; 39 tree[i].lazy=-1; 40 if(l==r) 41 { 42 tree[i].val=val[l]; 43 return; 44 } 45 push_down(i); 46 int mid=(tree[i].l+tree[i].r)>>1; 47 Build(l,mid,lson); 48 Build(mid+1,r,rson); 49 push_up(i); 50 } 51 52 int Query(int l,int r,int i=1) 53 { 54 if(tree[i].l==l&&tree[i].r==r) 55 { 56 return tree[i].val; 57 } 58 push_down(i); 59 int mid=(tree[i].l+tree[i].r)>>1; 60 if(r<=mid) 61 { 62 return Query(l,r,lson); 63 } 64 else if(l>mid) 65 { 66 return Query(l,r,rson); 67 } 68 else 69 { 70 return max(Query(l,mid,lson),Query(mid+1,r,rson)); 71 } 72 } 73 74 void Setval(int l,int r,int x,int i=1) 75 { 76 if(tree[i].l==l&&tree[i].r==r) 77 { 78 tree[i].lazy=tree[i].val=x; 79 return; 80 } 81 push_down(i); 82 int mid=(tree[i].l+tree[i].r)>>1; 83 if(r<=mid) 84 { 85 Setval(l,r,x,lson); 86 } 87 else if(l>mid) 88 { 89 Setval(l,r,x,rson); 90 } 91 else 92 { 93 Setval(l,mid,x,lson); 94 Setval(mid+1,r,x,rson); 95 } 96 push_up(i); 97 } 98 int main() 99 { 100 int n,m; 101 while(scanf("%d%d",&n,&m)!=EOF) 102 { 103 for(int i=1;i<=n;i++) 104 { 105 scanf("%d",&val[i]); 106 } 107 Build(1,n); 108 char q[20]; 109 int a,b; 110 for(int i=1;i<=m;i++) 111 { 112 scanf("%s%d%d",q,&a,&b); 113 if(q[0]=='Q') 114 { 115 printf("%d\n",Query(a,b)); 116 } 117 else 118 { 119 Setval(a,a,b); 120 } 121 } 122 } 123 return 0; 124 }