@atcoder - AGC037F@ Counting of Subarrays


@description@

Given L, at least L consecutive identical number k may be combined into a k + 1.
Given a sequence of length N, the sequence is asked how many subintervals by several combined into a number.

Constraints
1≤N≤2×10^5, 2≤L≤N, 1≤Ai≤10^9

Input
Input following form:
NL
A1 A2 ... the AN

Output
number of output sub-interval satisfies the condition.

Sample Input 1
9 3
2 1 1 1 1 1 1 2 3
Sample Output 1
22

@solution@

In fact, very simple idea.

Consider a range of determination is legitimate.
If the range contains only a number, apparently legal.
If only one section contains a number, this number only when the number> = L is legal.
Otherwise, we can be minimized as much as possible the merger of x to x + 1. If there can not be combined x is not legitimate; otherwise recursive judge the legality of the new sequence.

For different sections, merged into the smallest x x + 1 at the same time can actually do.
So we intervals statistical methods came out:

For the first minimum x, the statistics include only legitimate Interval x.
Then all of x contains only very long interval (i.e., not in section extending to the left and right) were combined as much as possible the x + 1.
Because some number out of a number of merger represents the original sequence, so we also need to maintain lf and rf, respectively, and "the number of a number of acts as the right end point of the program," "a number of programs to act as the left point number."

Lf and rf specifically how to maintain it?
For a very long interval of x a1, a2, ..., ak. a [1 ... L-1] is obviously not act as the right end (not synthesized x + 1), a [L ... 2L-1] a right end may serve as a first point of x + 1, to a [L ... 2L-1] corresponding to rf together can get a new section of the x + rf 1.
Similarly, a [2L ... 3L-1 ] corresponding to the right end of the second x + 1, a [3L ... 4L-1 ] corresponding to the right end of the third x + 1 ......
the right point in order to be out, the left point on the empathy.

If a very long interval <L, delete it directly.
May be synthesized as x x + 1 can of resynthesis, then the program will calculate the number of repetition (not legal and will calculate the Scheme). In the process of synthesis required x x + 1 subtracts the contribution generated by the synthesis of x + 1 x + 2, in order to ensure non-repetition.

Achieved by the deletion of the list. Remove the priority queue with the smallest element.
The combined as a synthetic L 1, L-1 at least reduced elements. So there will only O (n) merger.
So the complexity of the bottleneck priority queue, is O (nlogn).

@accepted code@

#include<cstdio>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
#define mp make_pair
#define fi first
#define se second
const int MAXN = 200000;
typedef pair<int, int> pii;
typedef long long ll;
priority_queue<pii, vector<pii>, greater<pii> >que;
int lst[MAXN + 5], nxt[MAXN + 5], N, L;
void link(int x, int y) {lst[y] = x, nxt[x] = y;}
bool check(int x, int y) {return nxt[x] == y;}
ll lf[MAXN + 5], rf[MAXN + 5], f1[MAXN + 5], f2[MAXN + 5];
vector<int>v1, v2;
ll solve(int x) {
    ll ret = 0, tmp = 0;
    int lt = v2.size(), lb = lst[v2[0]], rb = nxt[v2[lt-1]];
    for(int i=0;i<lt;i++) {
        if( i - L + 1 >= 0 ) tmp += lf[v2[i-L+1]];
        ret += tmp * rf[v2[i]];
    }
    for(int i=0;i<lt;i++)
        f1[v2[i]] = lf[v2[i]], f2[v2[i]] = rf[v2[i]], lf[v2[i]] = rf[v2[i]] = 0;
    int c = lt / L;
    if( c ) {
        for(int i=L-1;i<lt;i++) {
            int t = (i + 1)/L - 1;
            rf[v2[t]] += f2[v2[i]];
        }
        for(int i=lt-L;i>=0;i--) {
            int t = c - (lt - i)/L;
            lf[v2[t]] += f1[v2[i]];
        }
        for(int i=1;i<c;i++)
            link(v2[i-1], v2[i]);
        link(lb, v2[0]), link(v2[c-1], rb);
        for(int i=0;i<c;i++)
            que.push(mp(x + 1, v2[i]));
        tmp = 0;
        for(int i=0;i<c;i++) {
            if( i - L + 1 >= 0 ) tmp += lf[v2[i-L+1]];
            ret -= tmp * rf[v2[i]];
        }
    }
    else nxt[lb] = N + 1, lst[rb] = 0;
    v2.clear();
    return ret;
}
int main() {
    scanf("%d%d", &N, &L);
    for(int i=1;i<=N;i++) {
        int x; scanf("%d", &x);
        que.push(mp(x, i)), link(i, i + 1);
        lf[i] = rf[i] = 1;
    }
    ll ans = 0; link(0, 1);
    while( !que.empty() ) {
        int x = que.top().fi; v1.clear();
        while( !que.empty() && que.top().fi == x )
            v1.push_back(que.top().se), que.pop();
        v2.clear(); v2.push_back(v1[0]);
        for(int i=1;i<v1.size();i++) {
            if( !check(v1[i-1], v1[i]) )
                ans += solve(x);
            v2.push_back(v1[i]);
        }
        ans += solve(x);
    }
    printf("%lld\n", ans + N);
}

@details@

Why AGC there will be a change of title F water the illusion

Even thought in this direction, it is difficult to say to be able to think clearly all the details of it.
Ok. It should be like this.

Guess you like

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