1. 程式人生 > >kuangbin專題七 HDU1754 I Hate It (單點修改維護最大值)

kuangbin專題七 HDU1754 I Hate It (單點修改維護最大值)

cond color include 專題 一次 span 修改 信息 \n

很多學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。
這讓很多學生很反感。

不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。

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

經過兩道題的磨練,終於可以手搓了!細節需要註意一下

 1 #include <iostream>
 2 #include <stdio.h>
 3
#include <math.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <algorithm> 12 #include <sstream> 13 #include <stack> 14
using namespace std; 15 #define FO freopen("in.txt","r",stdin); 16 #define rep(i,a,n) for (int i=a;i<n;i++) 17 #define per(i,a,n) for (int i=n-1;i>=a;i--) 18 #define pb push_back 19 #define mp make_pair 20 #define all(x) (x).begin(),(x).end() 21 #define fi first 22 #define se second 23 #define SZ(x) ((int)(x).size()) 24 #define debug(x) cout << "&&" << x << "&&" << endl; 25 #define lowbit(x) (x&-x) 26 #define mem(a,b) memset(a, b, sizeof(a)); 27 typedef vector<int> VI; 28 typedef long long ll; 29 typedef pair<int,int> PII; 30 const ll mod=1000000007; 31 const int inf = 0x3f3f3f3f; 32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} 33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} 34 //head 35 36 const int N=200010; 37 int n,m,a[N<<2],maxx[N<<2],pos,val,ans; 38 39 void Pushup(int rt) { 40 maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]); 41 } 42 43 void build(int rt,int L,int R) { 44 if(L==R) { 45 scanf("%d",&a[L]);//這裏必須是L 46 maxx[rt]=a[L];//這裏是rt的維護信息 47 return; 48 } 49 int mid=(L+R)>>1; 50 build(rt<<1,L,mid); 51 build(rt<<1|1,mid+1,R); 52 Pushup(rt); 53 } 54 55 void Updata(int rt,int L,int R,int pos,int val) { 56 if(L==R) {//修改 57 a[rt]=val; 58 maxx[rt]=a[rt]; 59 return; 60 } 61 int mid=(L+R)>>1; 62 if(pos<=mid) Updata(rt<<1,L,mid,pos,val); 63 else Updata(rt<<1|1,mid+1,R,pos,val); 64 Pushup(rt); 65 } 66 67 void Query(int rt,int L,int R,int l,int r) {//維護最大值 68 if(L>=l&&R<=r) { 69 ans=max(ans,maxx[rt]); 70 return; 71 } 72 int mid=(L+R)>>1; 73 if(l<=mid) Query(rt<<1,L,mid,l,r); 74 if(r>mid) Query(rt<<1|1,mid+1,R,l,r); 75 } 76 77 int main() { 78 while(~scanf("%d%d",&n,&m)) { 79 build(1,1,n); 80 char s[5]; 81 while(m--) { 82 ans=-1; 83 scanf("%s%d%d",s,&pos,&val); 84 if(s[0]==Q) { 85 Query(1,1,n,pos,val); 86 printf("%d\n",ans); 87 } else Updata(1,1,n,pos,val); 88 } 89 } 90 }





kuangbin專題七 HDU1754 I Hate It (單點修改維護最大值)