位运算的时候注意括号。。。一定是我写的太不科学了,和没用位运算比起来没快多少。。。
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=200010;
int a[maxn];
char test[10];
int tree[maxn*4];
void build(int node,int left,int right)
{
int mid=(left+right)>>1;
if(left==right)
{
tree[node]=a[mid];
}
else
{
build((node<<1),left,mid);
build((node<<1)+1,mid+1,right);
tree[node]=max(tree[(node<<1)],tree[(node<<1)+1]);
}
}
void change(int node,int left,int right,int x,int v)
{
int mid=(left+right)>>1;
if(left==x&&right==x)
{
tree[node]=v;
return ;
}
if(x<=mid)
{
change((node<<1),left,mid,x,v);
}
else if(x>mid)
{
change((node<<1)+1,mid+1,right,x,v);
}
tree[node]=max(tree[(node<<1)],tree[(node<<1)+1]);
}
int query(int node,int left,int right,int x,int y)
{
if(x<=left&&right<=y)
{
return tree[node];
}
else
{
int mid=(left+right)>>1;
if(x>mid)
{
return query((node<<1)+1,mid+1,right,x,y);
}
else if(y<=mid)
{
return query((node<<1),left,mid,x,y);
}
else
{
return max(query((node<<1),left,mid,x,y),query((node<<1)+1,mid+1,right,x,y));
}
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
build(1,1,n);
while(m--)
{
scanf("%s",test);
int x,y;
if(test[0]=='Q')
{
scanf("%d%d",&x,&y);
if(x>y)
swap(x,y);
printf("%d\n",query(1,1,n,x,y));
}
else
{
scanf("%d%d",&x,&y);
change(1,1,n,x,y);
}
}
}
return 0;
}