Hdu 5289 Assignment (to an array, how many intervals to satisfy the difference between the maximum and minimum values in the interval is less than k)

Source: Internet
Author: User

1. The interval is a section, not disconnected yo

2. The code is written in the view of the standard

3. Enumerate the left endpoint, the binary right end-point process:



#include <cstdio> #include <cstring> #include <cmath> #define LL long Long#define Max (a) (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;const int n=200007;int minn[n][20];//2^18=262144 2^20=1048576int maxx[n][20];//----------- -----------Query O (1)-------------int querymin (int l,int r) {int K=floor (log2 (double) (r-l+1)),//2^k <= (r-l + 1), Flo The OR () takes the whole function down to return Min (minn[l][k],minn[r-(1<<k) +1][k]);}    int Querymax (int l,int r) {int K=floor (log2 (double) (r-l+1)); Return Max (maxx[l][k],maxx[r-(1<<k) +1][k]);}    -------------------------------------------------int calc (int l,int r) {int k=log2 ((double) (r-l+1));    int Max=max (maxx[l][k],maxx[r-(1<<k) +1][k]);    int Min=min (minn[l][k],minn[r-(1<<k) +1][k]); return max-min;}    int main () {int T;    int n,k,i,j,p;    LL ans;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&k); for (I=1; i<=n; ++i) {scanf ("%d",&AMP;J);        Minn[i][0]=maxx[i][0]=j; }//------------------------------------------pretreatment o (nlogn)---------------for (j=1; (1&LT;&LT;J) <=n; ++J)//1<<j==2^j, enumeration interval length 1,2,4,8,16,,,,, for (I=1; i+ (1<<j) -1<=n; ++i)//i+ (1<<j)-1 denotes interval right boundary, enumeration interval                Left Border {p= (1<< (j-1));                Minn[i][j]=min (Minn[i][j-1],minn[i+p][j-1]);            Maxx[i][j]=max (Maxx[i][j-1],maxx[i+p][j-1]); }//-----------------------------------------------------------------------//---------------------------Enumerate the left endpoint,        The two-point right endpoint---------------------------int l,r,mid;            The ans=0;//left endpoint is fixed to I, the right endpoint is determined with L,r,mid, and finally with one of L and R, at this time l+1==r for (i=1; i<=n; ++i) {l=i,r=n; while (L+1<r) {mid= (l+r) >>1;//(l+r)/2== (l+r) >>1 if (Calc (i,mid) &L                t;k) {l=mid; } else {r=Mid-1;//himself to demonstrate the algorithm flow know that R can be assigned Mid-1}} if (Calc (i,r) <k) {ans            =ans+ (LL) (r-i+1);            } else {ans=ans+ (LL) (l-i+1);    }}//---------------------------------------------------------------------------printf ("%lld\n", ans); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 5289 Assignment (to an array, how many intervals to satisfy the difference between the maximum and minimum values in the interval is less than k)

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.