LuoGuP3774: [CTSC2017] increased the longest sequence

Pre

God magical Chi Mei Mei Miu Miu.

The following sections are copied directly from solution inside Luo Gu above my questions.

Solution

Young tableau come to learn about this topic would be better to do (or not A).
By the \ (Dilworth \) theorem, like the name, the number of non-liter can be solved in the current sub-sequence of state after ordering the inquiry, with each row of the matrix to maintain the Young, this is before the query \ (k \ ) the number of elements and line, so Fenwick tree statistics.

However, the space will be less than the opening, noted that the number of required non-liter sequence length L is equal to the required sequence, sequences required length of the non-liter and equal number of liters required sequence, can have two purposes in the matrix Instead shape (matrix may be thought of as the number of rows is a sub-sequence number, the length is different from most of the following requirements on a subsequence of the longest sub-sequence).

That is, the shape of two mutually matrix transpose (I never learned linear algebra, if error also please forgive the expression).

Then it is assumed \ (sz = \ sqrt n \ ) then for the number of lines is greater than \ (SZ \) would be required in the transposed matrix of which can be demonstrated, the abscissa \ (x \ in [1, sz] \) and ordinate \ (y \ in [1, sz] \) in all possible positions among both all bit, and a bit outside of this range, since it is assumed all of the bit (within the scope and transposed matrix \ (x \ in [sz + 1, n], y \ in [sz + 1, n] \) region), quantity exceeds \ (n-\) .

Then transposed matrix abscissa \ (x \ in [sz + 1, n] \) in the range, must not exist ordinate \ (y \ in [sz + 1, n] \) point.

It is possible to maintain two \ (sz \ times n \) matrix solver.

Code

#include<bits/stdc++.h>
using namespace std;
const int N = 50000 + 5, M = 233;
inline int lowbit (int u) {return u & (-u);}
int tree[N];
inline void add (int u, int v) {while (u <= N - 5) tree[u] += v, u += lowbit (u);}
inline int query (int u) {int res = 0; while (u) res += tree[u], u -= lowbit (u); return res;}
struct Q {int m, k, id, ans;}qq[N << 2];
int sz, n, b[N], q;
struct Matrix {
    int info[M][N], sign;
    inline void insert (int u, int v, int p) {
        if (u > sz) {return ;}
        int l = 1, r = min (v, info[u][0] + 1), mid;
        while (l < r) {
            mid = (l + r) / 2;
            if (sign ^ (info[u][mid] < p)) {r = mid;}
            else {l = mid + 1;}
        }
        swap (info[u][l], p);
        info[u][0] = max (info[u][0], l);
        if (p) {insert (u + 1, l, p);}
        else {
            if (sign) {if (l > sz)add (l, 1);}
            else {add (u, 1);}
        }
    }
}m1, m2;
int main () {
    m1.sign = 0, m2.sign = 1;
    scanf ("%d%d", &n, &q);sz = sqrt (n);
    for (int i = 1; i <= n; ++i) {scanf ("%d", &b[i]);}
    for (int i = 1; i <= q; ++i) {scanf ("%d%d", &qq[i].m, &qq[i].k);qq[i].id = i;}
    sort (qq + 1, qq + q + 1, [](Q a, Q b){return a.m < b.m;});
    int pre = 0;
    for (int i = 1; i <= q; ++i) {
        while (pre < qq[i].m) {
            ++pre;
            m1.insert (1, INT_MAX, b[pre]);
            m2.insert (1, INT_MAX, b[pre]);
        }
        qq[i].ans = query (qq[i].k);
    }
    sort (qq + 1, qq + q + 1, [](Q a, Q b){return a.id < b.id;});
    for (int i = 1; i <= q; ++i) {printf ("%d\n", qq[i].ans);}
    return 0;
}

Conclusion

Young tableau was quite interesting, but on this question, this solution is also very interesting ah.

Guess you like

Origin www.cnblogs.com/ChiTongZ/p/11221018.html