St Algorithm of RMQ

Source: Internet
Author: User

1#include <stdio.h>2#include <string.h>3 Const intN = -;4 intA[n];5 intdp[n][ -];6InlineintMinConst int&a,Const int&b)7 {8     returnA < b?a:b;9 }Ten  One /* A Dp[i][j] Indicates the minimum value in the interval beginning with I, with a length of 2^j - very clearly dp[i][0] = a[i]; - and the transfer equation is dp[i][j] = min (dp[i][j-1], dp[i+ (1<< (j-1)][j-1]); Divide the interval into 2 2^ (j-1) Inter-cell the */ - voidRmq_init (intN) - { -     inti,j; +      for(i=1; i<=n; ++i) dp[i][0] =A[i]; -      for(j=1; (1&LT;&LT;J) <=n; ++j) +          for(i=0; i+ (1&LT;&LT;J)-1<=n; ++i) ADp[i][j] = min (dp[i][j-1],dp[i+ (1<< (J-1))][j-1]);//divide the interval into 2 2^ (j-1) of the inter-cell, the idea of DP at } -  - //make 2^k <= r-l+1, then start with L, end of R with a length of 2^k, covering the interval [l,r] - //2^k <= r-l+1, the length of the 2^k is more than half of the interval [l,r], so starting with L, the length 2^k with R ends can cover the interval [l,r] - intRMQ (intLintR) - { in     intK =0; -      while(1<< (k +1) <= r-l+1) k++; to     returnMin (Dp[l][k], dp[r-(1&LT;&LT;K) +1][k]); + } - intMain () the { *     intN, I,l,r; $scanf"%d",&n);Panax Notoginseng      for(i=1; i<=n; ++i) -scanf"%d",&a[i]); the rmq_init (n); +      while(SCANF ("%d%d", &l,&r)! =EOF) A     { theprintf"%d\n", RMQ (l,r)); +     } -     return 0; $}

St Algorithm of RMQ

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.