@loj - 6062 @ "2017 Shandong round training Day2" Pair


@description@

A given number of columns of length n {ai} of length m with a series {bi}, {ai} find the number of consecutive sub-series of length m and {bi} of the match.

Two columns can be matched, if and only if there is a scheme, the number of columns in the two may be paired off.

Two numbers can be paired, and only if they are not less than h.

Input format
of the first row of three integers n, m, h.
The second row has m numbers b1, b2, ..., bm.
The third row has n numbers a1, a2, ..., an.

Output format
outputs a digital, {ai} of length consecutive number m number of sub-columns can be matched with {bi}.

Sample
Sample Input 1
. 5 10 2
. 5. 3
. 1. 5. 8. 5. 7
Sample Output 1
2

And prompt data range
to 100% of the data, 1 <= m <= n <= 150000; 1 <= ai, bi, h <= 10 ^ 9.

@solution@

In fact, match bipartite graph matching here.
However, this data range, if you really write bipartite graph matching, fear that even edges are built out.
But only need to ask this question the existence of half maximum matching of view, and we have a hall theorems describe the bipartite graph maximum matching existence.

hall theorem shows that a set of points S optionally bipartite graph from the left, the point S is taken out with a point on the right side is connected, constituting Neighborhood T. If for any S satisfies | S | <= | T | , the maximum matching bipartite graph exists.
We generally use the time, need to try to find a set of points does not satisfy the hall theorem that | S |> | T | set of points S, or equivalently denoted | S | - | T |> 0.

Note that there is a nature of this question: If ai> = aj, then the connected aj bk, ai can be connected to bk. Obviously this comparison, because ai + bk> = aj + bk > = h.
So we set the set point for the maximum weight value S ax, apparently corresponding S T is uniquely determined by the value of ax. So if you want | S | - | T |> 0, that is, as much as possible to make big S, can be found in the greatest of all <= ax S into all inside.

First processing ambassador ai + bj> = number bj h, denoted by f [i].
For a bipartite graph, we have O (n) test: For every a i, statistical this bipartite graph aj <= the number p ai, j, it is determined whether p - f [i]> 0 , if so can not match.
He notes that each change bipartite graph of only one more point + at least one point, so we can build on the tree line weights, maintaining a fast p - f [i] of the maximum value (the code presented is f [i ] - p is the minimum).

@accepted code@

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 150000;
int d[MAXN + 5], dcnt = 0;
int a[MAXN + 5], b[MAXN + 5];
int n, m, h;
struct segtree{
    #define lch x<<1
    #define rch x<<1|1
    struct node{
        int l, r;
        int tg, mn;
    }t[4*MAXN + 5];
    void pushup(int x) {t[x].mn = min(t[lch].mn, t[rch].mn);}
    void pushdown(int x) {
        if( t[x].tg ) {
            t[lch].tg += t[x].tg, t[lch].mn += t[x].tg;
            t[rch].tg += t[x].tg, t[rch].mn += t[x].tg;
            t[x].tg = 0;
        }
    }
    void build(int x, int l, int r) {
        t[x].l = l, t[x].r = r, t[x].tg = t[x].mn = 0;
        if( l == r ) return ;
        int mid = (l + r) >> 1;
        build(lch, l, mid), build(rch, mid + 1, r);
    }
    void modify(int x, int l, int r, int d) {
        if( l > t[x].r || r < t[x].l )
            return ;
        if( l <= t[x].l && t[x].r <= r ) {
            t[x].mn += d, t[x].tg += d;
            return ;
        }
        pushdown(x);
        modify(x << 1, l, r, d);
        modify(x << 1 | 1, l, r, d);
        pushup(x);
    }
}T;
int main() {
    scanf("%d%d%d", &n, &m, &h);
    for(int i=1;i<=m;i++)
        scanf("%d", &b[i]);
    for(int i=1;i<=n;i++)
        scanf("%d", &a[i]), d[++dcnt] = a[i];
    sort(d + 1, d + dcnt + 1);
    dcnt = unique(d + 1, d + dcnt + 1) - d - 1;
    T.build(1, 1, dcnt);
    for(int i=1;i<=m;i++)
        T.modify(1, lower_bound(d + 1, d + dcnt + 1, h - b[i]) - d, dcnt, 1);
    for(int i=1;i<=n;i++)
        a[i] = lower_bound(d + 1, d + dcnt + 1, a[i]) - d;
    for(int i=1;i<=m;i++) T.modify(1, a[i], dcnt, -1);
    int ans = (T.t[1].mn >= 0);
    for(int i=m+1;i<=n;i++) {
        T.modify(1, a[i], dcnt, -1), T.modify(1, a[i-m], dcnt, 1);
        ans += (T.t[1].mn >= 0);
    }
    printf("%d\n", ans);
}

@details@

When processing the code f [i] is the direct line segment tree simple calculations.

others. . . There should be no noteworthy detail of it.

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11410820.html