BJOI2019

· · 题解

BJOI2019

题目按照主观难度排序。

P5322 [BJOI2019] 排兵布阵

容易发现对一个城堡派出的士兵数量一定能够刚好击败某个玩家,直接背包即可。

#include <bits/stdc++.h>

#define MAXN 110

#define MAXM 20010

using namespace std;



int s, n, m;

int a[MAXN][MAXN];

int dp[MAXM];



int main()

{

    freopen("battle.in", "r", stdin);

    freopen("battle.out", "w", stdout);

    ios::sync_with_stdio(false);

    cin.tie(0), cout.tie(0);

    cin >> s >> n >> m;

    for (int i = 1; i <= s; ++i) for (int j = 1; j <= n; ++j) cin >> a[j][i], a[j][i] = a[j][i] << 1 | 1;

    for (int i = 1; i <= n; ++i) sort(a[i] + 1, a[i] + s + 1);

    for (int i = 1; i <= n; ++i) for (int k = m; k >= 0; --k) for (int j = 1; j <= s && a[i][j] + k <= m; ++j) {

        dp[k + a[i][j]] = max(dp[k + a[i][j]], dp[k] + i * j);

    }

    cout << dp[m] << "\n";

    return 0;

}

P5323 [BJOI2019] 光线

令 f_i 表示前 i 面镜子的透光率,g_i 表示前 i 面镜子的从反方向射入的反射率。

枚举 j 表示光在第 i-1 面镜子和第 i 面镜子间反射的次数,则有:

f_i=f_{i-1}a_i\sum\limits_{j=0}^{\infty}b_ig_{i-1}=\dfrac{f_{i-1}a_i}{1-b_ig_{i-1}} g_i=b_i+a_i^2g_{i-1}\sum\limits_{j=0}^{\infty}b_ig_{i-1}=b_i+\dfrac{a_i^2g_{i-1}}{1-b_ig_{i-1}}

初值为 f_0=1,可以直接 dp。

#include <bits/stdc++.h>

#define MAXN 500010

using namespace std;



int n;

long long i100;

long long a[MAXN], b[MAXN];

long long f[MAXN], g[MAXN];

const long long mod = 1e9 + 7;



long long fastpow(long long a, long long b) {

    long long ans = 1;

    while (b) {

        (b & 1) && (ans = ans * a % mod);

        a = a * a % mod, b >>= 1;

    }

    return ans;

}



int main()

{

    freopen("light.in", "r", stdin);

    freopen("light.out", "w", stdout);

    ios::sync_with_stdio(false);

    cin.tie(0), cout.tie(0);

    i100 = fastpow(100, mod - 2);

    cin >> n;

    for (int i = 1; i <= n; ++i) {

        cin >> a[i] >> b[i];

        a[i] = a[i] * i100 % mod;

        b[i] = b[i] * i100 % mod;

    }

    f[0] = 1;

    for (int i = 1; i <= n; ++i) {

        f[i] = f[i - 1] * a[i] % mod * fastpow(mod + 1 - b[i] * g[i - 1] % mod, mod - 2) % mod;

        g[i] = (b[i] + a[i] * a[i] % mod * g[i - 1] % mod * fastpow(mod + 1 - b[i] * g[i - 1] % mod, mod - 2)) % mod;

    }

    cout << f[n] << "\n";

    return 0;

}

P5319 [BJOI2019] 奥术神杖

将每个串的贡献取自然对数,就能把几何平均数最大变成算数平均数最大。

求最大平均数,考虑二分,将贡献减去二分的平均数,并判断答案是否大于零。

建出 AC 自动机,预处理每个位置的贡献,在 AC 自动机上跑 dp 即可。

#include <bits/stdc++.h>

#define MAXN 1510

using namespace std;



int ac_tot;



struct AC{

    int son[10];

    int fail;

    double val, cnt;

}tree[MAXN];



vector<int> ed[MAXN];



int Insert(string st) {

    int p = 0;

    for (char ch : st) {

        int &pos = tree[p].son[ch - '0'];

        if (!pos) pos = ac_tot++;

        p = pos;

    }

    return p;

}



void dfs(const int p) {

    for (int q : ed[p]) tree[q].val += tree[p].val, tree[q].cnt += tree[p].cnt, dfs(q);

    return;

}



