HDU 1754 I Hate It<區間最值 單點修改>
阿新 • • 發佈:2017-06-21
des div 分數 流行 esc ble lin problems php
這讓很多學生很反感。
不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。
在每個測試的第一行,有兩個正整數 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。
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
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 76334 Accepted Submission(s):
29413
這讓很多學生很反感。
不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。
Input 本題目包含多組測試,請處理到文件結束。
學生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 Output 5 6 5 9 Hint Huge input,the C function scanf() will work better than cin
Author linle
Source 2007省賽集訓隊練習賽(6)_linle專場
Recommend lcy | We have carefully selected several similar problems for you: 1698 1542 1394 2795 1540 線段樹維護區間最值 單點修改
#include<cstdio> constint N=200005; char a[10]; int p,q; int sum[N*3]; int read(){ int x=0,f=1;char c=getchar(); while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c<=‘9‘&&c>=‘0‘){x=x*10+c-‘0‘;c=getchar();}return x*f;} int max(int x,int y) { if(x>y)return x; return y; } void update(int rt) { sum[rt]=max(sum[rt<<1],sum[rt<<1|1]); } void build(int l,int r,int rt) { if(l==r){ sum[rt]=read(); return ; } int mid=(l+r)>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); update(rt); } void modify(int l,int r,int rt) { if(r==l){ sum[rt]=q;return; } int mid=(l+r)>>1; if(mid>=p) modify(l,mid,rt<<1); else modify(mid+1,r,rt<<1|1); update(rt); } int query(int l,int r,int rt,int nowl,int nowr) { if(nowl<=l&&nowr>=r) { return sum[rt]; } int m=(r+l)>>1; int ans=0; if(nowl<=m) ans=max(ans,query(l,m,rt<<1,nowl,nowr)); if(nowr>m) ans=max(ans,query(m+1,r,rt<<1|1,nowl,nowr)); return ans; } int main() { int n,m; while(scanf("%d%d",&n,&m)==2) { build(1,n,1); while(m--) { scanf("%s",a); p=read();q=read(); if(a[0]==‘U‘)modify(1,n,1); else if(a[0]==‘Q‘)printf("%d\n",query(1,n,1,p,q)); } } return 0; }
HDU 1754 I Hate It<區間最值 單點修改>