---
id: 952
lang: en
translation_of: index.md
source_sha256: f507566821abc4e55f30ed62e12c69ac1e8a367a57a1f209a6f44fa59a9c933b
title: "[Fortuna OJ] Mar 7th - Group A Problem-Solving Report"
description: "Solutions to three problems: a plain bipartite matching, a hacky segment tree over a modified DFS order, and sorting plus binary search into three segments."
tags: ["OI","Hacky Heuristics","Binary Search","Segment Tree","Bipartite Matching","Bipartite Graph"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem titles 高维宇宙, 排队 and 心理学概论 were translated literally (High-Dimensional Universe, Queueing, Introduction to Psychology)."
  - "The author's coined term 乱搞序 (and 乱搞的线段树, 乱搞值, 乱搞思想) rendered with “hacky”; each 乱搞 is chipped with the glossary gloss."
  - "Hack in 欢迎来 Hack is standard CP terminology (hacking a submission), kept as “Hack” with no chip."
---

## A - High-Dimensional Universe

A <span class="slang" data-zh="裸" data-gloss="luǒ, “naked”. OI slang for plain, standard material with no twist on top.">bare</span> bipartite matching; just divide the matching size by two.

```cpp
// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100;
int head[MAX_N], n, current, matching[MAX_N], dfn[MAX_N], arr[MAX_N];
bool isPrime[2020];
struct edge
{
    int to, nxt;
} edges[(MAX_N * (MAX_N - 1)) >> 1];
void addpath(int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    head[src] = current++;
}
bool dfs(int u, int org)
{
    for (int i = head[u]; i != -1; i = edges[i].nxt)
    {
        int to = edges[i].to;
        if (dfn[to] != org)
        {
            dfn[to] = org;
            if ((!matching[to]) || dfs(matching[to], org))
            {
                matching[to] = u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    freopen("prime.in", "r", stdin);
    freopen("prime.out", "w", stdout);
    memset(head, -1, sizeof(head)), memset(isPrime, true, sizeof(isPrime));
    int ans = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &arr[i]);
    for (int i = 2; i < 2020; i++)
        if (isPrime[i])
            for (int j = 2; i * j < 2020; j++)
                isPrime[j * i] = false;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (isPrime[arr[i] + arr[j]])
                addpath(i, j);
    for (int i = 1; i <= n; i++)
        if (dfs(i, i))
            ans++;
    printf("%d", ans / 2);
    return 0;
}
```

## B - Queueing

I <span class="slang" data-zh="A 掉" data-gloss="“A” (short for AC, Accepted) + 掉 diào, “off”. OI slang for solving a problem, getting it accepted.">AC’d</span> this problem in the exam, with a method a bit different from the solution (feel free to Hack it), though of course the complexity may be a bit higher.

We consider a <span class="slang" data-zh="魔改" data-gloss="mógǎi, “magic modification”. Internet slang for modifying something heavily and unconventionally.">heavily modded</span> DFS order: sort before entering a node, and after sorting, mark the node once its DFS is finished. You can see that this is the optimal way of placing them when no nodes are deleted:

![Tree in Problem B, March 5th, 2019](media/graph-2.png)

As shown in the figure above, this is the best order of placement when we don't do any deleting: 4, 7, 3, 2, 6, 5, 1. We call this order the **<span class="slang" data-zh="乱搞" data-gloss="luàngǎo, “random messing”. Unproven heuristic or randomized tricks that happen to pass.">hacky</span> order**. Let's discuss using a kind of <span class="slang" data-zh="乱搞" data-gloss="luàngǎo, “random messing”. Unproven heuristic or randomized tricks that happen to pass.">hacky</span> segment tree (keyed by the order above) to handle these two operations.

In the case $OPT = 1$: first consider the case with no deletions before. It's actually very simple: just set the last slot with the smallest <span class="slang" data-zh="乱搞" data-gloss="luàngǎo, “random messing”. Unproven heuristic or randomized tricks that happen to pass.">hacky</span> order to one. If there were deletions before, then the existing <span class="slang" data-zh="乱搞" data-gloss="luàngǎo, “random messing”. Unproven heuristic or randomized tricks that happen to pass.">hacky</span> order is certainly not a contiguous run of 1s: there must be a 0 marking a deletion. We can check, inside the UPDATE operation, whether the left or right child fails to be contiguous (that is, whether the sum equals the interval length); if so, take some out of the addend, and if the addend has some left over, tag the left child directly and then recurse into the right child: because we set the return value of the UPDATE operation to be the last <span class="slang" data-zh="乱搞" data-gloss="luàngǎo, “random messing”. Unproven heuristic or randomized tricks that happen to pass.">hacky</span> value, which is then converted back into a node number through the anti array.

```cpp
int update(int l, int r, int p, int c)
{
    if (c == 0)
        return -1;
    if (l == r)
    {
        tree[p] += c;
        return l;
    }
    int lft = (mid - l + 1) - tree[lson], rig = c - lft;
    int ans = 0;
    if (rig > 0)
    {
        tree[lson] = mid - l + 1, lazy[lson] = 1;
        ans = update(mid + 1, r, rson, rig);
    }
    else
        ans = update(l, mid, lson, c);
    tree[p] = tree[lson] + tree[rson];
    return ans;
}
```

The case $OPT=2$ is also fairly simple: we just find the number of used nodes on the chain from this node to the root and subtract one, doing a point update.

I suggest reading the code carefully to understand my <span class="slang" data-zh="乱搞" data-gloss="luàngǎo, “random messing”. Unproven heuristic or randomized tricks that happen to pass.">hacky</span> thinking.

```cpp
// B.cpp
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lson (p << 1)
#define rson ((p << 1) | 1)
#define lowbit(x) (x & -x)
using namespace std;
const int MAX_N = 1e5 + 2000;
int n, t, dfn[MAX_N], st[20][MAX_N], tmpx, tmpy, tree[MAX_N << 2], lazy[MAX_N << 2], dtot, anti[MAX_N];
vector<int> G[MAX_N];
void addpath(int src, int dst) { G[src].push_back(dst); }
void pushdown(int p, int l, int r)
{
    if (lazy[p] && l != r)
    {
        tree[lson] = mid - l + 1, tree[rson] = r - mid;
        lazy[lson] = 1, lazy[rson] = 1;
    }
    lazy[p] = 0;
}
int update(int l, int r, int p, int c)
{
    if (c == 0)
        return -1;
    if (l == r)
    {
        tree[p] += c;
        return l;
    }
    int lft = (mid - l + 1) - tree[lson], rig = c - lft;
    int ans = 0;
    if (rig > 0)
    {
        tree[lson] = mid - l + 1, lazy[lson] = 1;
        ans = update(mid + 1, r, rson, rig);
    }
    else
        ans = update(l, mid, lson, c);
    tree[p] = tree[lson] + tree[rson];
    return ans;
}
void remove(int qx, int l, int r, int p)
{
    if (l == r && l == qx)
    {
        tree[p] = 0, lazy[p] = 0;
        return;
    }
    pushdown(p, l, r);
    if (qx <= mid)
        remove(qx, l, mid, lson);
    else
        remove(qx, mid + 1, r, rson);
    tree[p] = tree[lson] + tree[rson];
}
bool query(int qx, int l, int r, int p)
{
    if (l == r && l == qx)
        return tree[p];
    pushdown(p, l, r);
    if (qx <= mid)
        return query(qx, l, mid, lson);
    else
        return query(qx, mid + 1, r, rson);
}
void dfs(int u)
{
    sort(G[u].begin(), G[u].end());
    int siz = G[u].size();
    for (int i = 0; i < siz; i++)
    {
        int to = G[u][i];
        if (st[0][u] != to)
            st[0][to] = u, dfs(to);
    }
    dfn[u] = ++dtot, anti[dfn[u]] = u;
}
int main()
{
    scanf("%d%d", &n, &t);
    for (int i = 1; i <= n - 1; i++)
        scanf("%d%d", &tmpx, &tmpy), addpath(tmpx, tmpy), addpath(tmpy, tmpx);
    dfs(1);
    for (int i = 1; i < 20; i++)
        for (int j = 1; j <= n; j++)
            st[i][j] = st[i - 1][st[i - 1][j]];
    while (t--)
    {
        int opt, x;
        scanf("%d%d", &opt, &x);
        if (opt == 1)
            printf("%d\n", anti[update(1, n, 1, x)]);
        else
        {
            int u = x, ans = 0;
            for (int i = 19; i >= 0; i--)
                if (st[i][u] != 0 && query(dfn[st[i][u]], 1, n, 1) == true)
                    u = st[i][u], ans += 1 << i;
            printf("%d\n", ans);
            remove(dfn[u], 1, n, 1);
        }
    }
    return 0;
}
```

## C - Introduction to Psychology

In the exam I thought it was min-cost flow, coded it up, and <span class="slang" data-zh="凉掉了" data-gloss="liáng diào le, “went cold”. Internet slang for something failing or dying out.">that went cold</span>.

We can first sort the sequence (by the first, second and third keys), then split it into segments: binary search for two positions $mid,mid1$, then split into three segments: $[1,mid],[mid+1,mid1],[mid1+1,n]$

Then just count it up.

```cpp
// C.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1e5 + 1000;
struct cas
{
    ll x, y, z;
    bool operator<(const cas &cs) const { return x < cs.x || (cs.x == x && y < cs.y) || (y == cs.y && x == cs.x && z < cs.z); }
} cases[MAX_N];
ll n, ans1, ans2, ans3, ans;
int main()
{
    freopen("psy.in", "r", stdin);
    freopen("psy.out", "w", stdout);
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld%lld%lld", &cases[i].x, &cases[i].y, &cases[i].z);
        ans1 = max(ans1, cases[i].x), ans2 = max(ans2, cases[i].y), ans3 = max(ans3, cases[i].z);
    }
    ans = min(ans1, min(ans2, ans3));
    sort(cases + 1, cases + 1 + n);
    ll l = 0, r = n, l1 = 0, r1 = n;
    while (l < r)
    {
        ll mid = (l + r) >> 1;
        l1 = 0, r1 = n;
        bool flag = true;
        while (l1 < r1)
        {
            ll mid1 = (l1 + r1) >> 1;
            if (mid1 == mid)
                if (mid1 + 1 <= r)
                    mid1++;
                else if (mid1 - 1 >= l)
                    mid1--;
                else
                {
                    flag = false;
                    break;
                }
            ll k = 0;
            for (int i = 1; i <= n; i++)
            {
                if (cases[i].x <= cases[mid].x)
                    continue;
                if (cases[i].y <= cases[mid1].y)
                    continue;
                if (cases[i].z > k)
                    k = cases[i].z;
            }
            ans1 = cases[mid].x + cases[mid1].y + k;
            if (ans1 < ans)
                ans = ans1, flag = true, r1 = mid1;
            else
                l1 = mid1 + 1;
        }
        if (flag)
            r = mid;
        else
            l = mid + 1;
    }
    ll k = 0;
    for (int i = 1; i <= n; i++)
    {
        if (cases[i].x <= cases[l].x)
            continue;
        if (cases[i].y <= cases[l1].y)
            continue;
        if (cases[i].z > k)
            k = cases[i].z;
    }
    ans = min(ans, cases[l].x + cases[l1].y + k);
    printf("%lld", ans);
    return 0;
}
```