void Build_AC() {

    queue<int> q;

    while (!q.empty()) q.pop();

    for (int i = 0; i < 10; ++i) if (tree[0].son[i]) q.push(tree[0].son[i]);

    while (!q.empty()) {

        int now = q.front(); q.pop();

        for (int i = 0; i < 10; ++i) {

            if (tree[now].son[i]) tree[tree[now].son[i]].fail = tree[tree[now].fail].son[i], q.push(tree[now].son[i]);

            else tree[now].son[i] = tree[tree[now].fail].son[i];

        }

    }

    for (int i = 1; i < ac_tot; ++i) ed[tree[i].fail].push_back(i);

    dfs(0);

    return;

}



int n, m;

string S, T;

char ans[MAXN];

double l, r, mid;

double dp[MAXN][MAXN];

int from[MAXN][MAXN], fromc[MAXN][MAXN];

const double eps = 1e-10;



bool check() {

    for (int i = 0; i <= n; ++i) for (int j = 0; j < ac_tot; ++j) dp[i][j] = -1e9;

    dp[0][0] = 0;

    for (int i = 1; i <= n; ++i) for (int j = 0; j < ac_tot; ++j) if (dp[i - 1][j] > -1e9) {

        if (S[i - 1] == '.') {

            for (int c = 0; c < 10; ++c) {

                int to = tree[j].son[c];

                dp[i][to] = max(dp[i][to], dp[i - 1][j] + tree[to].val - mid * tree[to].cnt);

            }

        }

        else {

            int to = tree[j].son[S[i - 1] - '0'];

            dp[i][to] = max(dp[i][to], dp[i - 1][j] + tree[to].val - mid * tree[to].cnt);

        }

    }

    for (int i = 0; i <= ac_tot; ++i) if (dp[n][i] > 0) return true;

    return false;

}



int Getans() {

    for (int i = 0; i <= n; ++i) for (int j = 0; j < ac_tot; ++j) dp[i][j] = -1e9;

    dp[0][0] = 0;

    for (int i = 1; i <= n; ++i) for (int j = 0; j < ac_tot; ++j) if (dp[i - 1][j] > -1e9) {

        if (S[i - 1] == '.') {

            for (int c = 0; c < 10; ++c) {

                int to = tree[j].son[c];

                double val = dp[i - 1][j] + tree[to].val - mid * tree[to].cnt;

                if (dp[i][to] < val) {

                    dp[i][to] = val;

                    from[i][to] = j, fromc[i][to] = c;

                }

            }

        }

        else {

            int to = tree[j].son[S[i - 1] - '0'];

            double val = dp[i - 1][j] + tree[to].val - mid * tree[to].cnt;

            if (dp[i][to] < val) {

                dp[i][to] = val;

                from[i][to] = j, fromc[i][to] = S[i - 1] - '0';

            }

        }

    }

    for (int i = 0; i < ac_tot; ++i) if (dp[n][i] > 0) return i;

    cout << "Error!";

    return 0;

}



int main()

{

    freopen("arcana.in", "r", stdin);

    freopen("arcana.out", "w", stdout);

    ios::sync_with_stdio(false);

    cin.tie(0), cout.tie(0);

    cin >> n >> m; ac_tot = 1;

    cin >> S;

    for (int i = 1; i <= m; ++i) {

        cin >> T;

        double val; cin >> val;

        int pos = Insert(T);

        tree[pos].val += log(val), ++tree[pos].cnt;

    }

    Build_AC();

    l = 0, r = 30;

    while (r - l > eps) {

        mid = (l + r) / 2;

        if (check()) l = mid;

        else r = mid;

    }

    mid = l;

    int p = Getans();

    for (int i = n; i; --i) {

        ans[i - 1] = fromc[i][p] + '0';

        p = from[i][p];

    }

    cout << ans << "\n";

    return 0;

}

P5324 [BJOI2019] 删数

唯一一道需要脑子的题。

条件可以转换为:对于每一个出现的数,比他小的数的个数刚好等于它本身。

或者说,将原序列排序,则对于 1 到 n,一定满足 a_i=i 或 a_i=a_{i+1}。

考虑快速求出一个序列的答案。将每个出现过的 b 固定到第 b 位上,重复出现的 b 不断想左延展,则 b 会覆盖右端点位置为 b,长度为 b 出现次数的一段区间。

