题目:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1188
代码:
#include <stdio.h>
#include <stdlib.h>
#include<algorithm>
using namespace std;
int t;
struct node
{
int l,r;
int n,maxn;
}q[50000000]; //一般开3到4倍
void init(int l,int r,int i)
{
q[i].l=l;
q[i].r=r;
q[i].n=0;
q[i].maxn=0;
int mid=(l+r)/2;
if(l!=r)
{
init(l,mid,i*2);
init(mid+1,r,2*i+1);
}
}
void insert(int i,int x,int m)
{
if(x>=q[i].l&&x<=q[i].r)
{
q[i].n=m;
q[i].maxn=m;
}
if(q[i].l==q[i].r)
return;
int mid=(q[i].l+q[i].r)/2;
if(x>mid)
insert(2*i+1,x,m);
else
insert(2*i,x,m);
q[i].maxn=max(q[2*i].maxn,q[2*i+1].maxn);
}
int find_max(int x,int y,int i)
{
if(x == q[i].l && y == q[i].r)
return q[i].maxn;
int mid = (q[i].l+q[i].r)/2;
if(x>mid) return find_max(x,y,2*i+1);
else if(y<=mid) return find_max(x,y,2*i);
else
return max(find_max(x,mid,2*i),find_max(mid+1,y,2*i+1));
}
int main()
{
int temp=0;
while(~scanf("%d",&t))
{
init(1,t,1);
for(int i=1;i<=t;i++)
{
int y;
scanf("%d",&y);
insert(1,i,y);
}
int n;
scanf("%d",&n);
printf("Case %d:\n",++temp);
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",find_max(x,y,1));
}
}
}