题解:P12998 [GCJ 2022 #3] Duck, Duck, Geese

· · 题解

思路

根据大佬题解和代码启示,完成本题。

步骤

  1. 破环成链

  2. 由于 A_i, B_i 可能为 0,需要调整成 >1 的形式,避免可行空间 [i, x] 与 [l, r] 重叠或重复统计

  3. 代码书写稍微优化一些,比较容易看懂。

参考代码

#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <bitset>
#include <unordered_map>

using namespace std;

typedef long long lld;
typedef pair<int, int> pii;

const int kSiz = 1e5 + 10;
const int kNum = 2e5 + 10;

int N, M, C;
pii req[kSiz];
deque<int> pos[kSiz];
int a[kNum];

struct Node {
    int l, r;
    int max, tag;
    int cnt;
}
tr[kNum * 4];

void read() {
    cin >> N >> C;
    M = N + N;
    for (int i = 1; i <= C; i++) {
        cin >> req[i].first >> req[i].second;
        if (req[i].second == 0) {
            req[i].first = req[i].second = N + 1;
        }
        else if (req[i].first == 0) {
            req[i].first = 1;
        }

        pos[i].clear();
    }
    for (int i = 1; i <= N; i++) {
        cin >> a[i];
        pos[a[i]].push_back(i);
        a[i + N] = a[i];
    }
    for (int i = 1; i <= C; i++) {
        int n = pos[i].size();
        for (int j = 0; j < n; j++) {
            pos[i].push_back(pos[i][j] + N);
        }
    }
}

int lc(int p) {
    return p * 2;
}

int rc(int p) {
    return p * 2 + 1;
}

void pushup(Node& p, Node& l, Node& r) {
    p.max = max(l.max, r.max);
    p.cnt = 0;
    if (p.max == l.max) {
        p.cnt += l.cnt;
    }
    if (p.max == r.max) {
        p.cnt += r.cnt;
    }
}

void pushup(int p) {
    pushup(tr[p], tr[lc(p)], tr[rc(p)]);
}

void build(int p, int l, int r) {
    tr[p].l = l;
    tr[p].r = r;
    tr[p].tag = 0;
    if (l == r) {
        tr[p].max = 0;
        tr[p].cnt = 1;
    }
    else {
        int m = (l + r) >> 1;
        build(lc(p), l, m);
        build(rc(p), m + 1, r);
        pushup(p);
    }
}

void solve(int p, int d) {
    tr[p].max += d;
    tr[p].tag += d;
}

void pushdown(int p) {
    if (tr[p].tag) {
        solve(lc(p), tr[p].tag);
        solve(rc(p), tr[p].tag);
        tr[p].tag = 0;
    }
}

void delta(int p, int a, int b, int d) {
    if (a > b) {
        return;
    }
    if (a <= tr[p].l && tr[p].r <= b) {
        solve(p, d);
        return;
    }
    pushdown(p);
    int m = (tr[p].l + tr[p].r) >> 1;
    if (a <= m) {
        delta(lc(p), a, b, d);
    }
    if (b > m) {
        delta(rc(p), a, b, d);
    }
    pushup(p);
}

int query(int p, int a, int b) {
    if (a <= tr[p].l && tr[p].r <= b) {
        if (tr[p].max == C) {
            return tr[p].cnt;
        }
        return 0;
    }
    pushdown(p);
    int ans = 0;
    int m = (tr[p].l + tr[p].r) >> 1;
    if (a <= m) {
        ans += query(lc(p), a, b);
    }
    if (b > m) {
        ans += query(rc(p), a, b);
    }
    return ans;
}

pii getReq(int c) {
    pii ans = { 0, -1 };
    if (pos[c].size() < req[c].first) {
        return ans;
    }

    ans.first = pos[c][req[c].first - 1];
    if (pos[c].size() <= req[c].second) {
        ans.second = M;
    }
    else {
        ans.second = pos[c][req[c].second] - 1;
    }
    return ans;
}

void toggleColor(int c, int d) {
    if (!pos[c].empty()) {
        delta(1, 1, pos[c].front() - 1, d);
    }
    else {
        delta(1, 1, M, d);
    }
    pii p = getReq(c);
    delta(1, p.first, p.second, d);
}

void work() {
    build(1, 1, M);
    for (int c = 1; c <= C; c++) {
        toggleColor(c, 1);
    }

    lld ans = 0;
    for (int i = 1; i <= N; i++) {
        ans += query(1, i + 1, i + N - 2);

        int c = a[i];
        toggleColor(c, -1);

        pos[c].pop_front();
        toggleColor(c, 1);
    }
    printf("%lld\n", ans);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0), std::cout.tie(0);

    // freopen("test.in", "r", stdin);

    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        read();
        printf("Case #%d: ", t);
        work();
    }

    return 0;
}