Heap Application C + +

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.