I Hate It
Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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。
在每个测试的第一行,有两个正整数 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
/*
以前写的代码好恶心。。。。上一题代码稍微改一下就行,求最大值的时候先更新,后求值。。加油!!!
Time:2015-1-17 23:38
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=200000+10;
const int INF=0x3f3f3f3f;
typedef long long LL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
struct Tree{
int l,r;
int maxV;
}tree[MAX<<2];
void Build(int l,int r,int rt){
tree[rt].l=l;
tree[rt].r=r;
tree[rt].maxV=-INF;
if(l==r)return;
int mid=(l+r)>>1;
Build(lson);
Build(rson);
}
void update(int pos,int val,int l,int r,int rt){
if(pos==l&&pos==r){
tree[rt].maxV=val;
return;
}
int mid=(l+r)>>1;
if(pos<=mid){
update(pos,val,lson);
tree[rt].maxV=max(tree[rt].maxV,val);
}else if(pos>mid){
update(pos,val,rson);
tree[rt].maxV=max(tree[rt].maxV,val);
}
}
LL Query(int L,int R,int l,int r,int rt){
if(L==l&&R==r){
return tree[rt].maxV;
}
int mid=(l+r)>>1;
if(R<=mid){
return Query(L,R,lson);
}else if(L>mid){
return Query(L,R,rson);
}else{
return max(Query(L,mid,lson),Query(mid+1,R,rson));
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
Build(1,n,1);
int pos,val;
for(int i=1;i<=n;i++){
scanf("%d",&val);
update(i,val,1,n,1);
}
char cmd[3];
int L,R;
while(m--){
scanf("%s",cmd);
if(cmd[0]=='Q'){
scanf("%d%d",&L,&R);
printf("%lld\n",Query(L,R,1,n,1));
}else{
scanf("%d%d",&pos,&val);
update(pos,val,1,n,1);
}
}
}
return 0;
}
//指针做法
//心得:弱爆了
//注意点:记得在判断的时候用中间的判断而不是左边和右边的,此错犯了两三次了,一直找找不到
//Time:2014-8-2
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX=200000+5;
const int INF=0xffffff0;
struct CNode
{
int L,R;
CNode *pLeft,*pRight;
int maxV;
}
Tree[MAX*2];//二倍节点即可
int nCount=0;
int Mid(CNode *pRoot)
{
return (pRoot->L+pRoot->R)/2;
}
void BuildTree(CNode *pRoot,int L,int R)
{
pRoot->L=L;
pRoot->R=R;
pRoot->maxV=-INF;
if(L==R)return;
nCount++;
pRoot->pLeft=Tree+nCount;
nCount++;
pRoot->pRight=Tree+nCount;
BuildTree(pRoot->pLeft,L,Mid(pRoot));
BuildTree(pRoot->pRight,Mid(pRoot)+1,R);
}
void Update(CNode *pRoot,int pos,int val)
{
if(pRoot->L==pRoot->R)//此时=pos
{
pRoot->maxV=val;
return;
}
if(pos<=Mid(pRoot))
{
Update(pRoot->pLeft,pos,val);
pRoot->maxV=max(pRoot->pLeft->maxV,pRoot->maxV);
}
else if(pos>=Mid(pRoot))
{
Update(pRoot->pRight,pos,val);
pRoot->maxV=max(pRoot->pRight->maxV,pRoot->maxV);
}
}
int QuerynMax(CNode *pRoot,int s,int e)
{
if(pRoot->L==s&&pRoot->R==e)
return pRoot->maxV;
if(e<=Mid(pRoot))
return QuerynMax(pRoot->pLeft,s,e);
else if(s>Mid(pRoot))
return QuerynMax(pRoot->pRight,s,e);
else
{
return
max(QuerynMax(pRoot->pLeft,s,Mid(pRoot))
,QuerynMax(pRoot->pRight,Mid(pRoot)+1,e));
}
}
int main()
{
int N,M;
char cmd[10];
while(scanf("%d%d",&N,&M)!=EOF)
{
nCount=0;
BuildTree(Tree,1,N);
for(int i=1;i<=N;i++)
{
int val;
scanf("%d",&val);
Update(Tree,i,val);
}
/*
for(int i=0;i<N*(N-1)/2;i++)
{
printf("%d %d %d\n",(Tree+i)->L,(Tree+i)->R,(Tree+i)->maxV);
}
*/
while(M--)
{
scanf("%s",cmd);
if(cmd[0]=='Q')
{
int s,e;
scanf("%d%d",&s,&e);
int sum=QuerynMax(Tree,s,e);
printf("%d\n",sum);
}
else
{
int pos,val;
scanf("%d%d",&pos,&val);
Update(Tree,pos,val);
}
}
}
return 0;
}