5289 Assignment (rmq+ sub-interval)

Source: Internet
Author: User

Title Link: 5289 Assignment

Test instructions: given N and K, indicating that there is a series of n number of sequences, the number of intervals, the interval of any two number of the difference is less than k

Ideas:

1. The interval of any two number of less than k equivalent to the interval max-min<k, with the RMQ to maintain, the maximum interval minimum value

2. Finally, the violent enumeration interval must be timed out, it is found that with the expansion of the interval max-min value is also larger (non-diminishing), there is monotonicity is easy to think of two points, so is the enumeration of the left endpoint, two points to find the right endpoint.


AC Code:

#include <stdio.h> #include <algorithm>using namespace std; #define LL Int#define Lson l,m,rt<<1# Define Rson m+1,r,rt<<1|1const LL maxn = 100010; LL DP[3][MAXN][20];    LL mm[maxn],a[maxn];//Initialize RMQ, b array subscript starting from 1, start with 0 simple modify void initrmq (LL n,ll b[]) {mm[0] =-1;        for (LL i = 1; I <= n; i++) {Mm[i] = ((i& (i-1)) = = 0)? Mm[i-1]+1:mm[i-1];    Dp[0][i][0] = Dp[1][i][0]=b[i];  } for (ll j = 1, J <= Mm[n]; j + +) {for (ll i = 1; i + (1<<j)-1 <= N; i++) {Dp[0][i][j] =            Max (dp[0][i][j-1],dp[0][i+ (1<< (j-1))][j-1]);        Dp[1][i][j] = min (dp[1][i][j-1],dp[1][i+ (1<< (j-1))][j-1]);    }}}//Query Max ll RMQ (ll X,ll y) {ll k = mm[y-x+1];    LL Tmp=max (dp[0][x][k],dp[0][y-(1<<k) +1][k]);    Tmp-=min (dp[1][x][k],dp[1][y-(1<<k) +1][k]); return TMP;}    int main () {LL t,i,n;    LL K;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&k); for (I=1; i<=n; i++) scanf("%d", &a[i]);        INITRMQ (N,a);        LL l,r,m;        __int64 ans=0;            for (I=1; i<=n; i++) {l=i,r=n;            LL t1,t2,tmp;                while (l<=r) {m= (l+r)/2;                TMP=RMQ (I,M);                if (tmp<k) l=m+1;            else r=m-1; }//printf (".....            [L%d m%d r%d],tmp=%d\n], l,m,r,tmp);        ans+= (__int64) (l-i);    } printf ("%i64d\n", ans); } return 0;}



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

5289 Assignment (rmq+ sub-interval)

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.