POJ 3264 segment tree to find interval maximum minimum value

Source: Internet
Author: User

Description

For the daily milking, Farmer John's n cows (1≤ n ≤50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate frisbee with some of the cows. To keep things simple, he'll take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to has fun they should not differ too much in height.

Farmer John has made a list of Q (1≤ q ≤200,000) Potential groups of cows and their heights (1≤ H Eight ≤1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the G Roup.

Input

Line 1:two space-separated integers, Nand Q. Lines 2.. N+1:line I+1 contains a single integer which is the height of cow ILines N+2.. N+ Q+1:two integers Aand B(1≤ ABN), representing the range of cows from ATo BInclusive.

Output

Lines 1.. Q: Each line contains a single integer so is a response to a reply and indicates the difference in height between the Tal Lest and shortest cow in the range.

Sample Input

6 31734251 54) 62 2

Sample Output

630

Source

Usaco January Silver Segment Tree processing This problem storage interval maximum and minimum value
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<map>#include<queue>#include<stack>#defineINF 0XFFFFFFFusing namespacestd;structtree{intL; intR; intMINV; intMAXV; intmid () {return(L+R)/2; }}tree[800010];intmaxv=-INF;intminv=INF;voidBuildtree (intRootintLintR) {Tree[root]. L=L; Tree[root]. R=R; TREE[ROOT].MINV=INF; TREE[ROOT].MAXV=-INF; if(l!=R) {Buildtree (2*root+1, L, (l+r)/2); Buildtree (2*root+2, (L+R)/2+1, R); }}voidInse (intRootintIintv) {    if(Tree[root]. l==Tree[root]. R) {TREE[ROOT].MINV=v; TREE[ROOT].MAXV=v; return ; } TREE[ROOT].MINV=min (tree[root].minv,v); TREE[ROOT].MAXV=Max (TREE[ROOT].MAXV,V); if(i<=Tree[root].mid ()) Inse (2*root+1, i,v); ElseInse (2*root+2, i,v);}voidQueryintRoot,intSinte) {    if(tree[root].minv>minv&&tree[root].maxv<MAXV)return ; if(Tree[root]. L==s&&tree[root]. r==e) {MINV=min (MINV,TREE[ROOT].MINV); MAXV=Max (MAXV,TREE[ROOT].MAXV); return ; }    if(e<=tree[root].mid ()) query (2*root+1, s,e); Else if(s>tree[root].mid ()) query (2*root+2, s,e); Else{query (2*root+1, S,tree[root].mid ()); Query (2*root+2, Tree[root].mid () +1, E); }}intMain () {intn,q; intre; intSs,ee; scanf ("%d%d",&n,&q); Buildtree (0,1, N);  for(intI=1; i<=n; i++)        {            //cout<< "**********" <<endl;scanf"%d",&re); Inse (0, I,re); }         for(intj=1; j<=q; J + +) {scanf ("%d%d",&ss,&ee); MINV=INF; MAXV=-INF; Query (0, Ss,ee); printf ("%d\n", maxv-MINV); }return 0;}
View Code

POJ 3264 segment tree to find interval maximum minimum value

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.