Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 52417 Accepted Submission(s): 20598
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define INF 0xfffffff
using namespace std;
int MAXV=-INF;
int MINV=INF;
struct tree
{
int L;
int R;
int maxv;
int minv;
int mid()
{
return (L+R)/2;
}
}tree[600010];
void buildtree(int root,int ss,int ee)
{
tree[root].L=ss;
tree[root].R=ee;
tree[root].minv=INF;
tree[root].maxv=-INF;
if(ss!=ee)
{
buildtree(2*root,ss,(ss+ee)/2);
buildtree(2*root+1,(ss+ee)/2+1,ee);
}
}
void insetree(int root ,int i,int s)
{
if(tree[root].L==tree[root].R)
{
tree[root].maxv=s;
tree[root].minv=s;
return ;
}
tree[root].maxv=max(tree[root].maxv,s);
tree[root].minv=min(tree[root].minv,s);
if(i<=tree[root].mid())
insetree(2*root,i,s);
else
insetree(2*root+1,i,s);
tree[root].maxv=max(tree[2*root].maxv,tree[2*root+1].maxv);//关键所在
}
void Query(int root,int ss,int ee)
{
if(tree[root].minv>=MINV&&tree[root].maxv<=MAXV)
return;
if(tree[root].L==ss&&tree[root].R==ee)
{ //cout<<"1****5"<<endl;
MAXV=max(tree[root].maxv,MAXV);
MINV=min(tree[root].minv,MINV);
return ;
}
if(ee<=tree[root].mid())
Query(2*root,ss,ee);
else if(ss>tree[root].mid())
Query(2*root+1,ss,ee);
else
{
// cout<<"1*****4"<<endl;
Query(2*root,ss,tree[root].mid());
Query(2*root+1,tree[root].mid()+1,ee);
}
}
int main()
{
int n,m;
int chushi;
while(scanf("%d %d",&n,&m)!=EOF)
{
MAXV=-INF;
MINV=INF;
buildtree(1,1,n);
for(int i=1;i<=n;i++)
{
scanf("%d",&chushi);
insetree(1,i,chushi);
}
int ss,ee;
char panduan;
for(int j=1;j<=m;j++)
{ //scanf("%c",&panduan);
// getchar();
// scanf("%d %d",&ss,&ee);
cin>>panduan>>ss>>ee;
MAXV=-INF;
MINV=INF;
if(panduan==‘Q‘)
{
Query(1,ss,ee);
printf("%d\n",MAXV);
}
else
insetree(1,ss,ee);
}
}
return 0;
}
原文:http://www.cnblogs.com/hsd-/p/4783903.html