比赛 Segment Tree Competition 评测结果 AAAAAAAAAA
题目名称 延绵的山峰 最终得分 100
用户昵称 可以的. 运行时间 1.259 s
代码语言 C++ 内存使用 15.57 MiB
提交时间 2016-08-28 19:56:25
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <deque>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define M(a,v) memset(a,v,sizeof(a))

using namespace std;
const int maxn = 1000090*4;

int n,m,Max[maxn];
void read(int &res){
	int x , f = 1;  char ch;
	while(ch=getchar(),ch<'0'||ch>'9')if(ch=='-')f=-1;
	x = ch - 48;
	while(ch=getchar(),ch>='0'&&ch<='9')x=x*10+ch-48;
	res = x * f;
}

void Build(int i,int x,int rt,int l,int r){
	if(l==r){
		Max[rt] = x; return;
	}
	int mid=(l+r)>>1;
	if(i<=mid) Build(i,x,lson);
	else Build(i,x,rson);
	Max[rt] = max( Max[rt<<1] , Max[rt<<1|1] );
}

int getmax(int s,int t,int rt,int l,int r){
	if(s<=l && t>=r) return Max[rt];
	int mid = (l + r) >> 1;
	if(t<=mid) return getmax(s,t,lson);
	if(s>mid) return getmax(s,t,rson);
	return max(getmax(s,t,lson),getmax(s,t,rson));
}

void Work(){
	read(n); n ++ ;
	for(int i=1;i<=n;i++){
		int x; read(x); Build(i,x,1,1,n);
	}
	read(m);
	while(m--){
		int x,y; read(x); read(y); x++; y++;
		int ans=getmax(x,y,1,1,n);
		printf("%d\n",ans);
	}
}

int main(){
	freopen("climb.in","r",stdin);
	freopen("climb.out","w",stdout);
	Work(); return 0;
}