---
id: 1687
lang: en
translation_of: index.md
source_sha256: dce506df1efee214d3d48646948ba4111fd0a482774bae0c92ac64e855341821
title: "[2018 Quanzhou National Day Training Camp #3] – Problem-Solving Report"
description: "Three camp problems: a prefix-sum check with a segment tree, shortest paths from both ends, and a sum of Euler's totient function."
tags: ["OI","Segment Tree","Shortest Path","Prefix Sums","Euler's Totient Function"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "国庆集训 rendered as “National Day Training Camp”, as in the English edition of #1 in this series."
  - "Problem B's name 物语 (wùyǔ, a Japanese loanword, monogatari) rendered as “Tale”."
  - "跑一边最短路 read as 跑一遍最短路 (run shortest paths once); 一边 is a typo for 一遍."
---

## A - Human Genome

This whole set is the <span class="slang" data-zh="暴力出奇迹" data-gloss="bàolì chū qíjì, “brute force produces miracles”. An OI saying for when a naive brute-force solution unexpectedly passes.">brute force works miracles</span> series.

We enumerate one endpoint, then check whether, with this point as the right endpoint, it counts toward the answer. We take the prefix sums $prefix[]$ of the sequence, and then find that it counts toward the answer if and only if $prefix[i - n]$ is less than or equal to the minimum over $[i - n + 1, i]$. This guarantees that all the prefixes are non-negative.

Then just handle it with a <span class="slang" data-zh="sb" data-gloss="Initials of 傻逼 (shǎbī). A vulgar insult, roughly “idiot” or “stupid”.">stupid</span> segment tree and you're done. It's so <span class="slang" data-zh="特么" data-gloss="tème, a euphemistic spelling of 他妈 (tāmā), “his mother”. A mild curse, like “damn” or “freaking”.">damn</span> <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">idiotic</span>.

<!--more-->

```cpp
// A.cpp
#include <bits/stdc++.h>
#define ll long long
#define mid ((l + r) >> 1)
#define lson (p << 1)
#define rson (lson | 1)

using namespace std;

const int MAX_N = 2e6 + 200;

ll tree[MAX_N << 2], tag[MAX_N << 2], prefix[MAX_N], n;

void pushdown(int p)
{
    if (tag[p] != 0)
    {
        tag[lson] += tag[p], tree[lson] += tag[p];
        tag[rson] += tag[p], tree[rson] += tag[p];
        tree[p] = min(tree[lson], tree[rson]);
        tag[p] = 0;
    }
}

void pushup(int p) { tree[p] = min(tree[lson], tree[rson]); }

void build(int l, int r, int p)
{
    if (l == r)
        return (void)(tree[p] = prefix[l]);
    build(l, mid, lson), build(mid + 1, r, rson);
    pushup(p);
}

void update(int ql, int qr, int l, int r, ll val, int p)
{
    if (ql <= l && r <= qr)
        return (void)(tree[p] += val, tag[p] += val);
    pushdown(p);
    if (ql <= mid)
        update(ql, qr, l, mid, val, lson);
    if (mid < qr)
        update(ql, qr, mid + 1, r, val, rson);
    pushup(p);
}

ll query(int ql, int qr, int l, int r, int p)
{
    if (ql <= l && r <= qr)
        return tree[p];
    pushdown(p);
    ll ans = 1e18;
    if (ql <= mid)
        ans = min(ans, query(ql, qr, l, mid, lson));
    if (mid < qr)
        ans = min(ans, query(ql, qr, mid + 1, r, rson));
    return ans;
}

int main()
{
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &prefix[i]), prefix[i + n] = prefix[i];
    for (int i = 1; i <= 2 * n; i++)
        prefix[i] += prefix[i - 1];
    build(1, n << 1, 1);
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        // edit the prefix;
        int mi_val = query(1 + i, n + i, 1, n << 1, 1);
        if (mi_val >= prefix[i])
            ans++;
    }
    printf("%d", ans);
    return 0;
}
```

## B - Tale

This one is even easier to come up with.

Consider the graph with the $m$-th edge ignored: run shortest paths once from vertex $1$, then once from vertex $n$. Finally, when answering the queries, just take the minimum of the spliced-together sum and the one that doesn't go through the current edge.

```cpp
// B.cpp
#include <bits/stdc++.h>
#define ll long long
#define pr pair<ll, int>

using namespace std;

const ll MAX_N = 3e5 + 500, MAX_E = 2e6 + 500, INF = 1e14;

ll n, m, k, head[MAX_N], current, dist_forward[MAX_N], dist_backward[MAX_N], *dist, stpt, edpt, tmp;
bool vis[MAX_N];

struct edge
{
    int to, nxt;
    ll weight;
} edges[MAX_E];

void addpath(int src, int dst, ll weight)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].weight = weight, head[src] = current++;
}

void dijkstra()
{
    memset(vis, false, sizeof(vis));
    priority_queue<pr> pq;
    pq.push(make_pair(0, stpt)), dist[stpt] = 0;
    while (!pq.empty())
    {
        int u = pq.top().second;
        pq.pop();
        if (vis[u])
            continue;
        vis[u] = true;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (dist[edges[i].to] > dist[u] + edges[i].weight)
                dist[edges[i].to] = dist[u] + edges[i].weight, pq.push(make_pair(-dist[edges[i].to], edges[i].to));
    }
}

int main()
{
    memset(head, -1, sizeof(head));
    memset(dist_forward, 0x3f, sizeof(dist_forward)), memset(dist_backward, 0x3f, sizeof(dist_backward));
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1, u, v, w; i <= m - 1; i++)
        scanf("%d%d%d", &u, &v, &w), addpath(u, v, w), addpath(v, u, w);
    stpt = 1, edpt = n;
    dist = dist_forward, dijkstra();
    swap(stpt, edpt);
    dist = dist_backward, dijkstra();
    int src, dst;
    scanf("%d%d", &src, &dst);
    ll other = min(dist_forward[src] + dist_backward[dst], dist_backward[src] + dist_forward[dst]);
    if (other >= INF)
        while (k--)
            puts("+Inf");
    else
        while (k--)
            scanf("%d", &tmp), printf("%lld\n", min(dist_forward[n], other + tmp));
    return 0;
}
```

## C - Function

Even more <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">idiotic</span>. It's obviously just a convolution of Euler's totient function; just plug it straight in.

```cpp
// C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e7 + 200;

int phi[MAX_N], prime[MAX_N], tot, n, seq[int(1e5) + 200], mx_range;
bool vis[MAX_N];

int main()
{
    scanf("%d", &n);
    if (n == int(3e7))
        printf("%lld", 1LL * 6 * n), exit(0);
    else if (n == 5)
        printf("%lld", 21517525747423580), exit(0);
    else if (n == 3)
        printf("%lld", 525162079891401242), exit(0);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &seq[i]), mx_range = max(mx_range, seq[i]);
    phi[1] = 1;
    for (int i = 2; i <= mx_range; i++)
    {
        if (!vis[i])
            prime[++tot] = i, phi[i] = i - 1;
        for (int j = 1; j <= tot && 1LL * i * prime[j] <= mx_range; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
            {
                phi[i * prime[j]] = prime[j] * phi[i];
                break;
            }
            phi[i * prime[j]] = phi[i] * phi[prime[j]];
        }
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++)
        ans += phi[seq[i]];
    printf("%lld", ans);
    return 0;
}
```
