跳到正文

剪贴板

剪贴板 pmtxn47n

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/extc++.h>

using namespace std;

struct IO {
    const static int N = 1 << 20;

    char ib[N], ob[N], *i1 = ib, *i2 = ib, *o = ob;
    streambuf *isb, *osb;

    bool getc(char &ch) { return i1 == i2 && (i2 = (i1 = ib) + isb->sgetn(ib, N)), i1 == i2 ? 0 : ch = *i1++; }

    template<class Int, enable_if_t<is_integral<Int>::value, bool> = 1>
    bool read(Int &x) {
        int s = 1;
        Int y = 0;
        char ch = 0;
        for (; !isdigit(ch);) {
            if (!getc(ch)) return 0;
            if (ch == '-') s = -1;
        }
        for (; isdigit(ch);)
            if ((y *= 10) += ch ^ 48, !getc(ch)) return 0;
        return x = s * y, 1;
    }

    template<class T, class... Args>
    bool read(T &x, Args &...args) { return read(x) && read(args...); }

    void flush() { osb->sputn(ob, o - ob), o = ob; }

    void putc(char ch) { *o++ = ch, o == ob + N && (flush(), 0); }

    template<class Int, enable_if_t<is_integral<Int>::value, bool> = 1>
    void write(Int x) { x < -9 || x > 9 ? write(x / 10), 0 : x < 0 && (putc(45), x = -x), putc(abs(x % 10) ^ 48); }

    template<class T, class... Args>
    void write(T x, Args ...args) { write(x), write(args...); }

    template<class... Args>
    void writeln(Args ...args) { write(args...), putc(10); }

    IO(istream &is, ostream &os) : isb(is.rdbuf()), osb(os.rdbuf()) {}

    ~IO() { flush(); }
} io((cin.tie(0)->sync_with_stdio(0), cin), cout);

const int N = 5e5 + 5, B = 750, M = 5e5 + 5, inf = numeric_limits<int>::max();

int n, q, lc[N], rc[N], pr[N], sz[N], pos[N], mn[N], mx[N], bs[6], st[6], a[N], rt[N], nxt[N], t[N], id[N], lb[B], rb[B], cnt, x, y, z, bcnt, cc;
default_random_engine e;

inline int node(int p) { return sz[++cnt] = 1, pr[cnt] = e(), pos[cnt] = p, mn[cnt] = mx[cnt] = p - 2, cnt; }

inline void update(int i) {
    int l = lc[i], r = rc[i], ls = sz[l] + 1;
    sz[i] = ls + sz[r], mn[i] = min(mn[l], min(pos[i], mn[r]) - 2 * ls), mx[i] = max(mx[l], max(pos[i], mx[r]) - 2 * ls);
}

void split(int i, int k, int &a, int &b) { i ? pos[i] < k ? a = i, split(rc[i], k, rc[i], b), update(a) : (b = i, split(lc[i], k, a, lc[i]), update(b)), 0 : a = b = 0; }
int merge(int a, int b) { return a && b ? pr[a] < pr[b] ? rc[a] = merge(rc[a], b), update(a), a : (lc[b] = merge(a, lc[b]), update(b), b) : a ^ b; }
inline void insert(int &rt, int p) { split(rt, p, x, y), rt = merge(merge(x, node(p)), y); }
void change(int i, int p, int q) { pos[i] ^ p ? p < pos[i] ? change(lc[i], p, q) : change(rc[i], p, q), 0 : pos[i] = q, update(i); }

inline int order(int i, int k) {
    int r = 0;
    while (i) pos[i] < k ? r += sz[lc[i]] + 1, i = rc[i] : i = lc[i];
    return r;
}

inline int kth(int i, int k) {
    for (;;) {
        int ls = sz[lc[i]];
        if (k > ls) if (k ^ ls + 1) k -= ls + 1, i = rc[i];
        else return pos[i];
        else i = lc[i];
    }
}

int lower_bound(int i, int ql, int rq, int o = 0) {
    if (!i || o + sz[i] < ql || mx[i] - 2 * o < rq) return -1;
    int l = lc[i], rk = o + sz[l] + 1, res = lower_bound(l, ql, rq, o);
    return ~res ? res : rk < ql || pos[i] - 2 * rk < rq ? lower_bound(rc[i], ql, rq, rk) : rk;
}

