hdu 1754 I Hate It(线段树之 单点更新+区间最值)
生活随笔
收集整理的這篇文章主要介紹了
hdu 1754 I Hate It(线段树之 单点更新+区间最值)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
I Hate It
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Time Limit: 9000/3000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)Problem Description 很多學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。
這讓很多學生很反感。
不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。
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 9HintHuge input,the C function scanf() will work better than cin 線段樹入門題目。 最近做多校聯合的比賽,發現每一場比賽都有涉及到線段樹的題目,但是我之前沒有學過線段樹,于是決定學一下。 #include<cstdio> #include<algorithm> using namespace std;#define lson l, mid, root<<1 #define rson mid+1, r, root<<1|1const int N = 2000000 + 50; struct node {int l, r, mmax; }a[4*N];void build_tree(int l, int r, int root) {a[root].l = l;a[root].r = r;if(l == r){scanf("%d",&a[root].mmax);return ;}int mid = (l + r) >> 1;build_tree(lson);build_tree(rson);a[root].mmax = max(a[root<<1].mmax, a[root<<1|1].mmax); }void update(int l, int r, int root, int k) {if(l == a[root].l && r == a[root].r){a[root].mmax = k;return;}int mid = (a[root].l + a[root].r) >> 1;if(r <= mid)update(l, r, root<<1, k);else if(l > mid)update(l, r, root<<1|1, k);else{update(lson, k);update(rson, k);}a[root].mmax = max(a[root<<1].mmax, a[root<<1|1].mmax); //更新完單個點之后更新整棵樹 }int Query(int l, int r, int root) {if(l == a[root].l && r == a[root].r)return a[root].mmax;int mid = (a[root].l + a[root].r) >> 1;if(r <= mid)return Query(l, r, root<<1);else if(l > mid)return Query(l, r, root<<1|1);elsereturn max(Query(lson), Query(rson)); }int main() {int n, m, a, b;char ch[5];while(~scanf("%d%d",&n,&m)){build_tree(1, n, 1);while(m--){getchar();scanf("%s%d%d",ch, &a, &b);if(ch[0] == 'Q')printf("%d\n",Query(a, b, 1));elseupdate(a, a, 1, b);}}return 0; }
總結
以上是生活随笔為你收集整理的hdu 1754 I Hate It(线段树之 单点更新+区间最值)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UVA 11235 Frequent v
- 下一篇: hdu 1166 敌兵布阵(线段树之 单