I Hate It HDU
阿新 • • 發佈:2018-12-11
I Hate It
很多學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。 這讓很多學生很反感。 不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程式,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。
Input
本題目包含多組測試,請處理到檔案結束。 在每個測試的第一行,有兩個正整數 N 和 M ( 0<N<=200000,0<M<5000 ),分別代表學生的數目和操作的數目。 學生ID編號分別從1編到N。 第二行包含N個整數,代表這N個學生的初始成績,其中第i個數代表ID為i的學生的成績。 接下來有M行。每一行有一個字元 C (只取'Q'或'U') ,和兩個正整數A,B。 當C為'Q'的時候,表示這是一條詢問操作,它詢問ID從A到B(包括A,B)的學生當中,成績最高的是多少。 當C為'U'的時候,表示這是一條更新操作,要求把ID為A的學生的成績更改為B。
Output
對於每一次詢問操作,在一行裡面輸出最高成績。
Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
思路:線段樹單點更新區間最值問題
程式碼:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 int f[200005<<2]; void pushup(int rt) { if(f[rt<<1]>=f[rt<<1|1]) f[rt]=f[rt<<1]; else f[rt]=f[rt<<1|1]; } void built(int l,int r,int rt) //建樹 { if(l==r) { scanf("%d",&f[rt]); return ; } int m=(l+r)>>1; built(lson); built(rson); pushup(rt); } void updata(int k,int x,int l,int r,int rt) { if(l==r) { f[rt]=x; return ; } int m=(l+r)>>1; if(k<=m) updata(k,x,lson); else updata(k,x,rson); pushup(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) return f[rt]; int ans=0; int m=(l+r)>>1; if(L<=m) ans=max(ans,query(L,R,lson)); if(R>m) ans=max(ans,query(L,R,rson)); return ans; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { built(1,n,1); char s; int a,b; while(m--) { scanf(" %c%d%d",&s,&a,&b); if(s=='U') updata(a,b,1,n,1); else printf("%d\n",query(a,b,1,n,1)); } } return 0; }