題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 94860 Accepted Submission(s): 35904
這讓很多學生很反感。
不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。
學生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。
思路:線段樹模板題,還是板子好用啊~
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <queue>
#define MAXN 200010
#define inf 0x3f3f3f3f
using namespace std;
struct node{
int l,r;
int add;
int sum;
int mx;
int mn;
}tree[MAXN<<2];
void pushup(int index){
tree[index].mx = max(tree[index<<1].mx,tree[index<<1|1].mx);
}
void pushdown(int index){
if(tree[index].add){
tree[index<<1].mx = tree[index].add;
tree[index<<1|1].mx = tree[index].add;
tree[index<<1].add = tree[index].add;
tree[index<<1|1].add = tree[index].add;
tree[index].add = 0;
}
}
void build(int l,int r,int index){
tree[index].l = l;
tree[index].r = r;
tree[index].add = 0;
if(l == r){
scanf("%d",&tree[index].sum);
tree[index].mn = tree[index].mx = tree[index].sum;
return ;
}
int mid = (l+r)>>1;
build(l,mid,index<<1);
build(mid+1,r,index<<1|1);
pushup(index);
}
void updata(int l,int r,int index,int val){
if(l <= tree[index].l && r >= tree[index].r){
tree[index].mx = val;
tree[index].add = val;
return ;
}
pushdown(index);
int mid = (tree[index].l+tree[index].r)>>1;
if(l <= mid){
updata(l,r,index<<1,val);
}
if(r > mid){
updata(l,r,index<<1|1,val);
}
pushup(index);
}
int query(int l,int r,int index){
if(l <= tree[index].l && r >= tree[index].r){
return tree[index].mx;
}
pushdown(index);
int mid = (tree[index].l+tree[index].r)>>1;
int Max = 0;
if(l <= mid){
Max = max(query(l,r,index<<1),Max);
}
if(r > mid){
Max = max(query(l,r,index<<1|1),Max);
}
return Max;
}
int main()
{
int n,m,x,y,z;
char q;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);
while(m--){
getchar();
scanf("%c",&q);
if(q == 'Q'){
scanf("%d%d",&x,&y);
printf("%d\n",query(x,y,1));
}
else{
scanf("%d%d",&x,&z);
updata(x,x,1,z);
}
}
}
return 0;
}