显然,如果这些区间不重不漏的覆盖了 1 到 n,则这个序列直接就是合法的;否则,我们将重复覆盖的位置填补到空缺的位置,序列就合法了。

于是,答案等于 1 到 n 中空位的数量。

对于单点修改的操作,相当于修改两个数的出现次数,就是两次单点修改。

对于全局修改的操作,可以维护偏移量并对询问区间进行偏移。

有一个重要的细节:对于 b>n,b 所在的区间不能被统计进答案中,需要在全局修改时处理贡献。

所以我们需要一个线段树,能够维护区间加减和求区间中零的数量。

发现要维护的序列中的值一定不小于零,那么可以直接维护区间最小值和最小值的出现次数,在最小值为零的时候贡献进答案。

#include <bits/stdc++.h>

#define MAXN 150010

using namespace std;



struct SegTree{

    int l, r;

    int val, cnt;

    int tag;

}tree[MAXN << 4];



void Change(const int p, const int val) {

    tree[p].val += val, tree[p].tag += val;

    return;

}



void Pushdown(const int p) {

    if (tree[p].l == tree[p].r || !tree[p].tag) return;

    Change(p << 1, tree[p].tag);

    Change(p << 1 | 1, tree[p].tag);

    tree[p].tag = 0;

    return;

}



void Pushup(const int p) {

    if (tree[p].l == tree[p].r) return;

    tree[p].val = min(tree[p << 1].val, tree[p << 1 | 1].val);

    tree[p].cnt = 0;

    if (tree[p << 1].val == tree[p].val) tree[p].cnt += tree[p << 1].cnt;

    if (tree[p << 1 | 1].val == tree[p].val) tree[p].cnt += tree[p << 1 | 1].cnt;

    return;

}



void Build(const int p, const int l, const int r) {

    tree[p].l = l, tree[p].r = r;

    if (l == r) {tree[p].cnt = 1; return;}

    int mid = (l + r) >> 1;

    Build(p << 1, l, mid);

    Build(p << 1 | 1, mid + 1, r);

    Pushup(p);

    return;

}



void Modify(const int p, const int pos, const int val) {

    if (tree[p].l == tree[p].r) return Change(p, val);

    int mid = (tree[p].l + tree[p].r) >> 1;

    Pushdown(p);

    if (pos <= mid) Modify(p << 1, pos, val);

    else Modify(p << 1 | 1, pos, val);

    Pushup(p);

    return;

}



void Seg_Modify(const int p, const int l, const int r, const int val) {

    if (l > r) return;

    if (tree[p].l >= l && tree[p].r <= r) return Change(p, val);

    int mid = (tree[p].l + tree[p].r) >> 1;

    Pushdown(p);

    if (l <= mid) Seg_Modify(p << 1, l, r, val);

    if (mid < r) Seg_Modify(p << 1 | 1, l, r, val);

    Pushup(p);

    return;

}



int Query(const int p, const int l, const int r) {

    if (tree[p].l >= l && tree[p].r <= r) return tree[p].val == 0 ? tree[p].cnt : 0;

    int ans = 0, mid = (tree[p].l + tree[p].r) >> 1;

    Pushdown(p);

    if (l <= mid) ans += Query(p << 1, l, r);

    if (mid < r) ans += Query(p << 1 | 1, l, r);

    return ans;

}



void DEBUG(const int p) {

    Pushdown(p);

    if (tree[p].l == tree[p].r) cout << tree[p].l << " " << tree[p].r << " " << tree[p].val << " " << tree[p].cnt << "\n";

    if (tree[p].l < tree[p].r) DEBUG(p << 1), DEBUG(p << 1 | 1);

    return;

}



int n, m, delta;

int a[MAXN], cnt[MAXN << 2], s[MAXN << 2];



int main()

