有错误,一直RE 还没检查出来
代码:
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
const int MAX=1000000;
struct node
{
int left,right;
int mx;
}tree[MAX];
int a[MAX];
inline int max(int x,int y)
{
if(x<y)
return y;
else
return x;
}
void build(int T,int L,int R)
{
tree[T].left=L;
tree[T].right=R;//少了这两个
if(L==R)
{
tree[T].mx=a[L];
}
else
{
int mid=(L+R)/2;
build(T*2,L,mid);
build(T*2+1,mid+1,R);
tree[T].mx=max(tree[T*2].mx,tree[T*2+1].mx);
}
}
void UpDate(int T,int LL,int value)
{
tree[T].mx=max(tree[T].mx,value);
if(tree[T].left==LL&&tree[T].right==LL)
{
tree[T].mx=value;
return ;
}
//int mid=(L+R)/2;
if(LL<=tree[T*2].right)
{
UpDate(T*2,LL,value);
}
else
{
UpDate(T*2+1,LL,value);
}
//tree[T].mx=max(tree[T*2].mx,tree[T*2+1].mx);
}
int query(int T,int LL,int RR)
{
if(tree[T].left==LL&&tree[T].right==RR)
{
return tree[T].mx;
}
else
{
if(RR<=tree[T*2].right)
return query(T*2,LL,RR);
else if(tree[T*2+1].left<=LL)
return query(T*2+1,LL,RR);
else
{
int mid=(tree[T].left+tree[T].right)/2;
return max(query(T*2,LL,mid),query(T*2+1,mid+1,RR));
//return max(query(T*2,L,R,LL,mid),query(T*2+1,L,R,mid+1,RR));//错了
}
}
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
//memset(tree,0,sizeof(tree));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
build(1,1,n);
getchar();
char s[10];
for(int i=1;i<=m;i++)
{
gets(s);
//scanf("%s",s);
int x=s[2]-'0',y=s[4]-'0';
if(s[0]=='Q')
{
int ans=query(1,x,y);
printf("%d\n",ans);
}
else
{
UpDate(1,x,y);
}
}
}
system("pause");
return 0;
}
/*
***************TLE**************
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
const int MAX=1000000;
struct node
{
int left,right;
int value;
int mx;
}tree[MAX];
inline int max(int x,int y)
{
if(x<y)
return y;
else
return x;
}
void build(int T,int L,int R)
{
tree[T].left=L;
tree[T].right=R;
if(L==R)
{
tree[T].left=L;
tree[T].right=R;
}
else
{
int mid=(L+R)/2;
build(T*2,L,mid);
build(T*2+1,mid+1,R);
}
}
void UpDate(int T,int L,int R,int LL,int value)
{
if(L==LL&&R==LL)
{
tree[T].value=value;
tree[T].mx=value;
return ;
}
int mid=(L+R)/2;
if(LL<=mid)
{
UpDate(T*2,L,mid,LL,value);
}
else
{
UpDate(T*2+1,mid+1,R,LL,value);
}
tree[T].mx=max(tree[T*2].mx,tree[T*2+1].mx);
}
int query(int T,int L,int R,int LL,int RR)
{
if(L==LL&&R==RR)
{
return tree[T].mx;
}
else
{
int mid=(L+R)/2;
if(RR<=mid)
return query(T*2,L,mid,LL,RR);
else if(mid<LL)
return query(T*2+1,mid+1,R,LL,RR);
else
{
return max(query(T*2,L,mid,LL,mid),query(T*2+1,mid+1,R,mid+1,RR));
//return max(query(T*2,L,R,LL,mid),query(T*2+1,L,R,mid+1,RR));//错了
}
}
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
int temp;
build(1,1,n);
memset(tree,0,sizeof(tree));
for(int i=1;i<=n;i++)
{
scanf("%d",&temp);
UpDate(1,1,n,i,temp);
}
getchar();
char s[10];
for(int i=1;i<=m;i++)
{
gets(s);
//scanf("%s",s);
int x=s[2]-'0',y=s[4]-'0';
if(s[0]=='Q')
{
int ans=query(1,1,n,x,y);
printf("%d\n",ans);
}
else
{
UpDate(1,1,n,x,y);
}
}
}
system("pause");
return 0;
}*/