1068:rmq-st algorithm

Source: Internet
Author: User

#1068: Rmq-st algorithm time limit: 10000ms single point time: 1000ms memory limit: 256MB description

Little Hi and Little Ho have traveled in the United States for quite a long time and are finally ready to return home! And before returning home, they are going to the supermarket to buy some local specialties-such as Hamburg (fog) and so on to return home.

But after the supermarket, Little Hi and the small ho found the supermarket has too many kinds of goods-they really do not look over! So little hi decided to delegate a task to small ho: Suppose that the entire shelf from left to right visited N products, and sequentially numbered 1 to N, each time a small hi gave a period of [L, R], small ho to do is to select the label in this range of all goods weight of the lightest one, and tell the small hi this product weight , so that they can buy a lot of things without any difficulty-how pathetic the choice of difficult patients.

(Although the interval of each given is still small hi to make the decision-but little hi finally witty choice to use random numbers to generate these intervals!) But why is little hi not directly using random numbers to generate a shopping list? --ask so much what to do! )

Tip 1:2 is the method of cosmic Xeon! (Are you sure?) )

Hint Two: line tree is not the dichotomy?

Input

Each test point (input file) has and has only one set of test data.

The 1th behavior of each set of test data is an integer n, meaning as described earlier.

The 2nd behavior of each group of test data n integers, respectively, describes the weight of each commodity, where the I integer represents the weight of the commodity labeled I weight_i.

The 3rd behavior of each group of test data is an integer q, which indicates the number of times the small hi has inquired.

Each group of test data is in line n+4~n+q+3, each line describing a query, where the n+i+3 behavior is two integers li, RI, which represents a range of small hi queries [Li, RI].

For 100% of data, meet N<=10^6,q<=10^6, 1<=li<=ri<=n,0<weight_i<=10^4.

Output

For each set of test data, for each small hi query, according to the order in which they appear in the input, each output line represents the result: the weight of the lightest item in all the items in the interval [Li, Ri].

Sample input
1073341556828616402699480780689814120217953 42 82 46 87 10
Sample output
16409811556981981

Method One: Line segment tree
1#include <iostream>2 #defineN 10000053 #defineINF 0x3f3f3f3f4 using namespacestd;5 6 inttree[n<<2];7 8 voidPushup (intRT) {9TREE[RT] = min (tree[rt<<1],tree[rt<<1|1]);Ten } One  A voidBuildintLintRintRT) { -     if(L = =R) { -scanf"%d",&Tree[rt]); the         //cout<<tree[rt]<< "" <<rt<<endl; -         return ; -     } -     intMid = (l+r) >>1; +Build (l,mid,rt<<1); -Build (mid+1,r,rt<<1|1); + pushup (RT); A } at  - intQueryintLintRintLintRintRT) { -     //L,r is a transform zone level and l,r is a given zone level -     if(r<l| | L>R) { -         returninf; -     } in     if(l>=l&&r<=R) { -         returnTree[rt]; to     } +     intans; -     intMid = (l+r) >>1; theans = min (Query (l,mid,l,r,rt<<1), Query (mid+1,r,l,r,rt<<1|1)); *     returnans; $ }Panax Notoginseng  - intn,m; the intMain () { +scanf"%d",&n); ABuild1N1); thescanf"%d",&m); +      while(m--){ -         intx, y; $scanf"%d%d",&x,&y); $         intCNT = Query (1, N,x,y,1); -printf"%d\n", CNT); -     } the     return 0; -}



Method Two: Rmq,st

1#include <iostream>2#include <cmath>3 #defineN 10000054 using namespacestd;5 intAn[n];6 intdp[n][ +];7 intT,n;8 9 voidStintN) {Ten      for(inti =1; I <= n;++i) Onedp[i][0] =An[i]; A      for(inti =1;(1<<i) <= N; ++i) { -          for(intj =1; j+ (1<<i)-1<= N; ++j) { -Dp[j][i] = min (dp[j][i-1],dp[j+ (1<< (I-1))][i-1]); the         } -     } - } - intRmqintLintR) { +     intK =0; -      while((1<< (k +1)) <= (r-l+1)) +k++; A     returnMin (dp[l][k],dp[r-(1&LT;&LT;K) +1][k]); at } - intMain () { -scanf"%d",&t); -      for(intI=1; i<=t;i++){ -scanf"%d",&an[i]); -     } in St (t); -scanf"%d",&n); to      for(intI=1; i<=n;i++){ +         intx, y; -scanf"%d%d",&x,&y); theprintf"%d\n", RMQ (x, y)); *     } $     return 0;Panax Notoginseng}

1068:rmq-st algorithm

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.