#include<bits/stdc++.h>
using namespace std;
#define mx 100000
long tree[mx*3],ar[mx];
void build(long node,long start,long end)
{
if(start==end)
{
tree[node]=ar[start];
}
else
{
long mid=(start+end)/2;
build(2*node,start,mid);
build(2*node+1,mid+1,end);
tree[node]=max(tree[2*node],tree[2*node+1]);
}
}
long query(long node,long b,long e,long i,long j)
{
if (i > e || j < b)
return -10000000;
if (b >= i && e <= j)
return tree[node];
int Left = node * 2;
int Right = node * 2 + 1;
int mid = (b + e) / 2;
int p1 = query(Left, b, mid, i, j);
int p2 = query(Right, mid + 1, e, i, j);
return max(p1,p2);
}
int main()
{
long n,i,qry;
while(cin>>n)
{
for(i=1; i<=n; i++)
{
cin>>ar[i];
}
build(1,1,n);
long a,b;
cin>>qry;
for(i=1; i<=qry; i++)
{
cin>>a>>b;
cout<<query(1,1,n,a,b)<<endl;
}
}
}
using namespace std;
#define mx 100000
long tree[mx*3],ar[mx];
void build(long node,long start,long end)
{
if(start==end)
{
tree[node]=ar[start];
}
else
{
long mid=(start+end)/2;
build(2*node,start,mid);
build(2*node+1,mid+1,end);
tree[node]=max(tree[2*node],tree[2*node+1]);
}
}
long query(long node,long b,long e,long i,long j)
{
if (i > e || j < b)
return -10000000;
if (b >= i && e <= j)
return tree[node];
int Left = node * 2;
int Right = node * 2 + 1;
int mid = (b + e) / 2;
int p1 = query(Left, b, mid, i, j);
int p2 = query(Right, mid + 1, e, i, j);
return max(p1,p2);
}
int main()
{
long n,i,qry;
while(cin>>n)
{
for(i=1; i<=n; i++)
{
cin>>ar[i];
}
build(1,1,n);
long a,b;
cin>>qry;
for(i=1; i<=qry; i++)
{
cin>>a>>b;
cout<<query(1,1,n,a,b)<<endl;
}
}
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন