鏈接http://acm.hdu.edu.cn/showproblem.php?pid=1754
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 9
Hint
Huge input,the C function scanf() will work better than cin
這篇看了b站SWPU的教程,av9350697,鏈接https://www.bilibili.com/video/av9350697?from=search&seid=7800936964375617671
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 2e6+10;
struct stu
{
int l, r, v;
}node[maxn<<2];
int father[maxn];
void build(int i, int l, int r)
{
node[i].l = l;
node[i].r = r;
node[i].v = 0;
if(l == r)
{
father[l] = i;
return ;
}
build(i<<1, l, (l+r)/2);
build((i<<1)|1, (l+r)/2+1, r);
}
void update(int ri)
{
if(ri==1) return ;
int fi = ri/2;
int a = node[fi<<1].v;
int b = node[(fi<<1)|1].v;
node[fi].v = max(a, b);
update(fi);
}
int maax;
void query(int i, int l, int r)
{
if(node[i].l == l && node[i].r ==r)
{
maax = max(maax, node[i].v);
return ;
}
i = i<<1;
if(l<=node[i].r)
{
if(r<=node[i].r) query(i, l, r);
else query(i, l, node[i].r);
}
i++;
if(r>=node[i].l)
{
if(l>=node[i].l) query(i, l, r);
else query(i, node[i].l, r);
}
}
int main()
{
int n, m, i, t;
cin>>t;
char s[10];
while(t--)
{
scanf("%d%d", &n, &m);
int x, y;
build(1, 1, n);
for(i=1; i<=n; i++)
{
scanf("%d", &x);
node[father[i]].v = x;
update(father[i]);
}
while(m--)
{
scanf("%s%d%d", s, &x, &y);
if(s[0]=='Q')
{
maax = 0;
query(1, x, y);
cout<<maax<<endl;
}
else
{
node[father[x]].v = y;
update(father[x]);
}
}
}
return 0;
}