---
id: 1689
lang: en
translation_of: index.md
source_sha256: bafd0b1fbcf025a10b54b06e2ba8be345cf11c41969adf5fe18b7c775127a890
title: "[2018 Quanzhou National Day Training Camp #4] – Problem-Solving Report"
description: "Three camp problems: expand outward from the middle until the gcd is 1, brute force on 2D prefix sums in O(n^3), and CDQ divide and conquer."
tags: ["OI","Godlike","Brute Force","Divide and Conquer","Prefix Sums","CDQ Divide and Conquer","Human Wisdom"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "国庆集训 rendered as “National Day Training Camp”, matching the English editions of #1 and #2 in this series."
  - "The problem names 两情 / 若是 / 长久时 together echo Qin Guan's line 两情若是久长时; each is translated literally on its own (Two Hearts / If / Lasting Long)."
  - "左点对右问 is the author's shorthand for 左边的点对右边的询问, rendered as “the left points to the right queries”."
---

## A - Two Hearts

This problem is pure <span class="slang" data-zh="人类智慧" data-gloss="rénlèi zhìhuì, “human wisdom”. OI slang for a solution that hinges on a clever, hard-to-motivate insight rather than a standard technique.">human wisdom</span>; I really don't know how to derive it.

Expand outward from the middle, and as soon as you hit a case where $\gcd = 1$, just output the answer.

<!--more-->

```cpp
// A.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll n, T;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%lld", &n);
        for (ll i = n >> 1, j = (n + 1) >> 1; i >= 1 && j <= n; i--, j++)
            if (gcd(i, j) == 1)
            {
                printf("%lld\n", i * j / gcd(i, j));
                break;
            }
    }
    return 0;
}
```

## B - If

Doing brute force with prefix sums on this problem is really comfortable.

First compute the prefix sums, then enumerate the bottom-right point, and then enumerate $k$, the row number of the upper rectangle:

![Screenshot 2019-09-15 at 11.15.38 PM](media/屏幕快照-2019-09-15-下午11.15.38.png)

Take point $A$ as $(0, 0)$. Then the sum of the whole subrectangle should be $prefix[i][j] - prefix[i][k] - prefix[l][j] + prefix[l][k]$. Then, to find the maximum, we can just record the minimum of $prefix[i][j] - prefix[k][j]$ while scanning, which squeezes in at exactly $O(n^3)$.

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

using namespace std;

const int MAX_N = 220;

int n, m, val[MAX_N][MAX_N], prefix[MAX_N][MAX_N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &val[i][j]), prefix[i][j] = prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1] + val[i][j];
    int mx_ans = -0x3f3f3f3f;
    for (int i = 1; i <= n; i++)
        for (int k = 0; k < i; k++)
            for (int j = 0, min_val = 0x3f3f3f3f; j <= m; j++)
            {
                min_val = min(min_val, prefix[i][j] - prefix[k][j]);
                mx_ans = max(mx_ans, prefix[i][j] - prefix[k][j] - min_val);
                continue;
            }
    printf("%d", mx_ans);
    return 0;
}
```

## C - Lasting Long

This problem is classic CDQ divide and conquer, but I didn't figure out how exactly to do it, and then <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>.

As with all CDQ divide and conquer, we solve the subproblems in the order of the original sequence. At each level we compute the contribution of the left points to the right queries, then just keep recursing.

The triangles can just be sorted the normal way: sorting by their coordinate sum as the key lets the later points be included in the queries.

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

using namespace std;

const int MAX_N = 3e5 + 200;

int n, answer[MAX_N], xi[MAX_N], yi[MAX_N], xtot, ytot, tot;

struct tup
{
    int x, y, limit, val;
    bool operator<(const tup &tgt) const { return limit < tgt.limit || (limit == tgt.limit && abs(val) < abs(tgt.val)); }
} pool[MAX_N], tmp[MAX_N];

int tree1[MAX_N], tree2[MAX_N];

inline int lowbit(int x) { return x & -x; }

int query(int *arr, int x)
{
    int ans = 0;
    for (; x; x -= lowbit(x))
        ans += arr[x];
    return ans;
}

void update(int *arr, int x, int upper, int d)
{
    for (; x <= upper; x += lowbit(x))
        arr[x] += d;
}

void process(int cnt)
{
    sort(tmp + 1, tmp + 1 + cnt);
    int pt = 0;
    for (int i = 1; i <= cnt; i++)
        if (tmp[i].val != 0)
        {
            int val = abs(tmp[i].val), opt = val / tmp[i].val;
            answer[val] += opt * (pt - query(tree1, tmp[i].x) - query(tree2, tmp[i].y));
        }
        else
            pt++, update(tree1, tmp[i].x, xtot, 1), update(tree2, tmp[i].y, ytot, 1);
    for (int i = 1; i <= cnt; i++)
        if (tmp[i].val == 0)
            update(tree1, tmp[i].x, xtot, -1), update(tree2, tmp[i].y, ytot, -1);
}

void cdq(int l, int r)
{
    int mid = (l + r) >> 1, ptr1 = 0, ptr2 = 0;
    for (int i = l; i <= mid; i++)
        if (pool[i].val == 0)
            tmp[++ptr1] = pool[i];
    for (int i = mid + 1; i <= r; i++)
        if (pool[i].val != 0)
            tmp[ptr1 + (++ptr2)] = pool[i];
    if (ptr1 > 0 && ptr2 > 0)
        process(ptr1 + ptr2);
    if (ptr1 > 0 && ptr1 <= mid - l)
        cdq(l, mid);
    if (ptr2 > 0 && ptr2 < r - mid)
        cdq(mid + 1, r);
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int x, y, d;
        scanf("%d%d%d", &x, &y, &d);
        if (d == 0)
            xi[++xtot] = x, yi[++ytot] = y, pool[++tot] = {x, y, x + y, 0}, answer[i] = -1;
        else
        {
            xi[++xtot] = x - 1, yi[++ytot] = y - 1;
            pool[++tot] = {x - 1, y - 1, x + y - 1, -i};
            pool[++tot] = {x - 1, y - 1, x + y + d, i};
        }
    }
    sort(xi + 1, xi + 1 + xtot), xtot = unique(xi + 1, xi + 1 + xtot) - xi - 1;
    sort(yi + 1, yi + 1 + ytot), ytot = unique(yi + 1, yi + 1 + ytot) - yi - 1;
    for (int i = 1; i <= tot; i++)
    {
        pool[i].x = lower_bound(xi + 1, xi + 1 + xtot, pool[i].x) - xi;
        pool[i].y = lower_bound(yi + 1, yi + 1 + ytot, pool[i].y) - yi;
    }
    cdq(1, tot);
    for (int i = 1; i <= n; i++)
        if (answer[i] >= 0)
            printf("%d\n", answer[i]);
    return 0;
}
```