{

    freopen("del.in", "r", stdin);

    freopen("del.out", "w", stdout);

    ios::sync_with_stdio(false);

    cin.tie(0), cout.tie(0);

    cin >> n >> m;

    for (int i = 1; i <= n; ++i) cin >> a[i], ++cnt[a[i] + m];

    for (int i = 1; i <= n; ++i) ++s[i - cnt[i + m] + n + 1], --s[i + n + 1];

    Build(1, -n - m, n + m);

    for (int i = 1; i <= (n << 1); ++i) {

        s[i] += s[i - 1];

        Modify(1, i - n, s[i]);

    }

    for (int i = 1; i <= m; ++i) {

        int pos; cin >> pos;

        if (pos) {

            int val;

            cin >> val; val -= delta;

            if (a[pos] <= n - delta) Modify(1, a[pos] - cnt[a[pos] + m] + 1, -1);

            --cnt[a[pos] + m], a[pos] = val, ++cnt[a[pos] + m];

            if (a[pos] <= n - delta) Modify(1, a[pos] - cnt[a[pos] + m] + 1, 1);

        }

        else {

            int val; cin >> val;

            if (val == 1) Seg_Modify(1, n - delta - cnt[n + m - delta] + 1, n - delta, -1), ++delta;

            else --delta, Seg_Modify(1, n - delta - cnt[n + m - delta] + 1, n - delta, 1);

        }

        // for (int i = 1; i <= n; ++i) cout << a[i] << " ";

        // cout << "\n";

        cout << Query(1, 1 - delta, n - delta) << "\n";

        // DEBUG(1);

    }

    return 0;

}

P5320 [BJOI2019] 勘破神机

好题,我懂得欣赏。

暴力算出前 5000 位后直接 BM。

不用写多项式,暴力卷积取模就行。

#include <bits/stdc++.h>

#define MAXN 10010

using namespace std;

const long long mod = 998244353;



long long fastpow(long long a, long long b) {

    long long ans = 1;

    while (b) {

        (b & 1) && (ans = ans * a % mod);

        a = a * a % mod, b >>= 1;

    }

    return ans;

}



void BM(const int n, const long long *a, vector<long long> &ans) {

    vector<long long> las;

    int w = 0; long long dt = 0;

    ans.clear(), las.clear();

    for (int i = 1; i <= n; ++i) {

        long long tmp = 0;

        for (int j = 0; j < (int)ans.size(); ++j) tmp = (tmp + a[i - j - 1] * ans[j]) % mod;

        if ((a[i] - tmp) % mod == 0) continue;

        if (!w) {

            w = i, dt = (a[i] - tmp) % mod;

            for (int j = 0; j < i; ++j) ans.push_back(0);

            continue;

        }

        vector<long long> now = ans;

        long long mul = (a[i] - tmp) * fastpow(dt, mod - 2) % mod;

        if ((int)ans.size() < (int)las.size() + i - w) ans.resize((int)las.size() + i - w);

        ans[i - w - 1] = (ans[i - w - 1] + mul) % mod;

        for (int j = 0; j < (int)las.size(); ++j) ans[i + j - w] = (ans[i + j - w] - las[j] * mul) % mod;

        if ((int)now.size() - i < (int)las.size() - w) las = now, w = i, dt = (a[i] - tmp) % mod;

    }

    for (long long &val : ans) val = (val + mod) % mod;

    return;

}



void polymul(vector<long long> &f, vector<long long> g, vector<long long> h) {

    int n = g.size() - 1, m = h.size() - 1;

    f.clear();

    for (int i = 0; i <= n + m; ++i) f.push_back(0);

    for (int i = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) f[i + j] = (f[i + j] + g[i] * h[j]) % mod;

    return;

}



void polymod(vector<long long> &f, vector<long long> &g) {

    int n = f.size() - 1, m = g.size() - 1;

    long long inv = fastpow(g[m], mod - 2);

    for (int i = n; i >= m; --i) {

        long long val = inv * f[i];

        for (int j = m; j >= 0; --j) f[i - m + j] = (f[i - m + j] - g[j] * val) % mod;

    }

    for (int i = 0; i <= n; ++i) f[i] = (f[i] + mod) % mod;

    while (n >= 0 && !f[n]) --n, f.pop_back();

    return;

}



void polypow(vector<long long> &ans, vector<long long> &g, long long b) {

    vector<long long> a;

    ans.clear(), ans.push_back(1);

    a.clear(), a.push_back(0), a.push_back(1);

    while (b) {

        if (b & 1) {

            polymul(ans, ans, a);

            polymod(ans, g);

        }

        b >>= 1;

        polymul(a, a, a);

        polymod(a, g);

    }

    return;

}



