HDU 5289 ideas + line Tree processing

Source: Internet
Author: User

Give the number of N, and K

Find the number of intervals for all satisfying conditions (maximum-minimum number <k) of the n number

Array b records start at the current position, to the right of the maximum number of conditions, the value of B array can be found by the binary + segment tree to find the maximum interval minimum value

For the second set of data

10 5

0 3 4 5 2 1 6 7 8 9

The b array is

3 7 7 7 3 1 4 3 2 1

Because the current I point takes the most right-hand value may cause the i+1 point and the subsequent fetch to the point does not satisfy the condition, all should have: b[i]<=b[i+1]+1

Get B Array

3 5 4 3 2 1 4 3 2 1

Ans=28

#include "stdio.h" #include "string.h" struct node{int l,r; __int64 Ma,mi;}    Data[400010];__int64 a[100010],b[100010];__int64 Max (__int64 A,__int64 b) {if (a<b) return B; else return A;}    __int64 Min (__int64 A,__int64 b) {if (a<b) return A; else return b;}    void build (int l,int r,int k) {int mid;    Data[k].l=l;    Data[k].r=r;        if (l==r) {DATA[K].MA=DATA[K].MI=A[DATA[K].L];    return;    } mid= (DATA[K].L+DATA[K].R)/2;    Build (L,MID,K*2);    Build (mid+1,r,k*2+1);    Data[k].ma=max (data[k*2].ma,data[k*2+1].ma); Data[k].mi=min (DATA[K*2].MI,DATA[K*2+1].MI);}    __int64 query (int l,int r,int k,int op) {int mid;        if (data[k].l==l && data[k].r==r) {if (op==0) return DATA[K].MI;    else return data[k].ma;    } mid= (DATA[K].L+DATA[K].R)/2;    if (R&LT;=DATA[K*2].R) return query (L,R,K*2,OP);    else if (L&GT;DATA[K*2].R) return query (L,R,K*2+1,OP); else {if (op==0) return Min (query (l,mid,k*2,OP), query (MID+1,R,K*2+1,OP));    else return Max (query (L,MID,K*2,OP), query (MID+1,R,K*2+1,OP));    }}int Main () {int t,n,k,i,l,r,mid;    __int64 Ans,ma,mi;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&k);        for (i=1;i<=n;i++) scanf ("%i64d", &a[i]);        Build (1,n,1);            for (i=1;i<n;i++) {l=i;r=n;            if (data[1].ma<a[i]+k && data[1].mi>a[i]-k) l=n+1;                while (l<=r) {mid= (l+r)/2;                Ma=query (l,mid,1,1);                Mi=query (l,mid,1,0);                if (ma>=a[i]+k | | mi<=a[i]-k) r=mid-1;            else l=mid+1;        } b[i]=l-i;        } b[n]=1;        for (i=n-1;i>=1;i--) if (b[i]>b[i+1]+1) b[i]=b[i+1]+1;        ans=0;        for (i=1;i<=n;i++) ans+=b[i];    printf ("%i64d\n", ans); } return 0;}


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

HDU 5289 ideas + line Tree processing

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.