思路
线段树的应用,可是因为数据量太小,可以直接暴力
代码
#include <iostream>
#include <cstdio>
#define lson tree[root].ls
#define rson tree[root].rs
using namespace std;
const int maxn = 1000;
struct node
{
int ans;
int l, r;
int ls, rs;
}tree[maxn*2+10];
int num[maxn+10];
int nCount = 1;
void build(int root, int l, int r)
{
tree[root].l = l;
tree[root].r = r;
if(l==r)
tree[root].ans = num[l];
else
{
lson = ++nCount;
build(lson, l, (l+r)/2);
rson = ++nCount;
build(rson, (l+r)/2+1, r);
tree[root].ans = max(tree[lson].ans, tree[rson].ans);
}
}
int query(int root, int l, int r)
{
if(tree[root].l==l && tree[root].r==r)
return tree[root].ans;
else
{
int mid = (tree[root].l+tree[root].r)/2;
if(r<=mid) return query(lson, l, r);
else if(l>=(mid+1)) return query(rson, l, r);
else return max(query(lson, l, mid), query(rson, mid+1, r));
}
}
int main()
{
int t, n, q;
int l, r;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%d", num+i);
nCount = 1;
build(1, 1, n);
scanf("%d", &q);
for(int i=0; i<q; i++)
{
scanf("%d%d", &l, &r);
printf("%d\n", query(1, l, r));
}
}
return 0;
}