long long getans(const long long *a, vector<long long> ans, const long long r) {

    vector<long long> f, g;

    long long res = 0;

    f.clear(), g.clear();

    for (int i = (int)ans.size() - 1; i >= 0; --i) {

        g.push_back((mod - ans[i]) % mod);

    }

    g.push_back(1);

    polypow(f, g, r);

    for (int i = 0; i < (int)f.size(); ++i) res = (res + a[i + 1] * f[i]) % mod;

    return res;

}



int T, m, lim, k;

long long l, r, a[MAXN];

long long f[MAXN], ifac;

long long ans;

vector<long long> res;



long long C(const long long val) {

    long long ans = 1;

    for (int i = 0; i < k; ++i) ans = ans * (val - i) % mod;

    return ans * ifac % mod;

}



int main()

{

    freopen("sum.in", "r", stdin);

    freopen("sum.out", "w", stdout);

    ios::sync_with_stdio(false);

    cin.tie(0), cout.tie(0);

    cin >> T >> m;

    while (T--) {

        cin >> l >> r >> k;

        lim = 5000, ifac = 1;

        for (int i = 1; i <= k; ++i) ifac = ifac * i % mod;

        ifac = fastpow(ifac, mod - 2);

        if (m == 2) {

            f[0] = f[1] = 1;

            for (int i = 2; i < lim; ++i) f[i] = (f[i - 1] + f[i - 2]) % mod;

            for (int i = 0; i < lim; ++i) a[i + 1] = (C(f[i]) + a[i]) % mod;

            BM(lim, a, res);

            ans = (getans(a, res, r) - getans(a, res, l - 1) + mod) * fastpow((r - l + 1) % mod, mod - 2) % mod;

            cout << ans << "\n";

        }

        else {

            f[0] = 1, f[1] = 3;

            for (int i = 2; i < lim; ++i) f[i] = (4 * f[i - 1] - f[i - 2] + mod) % mod;

            for (int i = 0; i < lim; ++i) a[i + 1] = (C(f[i]) + a[i]) % mod;

            BM(lim, a, res);

            ans = (getans(a, res, r / 2) - getans(a, res, (l - 1) / 2) + mod) * fastpow((r - l + 1) % mod, mod - 2) % mod;

            cout << ans << "\n";

        }

    }

    return 0;

}

P5321 [BJOI2019] 送别

呃呃呃。

把一堵墙拆成两个方向相反的边,则会形成若干个环。

用可分裂合并的平衡树维护每个环,我用的 FHQ-Treap。

维护比较好想,但不好实现。

代码巨大难写难调。

#include <bits/stdc++.h>

#define MAXN 1002010

#define MAXL 510

using namespace std;



int tx, ty, tz;

mt19937 myrand(time(0) ^ *new int);



int n, m, q, tot;

bool vis[2][MAXL][MAXL];



struct FHQTreap{

    int fa, lson, rson, siz;

    unsigned int rnd;

}tree[MAXN];



void Pushup(const int p) {

    if (tree[p].lson) tree[tree[p].lson].fa = p;

    if (tree[p].rson) tree[tree[p].rson].fa = p;

    tree[p].siz = tree[tree[p].lson].siz + tree[tree[p].rson].siz + 1;

    return;

}



void Split_(const int p, const int tar, int &lt, int &rt) {

    if (!p) {lt = rt = 0; return;}

    if (tree[tree[p].lson].siz < tar) lt = p, Split_(tree[p].rson, tar - tree[tree[p].lson].siz - 1, tree[p].rson, rt);

    else rt = p, Split_(tree[p].lson, tar, lt, tree[p].lson);

    Pushup(p);

    return;

}



int Merge_(const int lt, const int rt) {

    if (!lt || !rt) return lt | rt;

    if (tree[lt].rnd < tree[rt].rnd) {

        tree[lt].rson = Merge_(tree[lt].rson, rt);

        Pushup(lt);

        return lt;

    }

    tree[rt].lson = Merge_(lt, tree[rt].lson);

    Pushup(rt);

    return rt;

}



void Split(const int p, const int tar, int &lt, int &rt) {

    Split_(p, tar, lt, rt);

    tree[lt].fa = tree[rt].fa = 0;

    return;

}



int Merge(const int lt, const int rt) {

    int root = Merge_(lt, rt);

    tree[root].fa = 0;

    return root;

}



