Accoders 4.7 省选模拟

· · 个人记录

radio

枚举最终的颜色,首先特判掉ml \geq n的情况,之后我们可以钦定覆盖位置不重。

然后要求最大覆盖点数,显然具有凸性,wqs二分即可。

一定要想清楚斜率和切点究竟是怎么变化。

#include <bits/stdc++.h>

#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)

typedef long long ll;

using std::cin;
using std::cout;
using std::endl;

typedef std::pair<int, int> pii;
#define fi first
#define se second

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

template <typename T>
T myrand(T l, T r) {
    return std::uniform_int_distribution<T>(l, r)(rnd);
}

void close_stdio() {
    std::ios::sync_with_stdio(false);
    std::cerr << "DO NOT use scanf/printf!\n";
}

const int kN = 1e6 + 5;
const ll kInf = 0x3f3f3f3f3f3f3f3fll;

int n, m, l, pre[kN];
char s[kN], a[kN];
typedef std::pair<ll, int> Ion;
Ion dp[kN];
Ion operator+(Ion x, int v) { return Ion(x.fi + v, x.se + 1); }

void init(char c) {
    For(i, 1, n) {
        if (s[i] == c)
            a[i] = 0;
        else
            a[i] = 1;
    }
}

int calc(char c) {
    // fprintf(stderr, "%c\n", c);
    init(c);
    int sum = 0;
    For(i, 1, n) sum += a[i], pre[i] = pre[i - 1] + a[i];
    if (l > n)
        return sum;
    if ((ll)m * l >= n)
        return 0;
    int lb = -n, rb = n, mid = 0;
    while (lb < rb) {
        mid = (lb + rb + 1) >> 1;
        std::fill(dp, dp + n + 1, Ion(-kInf, 0));
        dp[0] = Ion(0, 0);
        For(i, 1, n) {
            dp[i] = dp[i - 1];
            if (i >= l)
                dp[i] = std::max(dp[i], dp[i - l] + (pre[i] - pre[i - l] - mid));
        }
        // fprintf(stderr, "x=%d pos=%d %lld\n", mid, dp[n].se, dp[n].fi + (ll)lb * m);
        if (dp[n].se < m)
            rb = mid - 1;
        else
            lb = mid;
    }
    std::fill(dp, dp + n, Ion(-kInf, 0));
    dp[0] = Ion(0, 0);
    For(i, 1, n) {
        dp[i] = dp[i - 1];
        if (i >= l)
            dp[i] = std::max(dp[i], dp[i - l] + (pre[i] - pre[i - l] - lb));
    }
    // printf("x=%d pos=%d %lld\n", lb, dp[n].se, dp[n].fi + (ll)lb * m);
    return sum - dp[n].fi - (ll)lb * m;
}

