「CF1142B」Lynyrd Skynyrd

Portal
Luogu

Problem-solving ideas

I found a property:
for any cyclic shift arrangement, the precursor of the same number of permutations is definitely constant.
Further, if a cyclic shift sequence is arranged in a certain interval, then the end of this cyclic shift \ (n-1 \) class precursors must be located within this range.
Here we can multiply to maintain \ (2 ^ k \) level ancestor, and then engage in a data structure maintained at maximum range just fine.

Details Notes

  • Gugu Gu

Reference Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while (!isdigit(c)) f |= (c == '-'), c = getchar();
    while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
    s = f ? -s : s;
}

const int _ = 200010;

int n, m, q, p[_], pre[_], lg[_];
int a[_], las[_], f[22][_], st[22][_];

inline int query(int ql, int qr) {
    int x = lg[qr - ql + 1];
    return max(st[x][ql], st[x][qr - (1 << x) + 1]);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in", "r", stdin);
#endif
    read(n), read(m), read(q);
    for (rg int i = 2; i <= max(n, m); ++i) lg[i] = lg[i / 2] + 1;
    for (rg int i = 1; i <= n; ++i) read(p[i]); p[0] = p[n];
    for (rg int i = 1; i <= m; ++i) read(a[i]);
    for (rg int i = 1; i <= n; ++i) pre[p[i]] = p[i - 1];
    for (rg int i = 1; i <= m; ++i) f[0][i] = las[pre[a[i]]], las[a[i]] = i;
    for (rg int j = 1; j <= lg[m]; ++j)
        for (rg int i = 1; i <= m; ++i)
            f[j][i] = f[j - 1][f[j - 1][i]];
    for (rg int i = 1; i <= m; ++i) {
        st[0][i] = i;
        for (rg int j = 0; j <= lg[n]; ++j)
            if ((n - 1) & (1 << j)) st[0][i] = f[j][st[0][i]];
    }
    for (rg int j = 1; j <= lg[m]; ++j)
        for (rg int i = 1; i + (1 << j) - 1 <= m; ++i)
            st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
    for (rg int ql, qr, i = 1; i <= q; ++i)
        read(ql), read(qr), putchar(query(ql ,qr) >= ql ? '1' : '0');
    return 0;
}

End Sahua \ (qwq \)

Guess you like

Origin www.cnblogs.com/zsbzsb/p/11745912.html