int Getrk(int p) {

    int ans = 1;

    if (tree[p].lson) ans += tree[tree[p].lson].siz;

    while (tree[p].fa) {

        if (tree[tree[p].fa].rson == p) ans += tree[tree[tree[p].fa].lson].siz + 1;

        p = tree[p].fa;

    }

    return ans;

}



int Getrt(int p) {

    while (tree[p].fa) p = tree[p].fa;

    return p;

}



int Getid(bool dir, int x, int y) {

    if (dir) return (x * (m + 1) + y + (n + 1) * m) << 1 | 1;

    return (x * m + y) << 1 | 1;

}



bool DEBUGvis[MAXN];



void DEBUG_dfs(const int p) {

    DEBUGvis[p] = 1;

    if (tree[p].lson) DEBUG_dfs(tree[p].lson);

    cout << p << " ";

    for (int i = 0; i <= n; ++i) for (int j = 0; j < m; ++j) if (Getid(0, i, j) == p) cout << "{" << 0 << " " << i << " " << j << " " << 0 << "} ";

    for (int i = 0; i <= n; ++i) for (int j = 0; j < m; ++j) if (Getid(0, i, j) + 1 == p) cout << "{" << 0 << " " << i << " " << j << " " << 1 << "} ";

    for (int i = 0; i < n; ++i) for (int j = 0; j <= m; ++j) if (Getid(1, i, j) == p) cout << "{" << 1 << " " << i << " " << j << " " << 0 << "} ";

    for (int i = 0; i < n; ++i) for (int j = 0; j <= m; ++j) if (Getid(1, i, j) + 1 == p) cout << "{" << 1 << " " << i << " " << j << " " << 1 << "} ";

    if (tree[p].rson) DEBUG_dfs(tree[p].rson);

    return;

}



void DEBUG() {

    memset(DEBUGvis, 0, sizeof(bool) * (tot + 1));

    cout << "---------------------DEBUG---------------------\n";

    // for (int i = 1; i <= tot; ++i) cout << i << " " << tree[i].fa << " " << tree[i].lson << " " << tree[i].rson << " " << tree[i].siz << "\n";

    for (int i = 1; i <= tot; ++i) if (!DEBUGvis[i] && !tree[i].fa && tree[i].siz > 1) cout << "(" << tree[i].siz << ") ", DEBUG_dfs(i), cout << "\n";

    cout << "---------------------DEBUG---------------------\n";

    return;

}



void Rotate(int p) {

    int rk = Getrk(p), rt = Getrt(p);

    if (rk == tree[rt].siz) return;

    Split(rt, rk, tx, ty);

    Merge(ty, tx);

    return;

}



void Insert(int x0, int y0, int x1, int y1) {

    if (x0 + y0 > x1 + y1) swap(x0, x1), swap(y0, y1);

    vis[x0 < x1][x0][y0] = 1;

    int nowid = Getid(x0 < x1, x0, y0);

    int ubl = 0, dbl = 0;

    if (x0 < x1) {

        if (y0 && vis[0][x0][y0 - 1]) ubl = Getid(0, x0, y0 - 1) + 1;

        else if (x0 && vis[1][x0 - 1][y0]) ubl = Getid(1, x0 - 1, y0);

        else if (vis[0][x0][y0]) ubl = Getid(0, x0, y0);

        if (vis[0][x0 + 1][y0]) dbl = Getid(0, x0 + 1, y0);

        else if (vis[1][x0 + 1][y0]) dbl = Getid(1, x0 + 1, y0) + 1;

        else if (y0 && vis[0][x0 + 1][y0 - 1]) dbl = Getid(0, x0 + 1, y0 - 1) + 1;

    }

    else {

        if (x0 && vis[1][x0 - 1][y0 + 1]) ubl = Getid(1, x0 - 1, y0 + 1);

        else if (vis[0][x0][y0 + 1]) ubl = Getid(0, x0, y0 + 1);

        else if (vis[1][x0][y0 + 1]) ubl = Getid(1, x0, y0 + 1) + 1;

        if (vis[1][x0][y0]) dbl = Getid(1, x0, y0) + 1;

        else if (y0 && vis[0][x0][y0 - 1]) dbl = Getid(0, x0, y0 - 1) + 1;

        else if (x0 && vis[1][x0 - 1][y0]) dbl = Getid(1, x0 - 1, y0);

    }

    if (ubl) {

        if (dbl) {

            int ublrt = Getrt(ubl), dblrt = Getrt(dbl);

            if (ublrt == dblrt) {

                Rotate(ubl), ublrt = Getrt(ubl);

                Split(Getrt(ubl), Getrk(dbl), tx, ty), Merge(tx, nowid + 1), Merge(ty, nowid);

            }

            else {

                Rotate(ubl), ublrt = Getrt(ubl);

                Rotate(dbl), dblrt = Getrt(dbl);

                Merge(Merge(dblrt, nowid + 1), Merge(ublrt, nowid));

            }

        }

        else Rotate(ubl), Merge(Getrt(ubl), Merge(nowid, nowid + 1));

    }

    else {

        if (dbl) Rotate(dbl), Merge(Getrt(dbl), Merge(nowid + 1, nowid));

        else Merge(nowid, nowid + 1);

    }

    return;

}