int main() {
    freopen("radio.in", "r", stdin);
    freopen("radio.out", "w", stdout);
    scanf("%d%d%d%s", &n, &m, &l, s + 1);
    printf("%d", std::min({ calc('R'), calc('B'), calc('Y') }));
    return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */

channel

既然是平面图,显然要考虑对偶图相关的东西……但一直没有往补集上去想。

如果边权越变越小,直接用LCT维护即可。

注意平面图的最小生成树取补集是对偶图的最大生成树。我们用两棵LCT分别维护两者,那么不管边权是变大还是变小就都能知道MST的变化情况了。

注意检查底层操作……

#include <bits/stdc++.h>

#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)

typedef long long ll;

using std::cin;
using std::cout;
using std::endl;

typedef std::pair<int, int> pii;
#define fi first
#define se second

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

template <typename T>
T myrand(T l, T r) {
    return std::uniform_int_distribution<T>(l, r)(rnd);
}

void close_stdio() {
    std::ios::sync_with_stdio(false);
    std::cerr << "DO NOT use scanf/printf!\n";
}

const int kN = 4e5 + 5;
const int kInf = 0x3f3f3f3f;

int tot, n, m, q, ol, row[kN], col[kN];
ll lstans;

inline void decode(char ch, int &x, int &y, int &w) {
    // n is 'r'
    // m is 'c'
    static int mask = 0xfffff;
    w = (int)((w ^ lstans) & mask);
    if (ch == '-')
        x = (x + lstans - 1) % n + 1, y = (y + lstans - 1) % (m - 1) + 1;
    if (ch == '|')
        x = (x + lstans - 1) % (n - 1) + 1, y = (y + lstans - 1) % m + 1;
}

char gt() {
    char c = getchar();
    while (c != '|' && c != '-') c = getchar();
    return c;
}

inline int id1(int x, int y) {
    if (x > n || x <= 0 || y > m || y <= 0)
        return n * m + 1;
    return (x - 1) * m + y;
}
inline int id2(int x, int y) { return (x - 1) * (m + 1) + y; }

struct Edge {
    int adj[2][2], w, on;
    bool operator<(const Edge &rhs) const { return w < rhs.w; }
} e[kN];

bool isStrict;

// tp = 0: min ST; tp = 1: max ST
struct LCT {
    int tp, ch[kN][2], f[kN], rev[kN], best[kN];
    ll sum;
#define ls ch[o][0]
#define rs ch[o][1]
#define gtf(x) (x == ch[f[x]][1])
#define isrt(x) (x != ch[f[x]][gtf(x)])
    int chkm(int x, int y) {
        if (x <= (n + 1) * (m + 1) + 1 || y <= (n + 1) * (m + 1) + 1)
            return std::max(x, y);
        if (!x || !y)
            return x | y;
        if (e[x].w == e[y].w)
            return std::min(x, y);
        return (tp ^ (e[y] < e[x])) ? x : y;
    }
    void pushUp(int o) { best[o] = chkm(o, chkm(best[ls], best[rs])); }
    void pushRev(int o) {
        if (rev[o]) {
            std::swap(ls, rs);  // ERR
            rev[ls] ^= 1, rev[rs] ^= 1;
            rev[o] = 0;
        }
    }
    void connect(int x, int k, int y) {
        if (x)
            ch[x][k] = y;
        if (y)
            f[y] = x;
    }
    void rotate(int o) {
        int x = f[o], y = f[x], k = gtf(o);
        if (!isrt(x))
            connect(y, gtf(x), o);
        else
            f[o] = y;
        connect(x, k, ch[o][k ^ 1]);
        connect(o, k ^ 1, x);
        pushUp(x), pushUp(o);
    }
    void maintain(int o) {
        if (!isrt(o))
            maintain(f[o]);
        pushRev(o);
    }
    void splay(int o) {
        maintain(o);  // ERR
        while (!isrt(o)) {
            int x = f[o];
            if (!isrt(x))
                rotate(gtf(o) == gtf(x) ? x : o);
            rotate(o);
        }
    }
    void access(int o) {
        for (int x = 0; o; x = o, o = f[o]) {
            splay(o);
            connect(o, 1, x), pushUp(o);
        }
    }
    int getrt(int o) {
        access(o), splay(o);
        pushRev(o);
        while (ls) {
            o = ls, pushRev(o);
        }
        return o;
    }
    void makert(int o) { access(o), splay(o), rev[o] ^= 1; }
    void link(int x, int y) {
        // fprintf(stderr, "link %d %d\n", x, y);
        assert(x > (n + 1) * (m + 1) + 1 || y > (n + 1) * (m + 1) + 1);
        makert(x), splay(x), f[x] = y;  // ERR
    }
    void cut(int x, int y) {
        makert(x), access(y), splay(y);
        ch[y][0] = f[x] = 0, pushUp(y);
    }

    int getEdge(int x, int y) {
        makert(x);
        access(y);
        splay(y);
        if (best[y] <= (n + 1) * (m + 1) + 1) {
            printf("ls=%d rs=%d\n", ch[y][0], ch[y][1]);
        }
        return best[y];
    }
    bool addEdge(int id) {
        // fprintf(stderr, "addEdge %d (%d,%d)\n", id, e[id].adj[tp][0], e[id].adj[tp][1]);
        if (getrt(e[id].adj[tp][0]) != getrt(e[id].adj[tp][1])) {
            sum += e[id].w;
            e[id].on = tp;
            link(id, e[id].adj[tp][0]), link(id, e[id].adj[tp][1]);
            return true;
        }
        if (isStrict)
            assert(0);
        e[id].on = tp ^ 1;
        return false;
    }
    void cutEdge(int id) {
        // fprintf(stderr, "cutEdge %d\n", id);
        if (e[id].on != tp) {
            if (isStrict)
                assert(0);
            return;
        }
        e[id].on = tp ^ 1, sum -= e[id].w, cut(id, e[id].adj[tp][0]), cut(id, e[id].adj[tp][1]);  // ERR
    }
    void modify(int id, int w) {
        assert(e[id].on == tp);
        sum += w - e[id].w, e[id].w = w, splay(id);
    }
} ta, tb;

void show(int ec) {
#ifdef SHOW
    For(i, (n + 1) * (m + 1) + 2, ec) {
        fprintf(stderr, "Edge (%d,%d,%d,%d) belongs to %d\n", i, e[i].adj[0][0], e[i].adj[0][1], e[i].w,
                e[i].on);
    }
#endif
}

int main() {
    freopen("channel.in", "r", stdin);
    freopen("channel.out", "w", stdout);
    // freopen("in", "r", stdin);
    scanf("%d%d%d%d", &ol, &n, &m, &q);
    int ec = (n + 1) * (m + 1) + 1;
    tb.tp = 1;
    For(i, 1, n) For(j, 1, m) {
        row[id1(i, j)] = ++ec;
        e[ec].adj[0][0] = id1(i, j), e[ec].adj[0][1] = id1(i, j + 1), e[ec].adj[1][0] = id2(i, j + 1),
        e[ec].adj[1][1] = id2(i + 1, j + 1);
        if (e[ec].adj[0][1] == n * m + 1)
            e[ec].w = kInf;
        col[id1(i, j)] = ++ec;
        e[ec].adj[0][0] = id1(i, j), e[ec].adj[0][1] = id1(i + 1, j), e[ec].adj[1][0] = id2(i + 1, j),
        e[ec].adj[1][1] = id2(i + 1, j + 1);
        if (e[ec].adj[0][1] == n * m + 1)
            e[ec].w = kInf;
    }
    For(i, 1, n) For(j, 1, m) {
        if (i == 1) {
            ++ec;
            e[ec].adj[0][0] = id1(i, j), e[ec].adj[0][1] = n * m + 1, e[ec].adj[1][0] = id2(i, j),
            e[ec].adj[1][1] = id2(i, j + 1);
            e[ec].w = kInf;
        }
        if (j == 1) {
            ++ec;
            e[ec].adj[0][0] = id1(i, j), e[ec].adj[0][1] = n * m + 1, e[ec].adj[1][0] = id2(i, j),
            e[ec].adj[1][1] = id2(i + 1, j);
            e[ec].w = kInf;
        }
    }
    For(i, (n + 1) * (m + 1) + 2, ec) if (e[i].w == 0) {
        if (!ta.addEdge(i))
            tb.addEdge(i);
    }
    For(i, (n + 1) * (m + 1) + 2, ec) if (e[i].w == kInf) {
        if (!ta.addEdge(i))
            tb.addEdge(i);
    }

    isStrict = true;
    For(i, 1, q) {
        char c = gt();
        int x, y, w;
        scanf("%d%d%d", &x, &y, &w);
        if (ol)
            decode(c, x, y, w);
        int cur, del;
        if (c == '-')
            cur = row[id1(x, y)];
        else
            cur = col[id1(x, y)];
        if (e[cur].w == w) {
            printf("%lld\n", lstans = ta.sum - kInf);
            continue;
        }
        if (e[cur].w > w) {
            if (!e[cur].on)
                ta.modify(cur, w) /*, fprintf(stderr, "case 1.1\n")*/;
            else {
                del = ta.getEdge(e[cur].adj[0][0], e[cur].adj[0][1]);
                if (e[del].w <= w)
                    tb.modify(cur, w) /*, fprintf(stderr, "case 1.2.1\n")*/;
                else {
                    // fprintf(stderr, "case 1.2.2 del = (%d,%d,%d,%d,%d)\n", del, e[del].adj[0][0],
                    // e[del].adj[0][1], e[del].w, e[del].on);
                    ta.cutEdge(del), tb.cutEdge(cur);
                    e[cur].w = w;
                    ta.addEdge(cur), tb.addEdge(del);
                }
            }
        } else {
            if (e[cur].on)
                tb.modify(cur, w) /*, fprintf(stderr, "case 2.1\n")*/;
            else {
                del = tb.getEdge(e[cur].adj[1][0], e[cur].adj[1][1]);
                if (e[del].w >= w)
                    ta.modify(cur, w) /*, fprintf(stderr, "case 2.2.1\n")*/;
                else {
                    // fprintf(stderr, "case 2.2.2 del = (%d,%d,%d,%d,%d)\n", del, e[del].adj[0][0],
                    // e[del].adj[0][1], e[del].w, e[del].on);
                    tb.cutEdge(del), ta.cutEdge(cur);
                    e[cur].w = w;
                    tb.addEdge(cur), ta.addEdge(del);
                }
            }
        }
        printf("%lld\n", lstans = ta.sum - kInf);
        show(ec);
    }
    return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */

federation

由矩阵树定理,答案是

\begin{vmatrix} \sum w & -w_1 & -w_2 & \cdots & -w_{n-1} \\ -w_{n-1} & \sum w & -w_1 & \cdots & -w_{n-2} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ -w_1 & -w_2 & -w_3 & \cdots & \sum w \end{vmatrix}

然后有一个循环矩阵求行列式的黑科技,答案就是第一行DFT后的积,见notes。

#include <bits/stdc++.h>

#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)

typedef long long ll;

using std::cin;
using std::cout;
using std::endl;

typedef std::pair<int, int> pii;
#define fi first
#define se second

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

template <typename T>
T myrand(T l, T r) {
    return std::uniform_int_distribution<T>(l, r)(rnd);
}

void close_stdio() {
    std::ios::sync_with_stdio(false);
    std::cerr << "DO NOT use scanf/printf!\n";
}

const int P = 998244353;
const int kK = 22;
const int kN = (1 << kK);

int k, n, w[kN];
int rev[kN], f[kN];

int norm(int x) { return x >= P ? x - P : x; }
ll qpow(ll b, int k) {
    ll res = 1;
    for (; k; k >>= 1, b = b * b % P)
        if (k & 1)
            res = res * b % P;
    return res;
}

int main() {
    freopen("federation.in", "r", stdin);
    freopen("federation.out", "w", stdout);
    scanf("%d", &k);
    n = 1 << k;
    For(i, 1, n - 1) scanf("%d", &w[i]);
    f[0] = 1;
    For(i, 1, n - 1) f[0] = norm(f[0] + w[i]);
    For(i, 1, n - 1) f[i] = norm(P - w[i]);
    For(i, 1, n - 1) {
        rev[i] = rev[i >> 1] >> 1;
        if (i & 1)
            rev[i] |= n >> 1;
        if (rev[i] < i)
            std::swap(f[i], f[rev[i]]);
    }
    for (int h = 2; h <= n; h <<= 1) {
        int wn = qpow(3, (P - 1) / h);
        for (int i = 0; i < n; i += h) {
            ll w = 1, t = 0;
            for (int j = 0; j < (h >> 1); ++j, w = w * wn % P) {
                t = w * f[i + j + (h >> 1)] % P;
                f[i + j + (h >> 1)] = norm(f[i + j] + P - t), f[i + j] = norm(f[i + j] + t);
            }
        }
    }
    ll ans = 1;
    For(i, 0, n - 1)(ans *= f[i]) %= P;
    printf("%lld", ans);
    return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */