HDU 5289 the solution to the line tree is very interesting.

Source: Internet
Author: User


Welcome to the--bestcoder Anniversary (High quality topic + multiple Rewards)
AssignmentTime limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1565 Accepted Submission (s): 754


Problem Descriptiontom owns a company and he's the boss. There is n staffs which is numbered from 1 to N in the this company, and every the staff have a ability. Now, Tom was going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability for any of the. than K, and their numbers are continuous. Tom want to know the number of groups like this.
Inputin the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, K (1<=n<=100000, 0<k<=10^9), indicate the company have n Persons, k means the maximum difference between abilities of the staff in a group are less than K. The second line contains n integers:a[1],a[2],..., A[n] (0<=a[i]<=10^9), indicate the i-th staff ' s ability.
Outputfor each Test,output the number of groups.
Sample Input
24 23 1 2 410 50 3 4 5 2 1 6 7 8 9

Sample Output
528Hint

Authorfzuacm
Source2015 multi-university Training Contest 1
Recommendwe carefully selected several similar problems for you:5309 5308 5307 5306 5305

To satisfy the maximum length of any two element difference value less than K, with an array of d[i] representing the interval at the end of A[i]

Use a segment tree query before A[i] with a value greater than or equal to K in the position p

D[i] = max (d[i-1], p+1);

The answer is sum = Sigma (D[i]-i + 1);

The topic is very simple, the key is the idea to be precise, many times if the mathematics thought is stuck, can use the programming thought to solve. Still like to use line tree to solve problems, more efforts.

#include <cstdio> #include <algorithm> #include <cstring> #include <iostream>using namespace STD; #define MAXN 100000 + 10#define Lson L, Mid, Rt<<1#define Rson mid+1, R, Rt<<1|1int N, K;int a[maxn];int a ns;struct node{int mi, MA;}    t[maxn<<2];void pushup (int rt) {int L = rt<<1, r = rt<<1|1;    t[rt].ma = Max (t[l].ma, t[r].ma); T[RT].MI = min (t[l].mi, T[R].MI);}        void build (int L, int R, int rt) {if (L = = R) {t[rt].ma = T[RT].MI = A[l];    return;    } int mid = (L + R) >> 1;    Build (Lson);    Build (Rson); Pushup (RT);} Note that the nodes of the tree and the interval nodes do not confuse void query (int l, int r, int v, int l, int r, int rt) {if (L = = r) {if (ABS (T[RT].MI-V) &G        t;= k) {if (ans = =-1 | | ans < l) ans = l;    } return;    } int mid = (L + R) >> 1; if (R > Mid) if (ABS (T[RT&LT;&LT;1|1].MI-V) >= K | | ABS (T[RT&LT;&LT;1|1].MA-V) >= k) query (L, R,V, Rson);        if (ans = =-1 && l <= mid) if (ABS (T[RT&LT;&LT;1].MI-V) >= K | | ABS (T[RT&LT;&LT;1].MA-V) >= k) Query (l, R, V, Lson);}    int D[maxn];long long Sum;int main () {int t;    scanf ("%d", &t);        while (t--) {sum = 1;        scanf ("%d%d", &n, &k);        for (int i=1; i<=n; i++) scanf ("%d", &a[i]);        Build (1, N, 1);        D[1] = 1;            for (int i=2; i<=n; i++) {ans =-1;            Query (1, I, A[i], 1, N, 1);            if (ans = =-1) d[i] = d[i-1];            else D[i] = max (D[i-1], ans + 1);        Sum + = I-d[i] + 1;    } printf ("%i64d\n", sum); } return 0;}


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

HDU 5289 the solution to the line tree is very interesting.

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.