void Delete(int x0, int y0, int x1, int y1) {

    if (x0 + y0 > x1 + y1) swap(x0, x1), swap(y0, y1);

    int p = Getid(x0 < x1, x0, y0), q = p + 1;

    vis[x0 < x1][x0][y0] = 0;

    if (Getrt(p) == Getrt(q)) {

        Rotate(p);

        Split(Getrt(q), Getrk(q) - 1, tx, ty);

        Split(ty, 1, ty, tz);

        Split(tz, tree[tz].siz - 1, tx, ty);

    }

    else {

        Rotate(p), Rotate(q);

        int prt = Getrt(p), qrt = Getrt(q);

        Split(prt, tree[prt].siz - 1, tx, tz);

        Split(qrt, tree[qrt].siz - 1, ty, tz);

        Merge(tx, ty);

    }

    return;

}



int Query(int x0, int y0, int x1, int y1, int d0, int x2, int y2, int x3, int y3, int d1) {

    if (x0 + y0 > x1 + y1) swap(x0, x1), swap(y0, y1);

    if (x2 + y2 > x3 + y3) swap(x2, x3), swap(y2, y3);

    int p = Getid(x0 < x1, x0, y0) + d0, prt = Getrt(p);

    int q = Getid(x2 < x3, x2, y2) + d1, qrt = Getrt(q);

    if (prt != qrt) return -1;

    int dis = Getrk(q) - Getrk(p);

    if (dis < 0) dis += tree[prt].siz;

    return dis;

}



int main()

{

    freopen("farewell.in", "r", stdin);

    freopen("farewell.out", "w", stdout);

    ios::sync_with_stdio(false);

    cin.tie(0), cout.tie(0);

    cin >> n >> m >> q;

    tot = Getid(1, n - 1, m) + 1;

    for (int i = 1; i <= tot; ++i) tree[i].rnd = myrand(), tree[i].siz = 1;

    for (int i = 0; i < n; ++i) Insert(i, 0, i + 1, 0), Insert(i, m, i + 1, m);

    for (int i = 0; i < m; ++i) Insert(0, i, 0, i + 1), Insert(n, i, n, i + 1);

    for (int i = 0; i < n; ++i) for (int j = 1; j < m; ++j) {

        bool in; cin >> in;

        if (in) Insert(i, j, i + 1, j);

    }

    for (int i = 1; i < n; ++i) for (int j = 0; j < m; ++j) {

        bool in; cin >> in;

        if (in) Insert(i, j, i, j + 1);

    }

    while (q--) {

        int op; cin >> op;

        if (op == 1) {

            int x0, y0, x1, y1;

            cin >> x0 >> y0 >> x1 >> y1;

            Insert(x0, y0, x1, y1);

        }

        else if (op == 2) {

            int x0, y0, x1, y1;

            cin >> x0 >> y0 >> x1 >> y1;

            Delete(x0, y0, x1, y1);

        }

        else {

            int x0, y0, x1, y1, d0, x2, y2, x3, y3, d1;

            cin >> x0 >> y0 >> x1 >> y1 >> d0 >> x2>> y2 >> x3 >> y3 >> d1;

            cout << Query(x0, y0, x1, y1, d0, x2, y2, x3, y3, d1) << "\n";

        }

    }

    // DEBUG();

    return 0;

}

稍微帮助我提升了下代码能力。嗯。