---
id: 929
lang: en
translation_of: index.md
source_sha256: 794fcde3c8ddbb354b8f60d449b6a8400b11132c2706bd0fadc7b19911a8117c
title: "[Fortuna OJ] Mar 5th - Group A Problem-Solving Report"
description: "Solutions to three problems: offline Mo's algorithm for a weighted interval mode, SCC condensation plus DP on a DAG, and a median-based two-pointer sweep."
tags: ["OI","DP","DAG","Brute Force","Strongly Connected Components","Tarjan","SCC Condensation","Sqrt Decomposition","Median","Mo's Algorithm"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem titles 旷野大计算, 爬山 and 运输妹子 were translated literally; 货仓选址 (a classic problem name) and 妹子 got chips."
  - "这两玩意本质一样 (these two things are essentially the same) was kept in the quote as “the two things … are essentially the same”."
---

## A - Great Calculation in the Wilds

Um, it's the kind of problem a <span class="slang" data-zh="菜鸡" data-gloss="càijī, “vegetable chicken”. Slang for a noob, someone who is bad at something; self-deprecating here.">scrub</span> like me can't do.

The main idea for this problem is to use offline Mo's algorithm. This is my first time ever coding this algorithm, so I'll try to explain it clearly, one piece at a time. First, a rough analysis: the essence of this problem is really just querying the maximum weighted mode of an interval. Then, according to the solution, there is a conclusion like this:

> Given sets $A,B$, the two things $mode(A \cup B) \to mode(A) \cup B$ are essentially the same.

So we can consider a Mo's algorithm that only grows the interval. First, after coordinate compression, simply split the array into blocks, take the queries offline, sort them, and mess around with it. For each query, if it can be transitioned from the previous interval, do the fast transition; otherwise, start over.

The essence of Mo's algorithm is: do the transitions by brute force.

```cpp
// A.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1e6 + 2000;
int n, m, arr[MAX_N], blockId[MAX_N];
int currentId[MAX_N], bucket[MAX_N];
ll cnt[MAX_N], leftBucket[MAX_N], answer, anses[MAX_N];
struct query
{
    int l, r, id;
    bool operator<(const query &qu) const
    {
        return blockId[l] < blockId[qu.l] ||
               (blockId[l] == blockId[qu.l] && r < qu.r);
    }
} queries[MAX_N];
void update_left(int x)
{
    leftBucket[currentId[x]]++;
    answer = max(answer, (leftBucket[currentId[x]] + cnt[currentId[x]]) * 1LL * arr[x]);
}
int main()
{
    scanf("%d%d", &n, &m);
    int siz = sqrt(n * 1.0);
    for (int i = 1; i <= n; i++)
        scanf("%d", &arr[i]), bucket[i] = arr[i], blockId[i] = (i - 1) / siz + 1;
    sort(bucket + 1, bucket + 1 + n);
    int buckTot = unique(bucket + 1, bucket + 1 + n) - bucket;
    for (int i = 1; i <= n; i++)
        currentId[i] = lower_bound(bucket + 1, bucket + 1 + buckTot, arr[i]) - bucket;
    for (int i = 1; i <= m; i++)
        scanf("%d%d", &queries[i].l, &queries[i].r), queries[i].id = i;
    sort(queries + 1, queries + 1 + m);
    // Previous Information:
    int L = 1, R = 0, tmd;
    ll tmp = 0;
    answer = 0;
    queries[0].l = 0, blockId[0] = 0;
    for (int i = 1; i <= m; i++)
    {
        // Validiate if this interval is able to be calced by the previous one;
        if (blockId[queries[i - 1].l] != blockId[queries[i].l])
        {
            memset(cnt, 0, sizeof(cnt));
            R = tmd = blockId[queries[i].l] * siz;
            answer = tmp = 0;
        }
        L = min(tmd + 1, queries[i].r + 1);
        // Calc;
        while (L > queries[i].l)
            update_left(--L);
        while (R < queries[i].r)
        {
            R++;
            tmp = max((++cnt[currentId[R]]) * 1LL * arr[R], tmp);
            answer = max(answer, (cnt[currentId[R]] + leftBucket[currentId[R]]) * 1LL * arr[R]);
        }
        // Set the answer;
        anses[queries[i].id] = answer;
        // Set the bucket back;
        for (int j = L; j <= queries[i].r && j <= tmd; j++)
            leftBucket[currentId[j]]--;
        answer = tmp;
    }
    // Print the answer;
    for (int i = 1; i <= m; i++)
        printf("%lld\n", anses[i]);
    return 0;
}
```

## B - Mountain Climbing

This is an <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">idiotic</span> problem, and then I forced a topological-order DP on it and went <span class="slang" data-zh="GG" data-gloss="Gamer shorthand for “good game”, typed when conceding a match. Here: it is over, I am done for.">GG</span>. Thinking about it now, I really regret it.

Clearly, condensing every cycle into a single node forms a DAG (directed acyclic graph). After that, just brute-force the DP, then take the marked answers and compare them for the maximum. A really <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">idiotic</span> problem.

Oh, right, remember to enlarge the stack. (JZOJ <span class="slang" data-zh="万年卡栈" data-gloss="wànnián kǎ zhàn, “jamming the stack for ten thousand years”. 卡栈: a judge whose small stack limit crashes deep recursion; 万年: it has always been this way.">has been choking on the stack since forever</span>)

```cpp
// B.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 602000;
extern int theMain(void) __asm__("theMain");
int head[MAX_N << 1], current, n, m, dfn[MAX_N], low[MAX_N], aff[MAX_N];
int tot, stk[MAX_N], cur, afftot, indeg[MAX_N << 1], tmpx, tmpy, s;
ll cnt[MAX_N << 1], dp[MAX_N << 1], answer;
bool inst[MAX_N], mark[MAX_N];
struct edge
{
    int to, nxt;
} edges[MAX_N << 1];
void addpath(int src, int dst)
{
    edges[current].to = dst;
    edges[current].nxt = head[src], head[src] = current++;
}
void tarjan(int u)
{
    dfn[u] = low[u] = ++tot, stk[++cur] = u, inst[u] = true;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (dfn[edges[i].to] == 0)
            tarjan(edges[i].to), low[u] = min(low[u], low[edges[i].to]);
        else if (inst[edges[i].to])
            low[u] = min(low[u], dfn[edges[i].to]);
    if (low[u] == dfn[u])
    {
        int j, nd = ++afftot;
        do
        {
            j = stk[cur], inst[j] = false;
            aff[j] = nd;
        } while (stk[cur--] != u);
    }
}
void toposort()
{
    queue<int> q;
    q.push(aff[s]), dp[aff[s]] = cnt[aff[s]];
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i != -1; i = edges[i].nxt)
        {
            dp[edges[i].to] = max(dp[edges[i].to], dp[u] + cnt[edges[i].to]);
            q.push(edges[i].to);
        }
    }
}
int theMain()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++)
        scanf("%d%d", &tmpx, &tmpy), addpath(tmpx, tmpy);
    afftot = n;
    for (int i = 1; i <= n; i++)
        if (aff[i] == 0)
            tarjan(i);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &cnt[i]);
    for (int u = 1; u <= n; u++)
    {
        cnt[aff[u]] += cnt[u];
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (aff[u] != aff[edges[i].to])
                addpath(aff[u], aff[edges[i].to]), indeg[aff[edges[i].to]]++;
    }
    scanf("%d%d", &s, &tmpx);
    for (int i = 1; i <= tmpx; i++)
        scanf("%d", &tmpy), mark[aff[tmpy]] = true;
    toposort();
    for (int i = 1; i <= n; i++)
        if (mark[aff[i]])
            answer = max(answer, dp[aff[i]]);
    printf("%lld", answer);
    return 0;
}

int main()
{
    int size = 64 << 20;
    char *p = (char *)malloc(size) + size;
    __asm__ __volatile__("movq  %0, %%rsp\n"
                         "pushq $exit\n"
                         "jmp theMain\n" ::"r"(p));
    return 0;
}
```

## C - ~~<span class="slang" data-zh="货仓选址" data-gloss="huòcāng xuǎnzhǐ, “choosing a warehouse site”. A classic textbook problem: place a warehouse so the total distance to all stores is minimal.">Warehouse Location</span>~~ Transporting <span class="slang" data-zh="妹子" data-gloss="mèizi, “little sister”. Casual slang for girls or young women.">Girls</span>

Just as the title says, and then I <span class="slang" data-zh="秒切" data-gloss="miǎo qiē, “cut in a second”. To solve a problem instantly, almost without thinking.">insta-solved</span> it.

```cpp
// C.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 101000;
ll n, l, w, pos[MAX_N], prefix[MAX_N], answer;
bool validiate(int l, int r)
{
    int mid = (l + r) >> 1;
    ll ans = pos[mid] * (mid - l + 1) - (prefix[mid] - prefix[l - 1]) + (prefix[r] - prefix[mid]) - pos[mid] * (r - mid);
    return ans <= w;
}
int main()
{
    scanf("%lld%lld%lld", &n, &l, &w);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &pos[i]), prefix[i] = prefix[i - 1] + pos[i];
    ll lcur = 1, rcur = 0;
    while (rcur < n && lcur <= n)
    {
        rcur++;
        while (lcur <= rcur && !validiate(lcur, rcur))
            lcur++;
        answer = max(rcur - lcur + 1, answer);
    }
    printf("%lld", answer);
    return 0;
}
```
