I was back in 2016.5.9.
Today I saw my blog really someone to see, so excited, so excited ...
Today's generic version of the heap application is coming. Today's problem is to ask for the maximum interval. If you ask for the maximum interval, you will find that the collapse of the thing is if the time span of the request to do.
In fact, there are only three kinds of situations:
1/range is exactly (of course this is the best case)
2/Range to narrow (also not good trouble)
3/Cross Range ...
The specific solution is not a sentence can be said clearly, on the code
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define MAXN 100100
using namespace Std;
int n,l=1;
Struct ST
{
int left,right,d;
}T[3*MAXN];
int query (int id,int left,int right)
{
if (T[id].left==left && t[id].right==right)
return T[ID].D; Interval Just Right
if (t[2*id].right>=right)
return query (2*id,left,right);//Zoozi's right range is too large
else if (t[2*id+1].left<=left)
return query (2*id+1,left,right);//Right child's left range is too large
Else
Return Max (2*id,left,t[2*id].right),//cross-section, read message reply
Query (2*id+1,t[2*id+1].left,right));
}
int main ()
{
scanf ("%d", &n);
while (l<n) l+=l;
for (int i=l;i<=l+n-1;i++)
{
t[i].left=t[i].right=i-l+1;
scanf ("%d", &T[I].D);
}
for (int i=l+n;i<=2*l-1;i++)
t[i].left=t[i].right=i-l+1;
for (int i=l-1;i>=1;i--)
{
T[i].left=t[2*i].left;
T[i].right=t[2*i+1].right;
T[i].d=max (T[2*I].D,T[2*I+1].D);
}
int q,x,y;
scanf ("%d", &q);
for (int i=1;i<=q;i++)
{
scanf ("%d%d", &x,&y);
printf ("%d\n", Query (1,x,y));
}
return 0;
}
I'm going to prepare the super hard version.