1. 程式人生 > >7_22_N題 I Hate It(線段樹)

7_22_N題 I Hate It(線段樹)

7_22_N題 I Hate It

題意

給出一些學生的成績,老師會詢問,某一段學生中最高的成績,或修改某同學的成績。

思路

求區間最大值和單點更新,線段樹

#

#include <bits/stdc++.h>

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn = 2e5+10;
using namespace std;


char str[10];

/*------------------------*/
int Tree[maxn*3];
//更新父節點
inline void pushPlus(int rt){ Tree[rt] = max(Tree[rt <<1], Tree[rt<<1|1]); } //建樹 void buildT(int l,int r,int rt){ if(l == r){ scanf("%d",Tree+rt); return; } int mid = (l+r)>>1; buildT(lson); buildT(rson); pushPlus(rt); } int que(int NL,int
NR,int l,int r,int rt){ if(NL <= l && r <= NR) return Tree[rt]; int mid = (l+r)>>1; int ret = 0; if(NL <= mid) ret = max(ret, que(NL,NR,lson)); if(NR > mid) ret = max(ret,que(NL,NR,rson)); return ret; } void update(int p,int
c,int l,int r,int rt){ if(l == r){ Tree[rt] = c; return ; } int mid= (l+r) >> 1; if(p <= mid) update(p,c,lson); else update(p,c,rson); pushPlus(rt); } /*--------------------------------*/ int main(){ int T,n,a,b; while(~scanf("%d %d",&n,&T)){ buildT(1,n,1); while(T --){ scanf("%s",str); scanf("%d %d", &a, &b); if(str[0] == 'U') update(a,b,1,n,1); else if(str[0] == 'Q') printf("%d\n",que(a,b,1,n,1)); } } return 0; }