int upper_bound(int i, int qr, int rq, int o = 0) {
    if (!i || o >= qr || rq < mn[i] - 2 * o || rq > mx[i] - 2 * o) return -1;
    int l = lc[i], rk = o + sz[l] + 1, res = upper_bound(rc[i], qr, rq, rk);
    return ~res ? res : rk > qr || pos[i] - 2 * rk ^ rq ? upper_bound(l, qr, rq, o) : rk;
}

inline int posv(int i) {
    int c = a[i], r = order(rt[c], i) + 1, g = i - 2 * r + 1, t = lower_bound(rt[c], r + 1, g - 1);
    return ~t ? g + 2 * --t : min(g + 2 * sz[rt[c]], n + 1);
}

inline int posv2(int i, int c, int r) {
    int g = i - 2 * r + 1, t = lower_bound(rt[c], r + 1, g - 1);
    return ~t ? g + 2 * --t : min(g + 2 * sz[rt[c]], n + 1);
}

inline int count(int c, int l, int r) { return l > r ? 0 : order(rt[c], r + 1) - order(rt[c], l); }

inline int start(int c, int x) {
    int ox = order(rt[c], x), r = upper_bound(rt[c], ox, x - 2 * (ox + (a[x] == c)) - 1);
    if (!~r) return -1;
    int d = kth(rt[c], r);
    return posv2(d, c, r) ^ x ? -1 : d;
}

inline void init(int b) { for (int i = rb[b]; i >= lb[b]; t[i] = (nxt[i] > rb[b] ? nxt[i] : t[nxt[i]]), --i); }

inline int query(int i) {
    int cur = 1;
    while (t[cur] <= i) cur = t[cur];
    while (nxt[cur] <= i) cur = nxt[cur];
    int c = a[cur], oc = order(rt[c], cur);
    if (a[i] ^ c) return cur + 2 * (i - cur + 1 - order(rt[c], i + 1) + oc) - 1;
    int s = order(rt[c], i) - oc + 1;
    return cur + s - 1 + (nxt[cur] ^ n + 1 ? s - 1 : min(s - 1, n - cur + 1 - count(c, cur, n)));
}

inline void build(int *st, int cnt) {
    int bcnt = 0;
    for (int i = 0, s, b; i < cnt; ++i) {
        s = st[i];
        if (s < 1 || s > n) continue;
        b = id[s], nxt[s] = posv(s);
        bool f = 0;
        for (int j = 0; j < bcnt; ++j)
            if (bs[j] == b) {
                f = 1;
                break;
            }
        if (!f) bs[bcnt++] = b;
    }
    for (int i = 0; i < bcnt; init(bs[i++]));
}

inline void add(int *st, int &cnt, int x) {
    if (x < 1 || x > n) return;
    for (int i = 0; i < cnt; ++i)
        if (st[i] == x) return;
    st[cnt++] = x;
}

inline void swap(int i) {
    int x = a[i], y = a[i + 1], cnt = 0, d;
    if (x == y) return;
    add(st, cnt, i), add(st, cnt, i + 1), add(st, cnt, d = start(x, i)), add(st, cnt, d = start(y, i)), change(rt[x], i, i + 1), change(rt[y], i + 1, i), swap(a[i], a[i + 1]), add(st, cnt, d = start(x, i)), add(st, cnt, d = start(y, i)), build(st, cnt);
}

int main() {
    io.read(n, q), mn[0] = inf, mx[0] = -inf;
    for (int i = 0; i < n; io.read(a[++i]), insert(rt[a[i]], i));
    for (;;) {
        lb[bcnt] = bcnt * B + 1, rb[bcnt] = min(n, bcnt * B + B), fill(id + lb[bcnt], id + rb[bcnt] + 1, bcnt);
        if (rb[bcnt++] == n) break;
    }
    for (int i = 0; i < n; ++i, nxt[i] = posv(i));
    nxt[n + 1] = t[n + 1] = n + 1;
    for (int b = bcnt; b; init(--b));
    for (int tp, x; q--; io.read(tp, x), tp ^ 2 ? swap(x) : io.writeln(query(x)));
}