hdu5289 Assignment (interval query maximum minimum value, ST algorithm ...)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=5289

Test instructions: Given a sequence of length n and an integer k, find the interval between the maximum and minimum values less than K. Outputs the number of intervals that satisfy the condition.

Analysis: Enumeration A[i], to A[i] as the starting point, and then two points to find the end (large interval to meet the conditions of the Community must also meet), according to the location of the starting point and the end can be calculated to a[i] as the starting point to meet the conditions of the number of intervals. How to infer whether the interval satisfies the condition? The ST algorithm can be used to preprocess the O (N*logn) method, and then the O (1) query interval maximum can be the minimum value. The previous query timed out with the segment tree:

Later I saw others using the tree-like array + two points too.

There is a queue to write, or Direct line tree not two points ....

Code:

#include <iostream> #include <cstdio>using namespace std; #define Lson L,m,rt<<1#define Rson m+1,r,rt <<1|1const int maxn = 2e5;int _max,_min,n,k;int max[100006][20],min[100006][20],a[100006];void Init () {int i,j; for (i=0;i<n;i++) max[i][0]=min[i][0]=a[i];for (j=1; (1<<j) <=n;j++) {for (i=0;i+ (1<<j) -1<n;i++) {max[i][j]=max[i][j-1]>max[i+ (1<< (j-1))][j-1]? max[i][j-1]:max[i+ (1<< (j-1))][j-1]; min[i][j]=min[i][j-1]<min[i+ (1<< (j-1))][j-1]?

min[i][j-1]:min[i+ (1<< (j-1))][j-1];}}} bool OK (int l,int R) {int K=0;while ((1<< (k+1)) <= (r-l+1)) k++;_min=min[l][k]<min[r-(1<<k) +1][k]? min[l][k]:min[r-(1<<k) +1][k];_max=max[l][k]>max[r-(1<<k) +1][k]? max[l][k]:max[r-(1<<k) +1][k];return _max-_min<k;} int Find (int s) {int down=s+1,up=n-1,mid,ret=s;while (down<=up) {mid= (down+up) >>1;if (!ok (s,mid)) up=mid-1; Else{down=mid+1;if (Ret<mid) Ret=mid;}} return ret;} int main () {int ncase,i,j;long long ans;scanf ("%d", &ncase), while (ncase--) {scanf ('%d%d ', &n,&k); for (i=0;i <n;i++) scanf ("%d", &a[i]); Init (); Ans=n;for (i=0;i<n;i++) ans+=find (i)-i;printf ("%i64d\n", ans);} return 0;}



hdu5289 Assignment (interval query maximum minimum value, 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.