日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

HDU 1754 I Hate It 线段树

發(fā)布時間:2023/12/20 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 1754 I Hate It 线段树 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

I Hate It

Description

很多學(xué)校流行一種比較的習(xí)慣。老師們很喜歡詢問,從某某到某某當(dāng)中,分?jǐn)?shù)最高的是多少。
這讓很多學(xué)生很反感。

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

Input

本題目包含多組測試,請?zhí)幚淼轿募Y(jié)束。
在每個測試的第一行,有兩個正整數(shù) N 和 M ( 0<N<=200000,0<M<5000 ),分別代表學(xué)生的數(shù)目和操作的數(shù)目。
學(xué)生ID編號分別從1編到N。
第二行包含N個整數(shù),代表這N個學(xué)生的初始成績,其中第i個數(shù)代表ID為i的學(xué)生的成績。
接下來有M行。每一行有一個字符 C (只取'Q'或'U') ,和兩個正整數(shù)A,B。
當(dāng)C為'Q'的時候,表示這是一條詢問操作,它詢問ID從A到B(包括A,B)的學(xué)生當(dāng)中,成績最高的是多少。
當(dāng)C為'U'的時候,表示這是一條更新操作,要求把ID為A的學(xué)生的成績更改為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

題意

給定一個區(qū)間,有單點修改和區(qū)間查詢操作

題解

線段樹版題/樹狀數(shù)組版題

改天用樹狀數(shù)組實現(xiàn)一下

代碼

1 //HDU-1754 copyright:scidylanpno 2 #include<bits/stdc++.h> 3 using namespace std; 4 5 #define lson l,m,rt<<1 6 #define rson m+1,r,rt<<1|1 7 8 const int MAXN = 200000 +10; 9 int Tree[MAXN<<2]; 10 11 void PushUp(int rt) 12 { 13 Tree[rt]=max(Tree[rt<<1],Tree[rt<<1|1]); 14 } 15 void Build(int l,int r,int rt) 16 { 17 if(l==r) 18 { 19 scanf("%d",Tree+rt); 20 return; 21 } 22 int m=(l+r)>>1; 23 Build(lson); 24 Build(rson); 25 PushUp(rt); 26 } 27 28 void Update(int pos,int x,int l,int r,int rt) 29 { 30 if(l==r) 31 { 32 Tree[rt]=x; 33 return; 34 } 35 int m=(l+r)>>1; 36 if(pos<=m) Update(pos,x,lson); 37 else Update(pos,x,rson); 38 PushUp(rt); 39 } 40 41 int Query(int L,int R,int l,int r,int rt) 42 { 43 if(L<=l&&r<=R) 44 { 45 return Tree[rt]; 46 } 47 int m=(l+r)>>1; 48 int ans=-1; 49 if(L<=m) ans=max(ans,Query(L,R,lson)); 50 if(R>m) ans=max(ans,Query(L,R,rson)); 51 return ans; 52 } 53 54 void Run(int n,int m) 55 { 56 Build(1,n,1); 57 char str[3]; 58 for(int i=0,j,k;i<m;i++) 59 { 60 scanf("%s%d%d",str,&j,&k); 61 if(str[0]=='Q') printf("%d\n",Query(j,k,1,n,1)); 62 else Update(j,k,1,n,1); 63 } 64 } 65 66 int main() 67 { 68 int n,m; 69 while(scanf("%d%d",&n,&m)==2) 70 { 71 Run(n,m); 72 } 73 return 0; 74 }

?


?

題解鏈接:http://www.cnblogs.com/scidylanpno/p/7348281.html

版權(quán)所有:scidylanpno

轉(zhuǎn)載于:https://www.cnblogs.com/scidylanpno/p/7348281.html

總結(jié)

以上是生活随笔為你收集整理的HDU 1754 I Hate It 线段树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。