RMQ Nude Questions

Source: Internet
Author: User

POJ 3264

To find the difference between the maximum and the minimum in a certain interval

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <cmath>6 #defineMAXN 500057 using namespacestd;8 intN,M,A[MAXN];9 intma[maxn][ -],mi[maxn][ -];Ten voidPre () One { A      for(intI=1; i<=n;i++) mi[i][0]=a[i],ma[i][0]=A[i]; -      for(intj=1;(1&LT;&LT;J) <=n;j++) -      for(intI=1; i+ (1&LT;&LT;J)-1<=n;i++) theMi[i][j]=min (mi[i][j-1],mi[i+ (1<< (J-1))][j-1]), -Ma[i][j]=max (ma[i][j-1],ma[i+ (1<< (J-1))][j-1]); - }  - intQueryintLintR) + { -     intK=trunc (log2 (r-l+1)); +     return(Max (ma[l][k],ma[r-(1&LT;&LT;K) +1][k])-min ((Mi[l][k]), mi[r-(1&LT;&LT;K) +1][k])); A } at intMain () - { -     intL,r; -scanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++) scanf ("%d",&a[i]); - pre (); in      for(intI=1; i<=m;i++) -     { toscanf"%d%d",&l,&R); +printf"%d\n", Query (l,r)); -     } the     return 0; *}
View Code

Above by DSB Lq_double

RMQ Nude Questions

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.