ShanireZ OJ Style (shanirez-style)
What this is
A style fingerprint distilled from ~3,200 single-file C++ solutions in the OJCode workspace
(Luogu/, CodeForces/, LOJ/, PAT/, SP UVA AT/). The goal is simple: code you generate
or touch should read as if the author wrote it themselves — same skeleton, same brace style, same
short names, same idioms. These are observed habits, not rules handed down from on high; where
the corpus is genuinely mixed, this file says so, and you should match the dominant habit unless
the surrounding file in the same folder does otherwise.
Where a habit has shifted over time, this file follows the last two years of the corpus — the
940 files added since 2024-08 that still exist — not the all-time average. That's what "the
author's current hand" means, and every "N of the last 940" figure below is measured against that
set.
Solutions here are throwaway-style scripts: one file, one problem, global state, terse names, no
build system, no abstraction layers. Resist the instinct to "engineer" them. No OOP wrappers
around algorithms, no namespaces, no templates-for-reuse, no exceptions, no logging, no test
scaffolding. The whole point is a dense, fast, self-contained script.
The C++ dialect is C++14 — the workspace build task compiles with
g++ -std=c++14 -O2 -Wall -m64 -static-libgcc -fexec-charset=UTF-8 -Wl,-stack=1073741824.
Don't reach for C++17/20-only library features. Note the 1 GB stack in that flag list:
deep recursion is safe on this machine, which is why the recursive form stays the house form
for tree algorithms that a textbook would write iteratively.
The skeleton
Every file looks like this. Internalize it.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MX 100005
int n, m, ans, a[MX];
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
cout << ans << endl;
return 0;
}
// TAG: 算法 标签
- Explicit headers, alphabetically sorted (the editor sorts them automatically).
<iostream> and <algorithm> are the workhorses — they appear in ~100% and ~96% of files —
then <vector> <queue> <cstring> <cmath> <map> <iomanip> <set> <string> as needed.
Avoid <bits/stdc++.h> (12 files in the whole corpus) unless the existing file in that
folder already uses it. The one sanctioned bits/ header is <bits/extc++.h> for PBDS —
see the PBDS note below.
using namespace std; — universal (99.9%). Always include it.
- Globals right after the headers: big arrays, struct definitions, helper functions, then
int main(). Helpers (dfs, dfn, query…) are free top-level functions above main, never methods.
int main() (never signed main()), ending in an explicit return 0;.
- No
#pragma GCC optimize (0 occurrences).
Formatting (the part that's most visible)
Where the formatting comes from
The layout isn't maintained by hand: it's what clang-format produces under the VS Code C/C++
extension's default Visual Studio fallback style — Allman braces, 4-space indent,
UseTab: Never, SortIncludes on, ColumnLimit: 0. Two consequences are worth internalizing:
- Includes are always alphabetically sorted. Zero of the last 940 files have an out-of-order
include block.
- There is no column limit — do not wrap lines to fit 80 or 100 columns. Longest-line
percentiles over the last 940 files: p50 = 53, p90 = 83, p95 = 98, p99 = 134, max = 155. A long
condition, a long
cout chain, or a long comma-operator line stays on one line. Break only
where the author would naturally break — a multi-clause || guard, continuation aligned under
the opening paren — never to satisfy a ruler.
Allman braces — always, on every block
The opening { goes on its own line, for functions, structs, loops, and conditionals — and
braces are never omitted, even for a single-statement if/for/while/else. This is the
single most consistent habit in the corpus (effectively 100%). Getting this wrong is the fastest
way to make code look foreign.
// house style
if (gx != gy)
{
g[gx] = gy;
}
// NOT this — brace omitted
if (gx != gy) g[gx] = gy;
// and NOT K&R
if (gx != gy) {
g[gx] = gy;
}
Indentation: 4 spaces — but check the file you're editing
Write 4 spaces. That's what the local editor config produces and what ~90% of the last two
years' files use.
The exception is real and not rare: 96 of the last 940 files (10.2%) are tab-indented. It
arrives in bursts rather than as a drift — every file added in 2025-08, 41 of 51 in 2025-12, and
11 of 36 in 2026-01 are tabs, while 2026-02 through 2026-04 and 2026-07 have none at all. Read it
as "written somewhere without the workspace's editor config", not as a change of taste.
So: new file → 4 spaces. Editing an existing file → look at what it already uses and match it;
roughly one file in ten will be tabs. Never mix the two inside one file, and never reindent a
whole file just to normalize it.
No blank lines, ever
A solution is one solid block of code — no blank line between the headers and the globals,
between two functions, or anywhere inside a function. 938 of the last 940 files (99.8%) contain
zero blank lines. This is as visible as the brace style: a file with airy paragraph spacing reads
as someone else's.
No trailing newline at end of file
The last byte is the closing } or the // TAG: line — no final \n. Every recent file in the
corpus is written this way.
Arrays, variables, and naming
- Large arrays live at global scope, sized with a safety margin (e.g.
100005, 200005,
MX * 4 for a segment tree). Global allocation avoids stack overflow and is zero-initialized for
free. Loop counters and small per-query scalars are declared locally where they're used.
- STL containers and strings also go global, not declared inside a function:
queue<int> q; (52 recent files declare the queue q; que appears in none),
priority_queue<Path> q; — or pq when a plain queue already owns q —
stack<int> st;, string s; at file scope. The corpus is emphatic for containers
(queue/priority_queue/stack/deque are almost always global) and leans global for strings
too. The one thing to remember: a reused global container must be cleared between independent
uses (e.g. on each test case).
- Sizing: an inline literal (
int a[100005];) is the most common; #define MX 100005 is the
next most common (25 of the last 940 files, ~3%; ~6% all-time) and typical in larger solutions
so the bound has one name. const int maxn is essentially never used here — don't introduce it.
#define is for named bounds and shorthands, and the list is short. The main forms are
MX (array bound), MOD (modulus), and LC/RC (segment-tree child shorthand,
#define LC ns[now].lc). A few side forms show up too and read fine when they earn their keep:
MX2 / M / N for a second bound in the same file, LG for a binary-lifting height,
and ull / a gc() fread macro inside a hand-rolled fast reader. What stays out is the
reflex macro that replaces plain code: #define mid (l + r) / 2 reads as foreign (one file in
the whole corpus) — write int mid = (l + r) / 2; in each function instead. When in doubt,
prefer a literal or a plain variable over a new macro.
- Pack related globals on one line:
int n, m, cnt, g[100005];
- Short, traditional names. This is competitive code; verbosity reads as foreign.
- sizes/counts:
n m k q T
- loop indices:
i for a single loop; i and j for the first and second dimension of a
2-D array — named by array dimension, not loop-nesting order (anc[i][j], so the build loop
is for (j...) for (i...), outer j, inner i). Reserve k for a genuinely special third
index, e.g. Floyd's intermediate vertex for (k) for (i) for (j) dis[i][j] = min(..., dis[i][k] + dis[k][j]).
- graph:
u v w for an edge's two endpoints and weight; to for an unweighted adjacency
list and es for a vector<Edge> one (see the graph-storage idiom below). pre/last
belong to the legacy 链式前向星 form — read them, don't write them.
- depth in a rooted tree is
h[] (29 recent files) more often than dep[] (5).
- node you're at vs. node you step to:
now = the current node, nxt = the next/neighbour
node (for (int nxt : to[now]), int nxt = e.to;). Keep this pair consistent.
- accumulators:
ans cnt tot sum res
- state/pointers:
vis dis dp pos id fa
- boolean flags / on-off state:
ok, occasionally trig — and declared int, not bool
(int ok = 1; ... ok = 0;). Never a long descriptive name.
- intervals:
l r mid
- structs: PascalCase —
Node (100 recent files), Edge (39), Path (13) cover almost
everything — with terse fields lc rc v tag, x y, v w, to w. The global array of a
struct is named ns — Node ns[MX * 4];, sort(ns + 1, ns + n + 1). Its allocation
counter is pos or npos (equally common). The one exception to PascalCase is a
comparator functor, which takes a lowercase name — cmpa / cmpb / cmp11 / hsh —
used when one struct needs several orderings or a custom hash.
- a Chinese-pinyin initialism is fine for a derived table with no short English name —
qzmx/qzmn for 前缀 max/min, hzmx/hzmn for 后缀. Don't force it where pre/suf reads fine.
long long is written out explicitly where range demands it. Never #define int long long
(0 occurrences in the corpus) and no typedef long long ll. On a long-long-range problem the rule
is pragmatic, not dogmatic: if a handful of variables fit on one short declaration line, just
make the whole line long long rather than splitting hairs over which one actually overflows (the
author's lazy-but-safe default). But when declarations are many or spread across big arrays,
keep only the value-carrying ones long long and leave structural data — indices, depths, node
ids, visited flags, small counts — as int (no point burning memory on a long long anc[MX][20]).
Indexing: 1-based by default
Algorithmic data is 1-based: for (int i = 1; i <= n; i++), arrays used as a[1..n],
sort(a + 1, a + n + 1). Across the last 940 files that's 1,481 for (i = 1; i <= …) loops
against 168 for (i = 0; i < …). Drop to 0-based only for things that are natively 0-indexed —
string characters, bitmask bits, and a grid held as string s[MX], which is 0-based in both
dimensions — and cast .size() to int when comparing in a loop bound.
I/O
cin/cout by default (~90% of files). Plain, unadorned.
endl is the line terminator — 830 of the last 940 files (88%). Default to it; it's the
author's reflex. When a file does use a bare newline instead, it's the char '\n'
(27 files) more often than the string "\n" (17) — and it's a whole-file choice, not a
per-line one: 828 files use only endl, 15 use only a newline literal, 2 mix them.
ios::sync_with_stdio(false); is occasional, not default — ~2% of files, reserved for
genuinely I/O-heavy problems. Written as that one statement on its own, sometimes chained as
cin.tie(0)->ios::sync_with_stdio(false); or cin.tie(0)->sync_with_stdio(false);. Don't
sprinkle it on every file; reach for it (or the hand-written read() below) only when input
size actually warrants it.
- Hand-written
read() (getchar loop) is rarer than it used to be — 13 of the last 940
files (1.4%), down from ~9% all-time. When you do use it, printf comes with it: 13 of
the 18 recent files that call printf also define read(). Fast input and fast output travel
as a pair, and cin + printf is a mix that appears in only 2 files. See
references/templates.md.
- Formatted floats use
cout << fixed << setprecision(n) (with <iomanip>), not
printf("%.2f"). This matters on PAT, where output precision is judged.
scanf is nearly gone — 5 of the last 940 files (0.5%). printf is not (18 files,
1.9%), but almost every occurrence is the output half of the read() + printf fast-IO pair
above, or a %llu. Don't reach for printf on its own; don't pair it with cin.
- An EOF-driven input loop is
while (cin >> n) — 17 recent files, against 5 for
while (scanf(...) != EOF). Use the scanf form only when matching an existing file.
- Multiple test cases:
int T; cin >> T; while (T--) { ... } (95 recent files; the variable
is T or t).
Switch to for (int t = 1; t <= T; t++) only when the case number is part of the output
(cout << "Case #" << t << ": " << ans << endl;).
Idioms that make it look authentic
- Comma operator for grouped side-effects on one line — a signature habit. Use it for tightly
related updates:
g[gu] = gv, cnt++; // DSU merge + count
anc[nxt][0] = now, h[nxt] = h[now] + 1; // fix parent + depth before recursing
vis[nx][ny] = 1, dis[nx][ny] = dis[x][y] + 1;
ans += es[i].w, d -= es[i].cnt; // greedy take
- Inline ternary for simple branches, including as a statement and inside output — this has
become steadily more common:
check(mid) ? l = mid + 1 : r = mid - 1;
s[now] = (sc[j] >= 60 ? 1 : -1);
cout << (ans == 1e9 ? -1 : ans) << endl;
- Infinity is a literal, not a named constant.
1e9 for int range and 1e18 for
long long are the current default (52 vs 27 recent files against 0x3f3f3f3f) — assign it
directly and compare against it directly:int ans = 1e9, l = 1, r = 1e9;
dp[i] = 1e9;
if (c0 == 1e9 && c1 == 1e9) { ... }
memset(x, 0x3f, sizeof(x)) stays the form when you need to fill a whole array at once
(needs <cstring>); then compare against 0x3f3f3f3f (or 0x3f3f3f3f3f3f3f3f for long long).
Never INT_MAX/LLONG_MAX/#define INF.
- Compute a shared prerequisite once, then merge guard clauses with
|| — don't write a
staircase of early-exits. Get the value both guards need first, then OR the conditions:int l = lca(x, y);
if (a[x] == 0 || z[x] + z[y] - 2 * z[l] > 0) // both "trivially Yes" cases, together
{
cout << "Yes" << endl;
continue;
}
rather than an if (a[x] == 0) {...continue;}, then the lca, then a separate
if (zero on path) {...continue;}. One guard, all the trivial cases visible at once.
- STL, used plainly:
queue, priority_queue (min-heap via a reversed operator<, or the
three-argument priority_queue<int, vector<int>, greater<int>> when the element is a bare
scalar), map, set.
- A container is tested for emptiness with
.size(), never .empty(). while (q.size())
appears in 48 recent files and while (!q.empty()) in zero; .empty() shows up anywhere at
all in only 5. The same reflex applies to a plain truth test: if (now.size()), if (v.size()).
- Graph storage — adjacency
vector, and the name follows the payload. Unweighted:
vector<int> to[MX] — 29 recent files, against es 13 and g 7. Weighted, or carrying
extra per-edge data: a small struct Edge { int to, w; }; held in vector<Edge> es[MX] —
25 files, and vector<Edge> to[] never appears. Keeping the two names apart is the habit: to
when the element is the neighbour, es when it's an edge record.
Push with push_back — push_back(Edge{v, w}) 22 files vs emplace_back(Edge{v, w}) 2,
and unweighted push_back(v) 21 vs emplace_back(v) 7. emplace_back isn't foreign, but
push_back is the default; stay consistent within a file.
链式前向星 (chained forward star) is legacy: 72 old files use it and zero in the last two
years. Recognize it when reading old code (last[u] head, pre[i] previous edge, to[i]
endpoint); don't write it in a new file.
- Iterate edges with range-for when you don't need the edge index:
for (int nxt : to[now]) or
for (Edge e : es[now]). Only fall back to an indexed loop when the index itself matters, and
cast the bound: for (int i = 0; i < (int)es[now].size(); i++).
- A tree DFS carries its parent and skips it — no
vis[]. The signature is
void dfs(int now, int from) and the first thing in the loop is the parent guard; ~25 recent
files do this, and it's the reason vis[] is scarce in tree code (it stays for general graphs
and grids):void dfs(int now, int from)
{
for (Edge e : es[now])
{
if (e.to == from)
{
continue;
}
h[e.to] = h[now] + 1;
dfs(e.to, now);
}
}
- Grid movement uses a global
ms[4][2] offsets table and nx/ny — never dx/dy:int ms[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
// ...
for (int i = 0; i < 4; i++)
{
int nx = x + ms[i][0], ny = y + ms[i][1];
if (nx < 1 || nx > n || ny < 1 || ny > m || vis[nx][ny])
{
continue;
}
}
Diagonals go in ms[8][2], or a second table ms2[4][2] when the two move sets are distinct.
The grid itself is usually a string array — string s[MX]; read with cin >> s[i] and
indexed s[x][y] (19 recent files, against 10 for a char mp[MX][MX]). A string grid is
naturally 0-based, so that's one of the places the 1-based default doesn't apply; bounds
read nx >= 0 && nx < n.
__int128 is the overflow escape hatch — 11 recent files. Reach for it when an intermediate
product would blow past long long (__int128 res = min((__int128)b, (__int128)n * a);) or when
a comparison has to be done exactly. cin/cout can't handle it, so printing needs a
hand-written recursive write() on putchar — see references/templates.md. unsigned __int128 shows up too when the value is known non-negative.
- One function with a parameter, not near-duplicate functions. When two helpers would share the
same body and differ only by a constant, write a single function that takes that constant as an
argument —
val(x, 2) / val(x, 5), not separate val2 / val5:int val(long long x, int p) // count how many times prime p divides x
{
int c = 0;
while (x % p == 0)
{
x /= p, c++;
}
return c;
}
- No redundant casts. Don't cast an operand wide when the other operand is already wide —
promotion handles it. With
long long scale, write fp += (s[i] - '0') * scale;, not
fp += (long long)(s[i] - '0') * scale;. Only cast when the whole expression would otherwise be
evaluated narrow (e.g. (long long)a * b when both are int).
- Sorting: a member
bool operator<(const T &oth) const is the strong default — 79 of the 82
recent files that define one, always by const reference, with the parameter named oth (the
three strays take it by value or name it other). A free bool cmp(T a, T b) passed to
sort(ns + 1, ns + n + 1, cmp) is the fallback when you sort the same data several ways, and a
lowercase functor struct (cmpa, cmpb) when a container needs the ordering as a type. Keep it
1-based. No lambdas — they never appear here, in a sort or anywhere else (0 of 940 files).
auto is rare — mostly for iterators. Prefer explicit types.
PBDS — the one sanctioned bits/ header
For a hash table or an order-statistic tree, the author reaches for GNU PBDS. It's a small but
entirely recent habit (7 files, all from the last two years), and it's the sole exception to the
"no bits/* headers" rule:
#include <algorithm>
#include <bits/extc++.h>
#include <iostream>
using namespace std;
using namespace __gnu_pbds;
cc_hash_table<int, int> mp;
The include still sorts alphabetically (algorithm < bits/extc++.h < iostream), and
using namespace __gnu_pbds; goes on the line after using namespace std;. When PBDS is an
alternative solution to a problem you already solved another way, name the file with the
PBDS suffix — P3014 PBDS.cpp.
Comments and the // TAG: line
Comments are sparse — 129 of the last 940 files carry one at all. A short Chinese note
(UTF-8) for a non-obvious step, a math derivation, or a struct field's meaning, trailing on the
same line as the code it explains (239 trailing lines vs 48 standalone). Block comments
(/* … */) never appear. Don't narrate line by line; the code carries itself.
End a file you create or substantially touch with an algorithm tag — space-separated
techniques, Chinese (sometimes mixed with English names).
This one is an aspiration, not a reproduction of the current hand. Only 17 of the last 940
files (1.8%) carry a // TAG: line, and the habit effectively stopped after 2024-10; across the
whole corpus it's 243 of 3,208 (7.6%). The reason to write it anyway is that the repo README
lists "为题解补齐算法标签" as an open Todo — so adding it is helping with a stated goal. Don't
mistake its absence in surrounding files for a signal to drop it, and don't cite it as evidence
of what the author's recent code looks like.
// TAG: 网络流 最大流 最小割 Dinic
// TAG: 数位DP 回文
// TAG: 树状数组 线段树 逆序对 动态维护
// TAG: 双向搜索 meet in the middle
This line is the last thing in the file, with no newline after it.
File placement & naming
- Put the file in the right judge folder:
Luogu/, CodeForces/, LOJ/, PAT/Basic/,
PAT/Advanced/ (or a contest subfolder like PAT/22Q2/Basic/), SP UVA AT/.
- Name it by the exact problem id:
P3372.cpp, B3609.cpp, CF1009F.cpp, #10000.cpp,
ABC403G.cpp, ARC085E.cpp, UVA10079.cpp, SP1043.cpp. Luogu's other id prefixes keep the
same rule — T777250.cpp (团队/私有题), U422706.cpp (用户上传题).
- For an alternative approach or partial solution, append
space + Suffix:
P3367 Dfn.cpp, B3609 Tarjan.cpp, B3609 Kosaraju.cpp, P3372 Zkw.cpp, P9753 90pts.cpp,
P3014 PBDS.cpp, P1486 Seg.cpp, P1525 PowDfn.cpp (带权并查集), P2216 DQ.cpp.
Verified algorithm templates
When you need a standard data structure or algorithm, use the canonical forms in
references/templates.md — they're lifted directly from this codebase and
already follow every convention above (Allman, no blank lines, 1-based, global pools, dfn/g[]
DSU, lc/rc segment-tree pool, to[]/es[] graphs with range-for, recursive binary-lifting LCA,
read() + printf, Dijkstra, grid BFS, struct sort, __int128 output, fixed-precision float;
链式前向星 included only as a legacy form to read). Read that file when implementing one of those;
don't reinvent the form from memory.
Don't do these (they read as foreign here)
- Blank lines anywhere in the file, or a trailing newline after the last
} / // TAG: line.
- Wrapping a long line to fit 80 or 100 columns — there is no column limit here.
- Reordering an include block out of alphabetical order.
#include <bits/stdc++.h> in a new file — use the specific headers. (<bits/extc++.h> for PBDS
is the one exception.)
#define int long long — declare the wide variables as long long instead.
#define mid (l + r) / 2 or a macro standing in for ordinary code — recompute
int mid = (l + r) / 2; in each function. (Named bounds are fine: MX, MX2, M, N,
plus MOD, LC/RC, LG.)
- K&R braces, or omitting braces on a single-line
if/for/while/else — always Allman, always braced.
- Tab indentation in a new file — 4 spaces. (In an existing file, match what's already there;
about one in ten is tabs.)
while (!q.empty()) — the house form is while (q.size()).
- 链式前向星 in a new file —
vector<int> to[MX] unweighted, vector<Edge> es[MX] weighted.
vector<int> es[MX] for an unweighted graph — that name is reserved for vector<Edge>; the
unweighted list is to[].
dx/dy grid offsets — the table is ms[4][2], the targets are nx/ny.
operator< taking its argument by value, or naming it o/np — it's const T &oth.
- Wrapping an algorithm in a class/namespace/reusable template — keep raw globals + free functions
(
using namespace __gnu_pbds; for PBDS is not an exception to this; it's just a using-directive).
- Defaulting every file to
ios::sync_with_stdio(false) — it's situational here, not a habit.
- Long descriptive identifiers (
parentArray, currentNode) — use g/fa, now.
const int maxn = ..., typedef long long ll;, INT_MAX/LLONG_MAX — match the inline-literal /
#define MX / explicit-long long / 1e9-1e18 conventions instead.
1---2name: shanirez-style3description: ShanireZ's personal C++ competitive-programming / online-judge coding style. Use whenever the user wants C++ for an algorithm or online-judge problem — pasting a problem statement ("题目如下, 帮我分析解决", "帮我完成这道题", "帮我写题解 / 给出完整代码"), naming a judge problem id (Luogu P*/B*, CodeForces CF*, LOJ #*, PAT, AtCoder ABC*/ARC*/AGC*, SPOJ SP*, UVa UVA*), or asking to write, debug, optimize, or refactor an existing .cpp in the OJCode workspace. Read this first so the code matches the author's own hand (Allman braces, 4-space, no blank lines, 1-based, global state, terse names, endl, explicit long long, // TAG line). Do NOT use for general-purpose C++ engineering (GUI/Qt apps, servers/networking, embedded, build systems, library/API design, business-logic debugging) or for non-C++ tasks — those are not OJCode solutions.4---56# ShanireZ OJ Style (shanirez-style)78## What this is910A style fingerprint distilled from ~3,200 single-file C++ solutions in the `OJCode` workspace11(`Luogu/`, `CodeForces/`, `LOJ/`, `PAT/`, `SP UVA AT/`). The goal is simple: code you generate12or touch should read as if the author wrote it themselves — same skeleton, same brace style, same13short names, same idioms. These are **observed habits**, not rules handed down from on high; where14the corpus is genuinely mixed, this file says so, and you should match the *dominant* habit unless15the surrounding file in the same folder does otherwise.1617Where a habit has **shifted over time**, this file follows the last two years of the corpus — the18940 files added since 2024-08 that still exist — not the all-time average. That's what "the19author's current hand" means, and every "N of the last 940" figure below is measured against that20set.2122Solutions here are throwaway-style scripts: one file, one problem, global state, terse names, no23build system, no abstraction layers. Resist the instinct to "engineer" them. No OOP wrappers24around algorithms, no namespaces, no templates-for-reuse, no exceptions, no logging, no test25scaffolding. The whole point is a dense, fast, self-contained script.2627The C++ dialect is **C++14** — the workspace build task compiles with28`g++ -std=c++14 -O2 -Wall -m64 -static-libgcc -fexec-charset=UTF-8 -Wl,-stack=1073741824`.29Don't reach for C++17/20-only library features. Note the **1 GB stack** in that flag list:30deep recursion is safe on this machine, which is why the recursive form stays the house form31for tree algorithms that a textbook would write iteratively.3233---3435## The skeleton3637Every file looks like this. Internalize it.3839```cpp40#include <algorithm>41#include <iostream>42#include <vector>43using namespace std;44#define MX 10000545int n, m, ans, a[MX];46int main()47{48 cin >> n >> m;49 for (int i = 1; i <= n; i++)50 {51 cin >> a[i];52 }53 cout << ans << endl;54 return 0;55}56// TAG: 算法 标签57```5859- **Explicit headers, alphabetically sorted** (the editor sorts them automatically).60 `<iostream>` and `<algorithm>` are the workhorses — they appear in ~100% and ~96% of files —61 then `<vector> <queue> <cstring> <cmath> <map> <iomanip> <set> <string>` as needed.62 **Avoid `<bits/stdc++.h>`** (12 files in the whole corpus) unless the existing file in that63 folder already uses it. The one sanctioned `bits/` header is `<bits/extc++.h>` for PBDS —64 see the PBDS note below.65- **`using namespace std;`** — universal (99.9%). Always include it.66- **Globals right after the headers**: big arrays, struct definitions, helper functions, then67 `int main()`. Helpers (dfs, dfn, query…) are free top-level functions above `main`, never methods.68- **`int main()`** (never `signed main()`), ending in an explicit `return 0;`.69- **No `#pragma GCC optimize`** (0 occurrences).7071---7273## Formatting (the part that's most visible)7475### Where the formatting comes from7677The layout isn't maintained by hand: it's what `clang-format` produces under the VS Code C/C++78extension's default *Visual Studio* fallback style — **Allman braces, 4-space indent,79`UseTab: Never`, `SortIncludes` on, `ColumnLimit: 0`**. Two consequences are worth internalizing:8081- **Includes are always alphabetically sorted.** Zero of the last 940 files have an out-of-order82 include block.83- **There is no column limit — do not wrap lines to fit 80 or 100 columns.** Longest-line84 percentiles over the last 940 files: p50 = 53, p90 = 83, p95 = 98, p99 = 134, max = 155. A long85 condition, a long `cout` chain, or a long comma-operator line stays on one line. Break only86 where the author would naturally break — a multi-clause `||` guard, continuation aligned under87 the opening paren — never to satisfy a ruler.8889### Allman braces — always, on every block9091The opening `{` goes on **its own line**, for functions, structs, loops, and conditionals — and92braces are **never omitted**, even for a single-statement `if`/`for`/`while`/`else`. This is the93single most consistent habit in the corpus (effectively 100%). Getting this wrong is the fastest94way to make code look foreign.9596```cpp97// house style98if (gx != gy)99{100 g[gx] = gy;101}102```103104```cpp105// NOT this — brace omitted106if (gx != gy) g[gx] = gy;107// and NOT K&R108if (gx != gy) {109 g[gx] = gy;110}111```112113### Indentation: 4 spaces — but check the file you're editing114115**Write 4 spaces.** That's what the local editor config produces and what ~90% of the last two116years' files use.117118The exception is real and not rare: **96 of the last 940 files (10.2%) are tab-indented.** It119arrives in bursts rather than as a drift — every file added in 2025-08, 41 of 51 in 2025-12, and12011 of 36 in 2026-01 are tabs, while 2026-02 through 2026-04 and 2026-07 have none at all. Read it121as "written somewhere without the workspace's editor config", not as a change of taste.122123So: **new file → 4 spaces. Editing an existing file → look at what it already uses and match it**;124roughly one file in ten will be tabs. Never mix the two inside one file, and never reindent a125whole file just to normalize it.126127### No blank lines, ever128129A solution is **one solid block of code** — no blank line between the headers and the globals,130between two functions, or anywhere inside a function. 938 of the last 940 files (99.8%) contain131zero blank lines. This is as visible as the brace style: a file with airy paragraph spacing reads132as someone else's.133134### No trailing newline at end of file135136The last byte is the closing `}` or the `// TAG:` line — no final `\n`. Every recent file in the137corpus is written this way.138139---140141## Arrays, variables, and naming142143- **Large arrays live at global scope**, sized with a safety margin (e.g. `100005`, `200005`,144 `MX * 4` for a segment tree). Global allocation avoids stack overflow and is zero-initialized for145 free. Loop counters and small per-query scalars are declared locally where they're used.146- **STL containers and strings also go global**, not declared inside a function:147 **`queue<int> q;`** (52 recent files declare the queue `q`; `que` appears in none),148 `priority_queue<Path> q;` — or `pq` when a plain `queue` already owns `q` —149 `stack<int> st;`, `string s;` at file scope. The corpus is emphatic for containers150 (`queue`/`priority_queue`/`stack`/`deque` are almost always global) and leans global for strings151 too. The one thing to remember: a reused global container must be cleared between independent152 uses (e.g. on each test case).153- **Sizing:** an inline literal (`int a[100005];`) is the most common; `#define MX 100005` is the154 next most common (25 of the last 940 files, ~3%; ~6% all-time) and typical in larger solutions155 so the bound has one name. `const int maxn` is essentially never used here — don't introduce it.156- **`#define` is for named bounds and shorthands, and the list is short.** The main forms are157 **`MX`** (array bound), **`MOD`** (modulus), and **`LC`/`RC`** (segment-tree child shorthand,158 `#define LC ns[now].lc`). A few side forms show up too and read fine when they earn their keep:159 **`MX2` / `M` / `N`** for a second bound in the same file, **`LG`** for a binary-lifting height,160 and `ull` / a `gc()` `fread` macro inside a hand-rolled fast reader. What stays out is the161 reflex macro that replaces plain code: `#define mid (l + r) / 2` reads as foreign (one file in162 the whole corpus) — write `int mid = (l + r) / 2;` in each function instead. When in doubt,163 prefer a literal or a plain variable over a new macro.164- **Pack related globals on one line:** `int n, m, cnt, g[100005];`165- **Short, traditional names.** This is competitive code; verbosity reads as foreign.166 - sizes/counts: `n m k q T`167 - **loop indices:** `i` for a single loop; `i` and `j` for the first and second dimension of a168 2-D array — named by **array dimension, not loop-nesting order** (`anc[i][j]`, so the build loop169 is `for (j...) for (i...)`, outer `j`, inner `i`). Reserve `k` for a genuinely special third170 index, e.g. Floyd's intermediate vertex `for (k) for (i) for (j) dis[i][j] = min(..., dis[i][k] + dis[k][j])`.171 - graph: `u v w` for an edge's two endpoints and weight; **`to`** for an unweighted adjacency172 list and **`es`** for a `vector<Edge>` one (see the graph-storage idiom below). `pre`/`last`173 belong to the legacy 链式前向星 form — read them, don't write them.174 - **depth in a rooted tree is `h[]`** (29 recent files) more often than `dep[]` (5).175 - **node you're at vs. node you step to: `now` = the current node, `nxt` = the next/neighbour176 node** (`for (int nxt : to[now])`, `int nxt = e.to;`). Keep this pair consistent.177 - accumulators: `ans cnt tot sum res`178 - state/pointers: `vis dis dp pos id fa`179 - **boolean flags / on-off state: `ok`, occasionally `trig`** — and declared `int`, not `bool`180 (`int ok = 1; ... ok = 0;`). Never a long descriptive name.181 - intervals: `l r mid`182 - structs: PascalCase — **`Node` (100 recent files), `Edge` (39), `Path` (13)** cover almost183 everything — with terse fields `lc rc v tag`, `x y`, `v w`, `to w`. **The global array of a184 struct is named `ns`** — `Node ns[MX * 4];`, `sort(ns + 1, ns + n + 1)`. Its allocation185 counter is `pos` or `npos` (equally common). The one exception to PascalCase is a186 **comparator functor**, which takes a lowercase name — `cmpa` / `cmpb` / `cmp11` / `hsh` —187 used when one struct needs several orderings or a custom hash.188 - a Chinese-pinyin initialism is fine for a derived table with no short English name —189 `qzmx`/`qzmn` for 前缀 max/min, `hzmx`/`hzmn` for 后缀. Don't force it where `pre`/`suf` reads fine.190- **`long long` is written out explicitly** where range demands it. **Never `#define int long long`**191 (0 occurrences in the corpus) and no `typedef long long ll`. On a long-long-range problem the rule192 is pragmatic, not dogmatic: if a handful of variables fit on **one short declaration line**, just193 make the whole line `long long` rather than splitting hairs over which one actually overflows (the194 author's lazy-but-safe default). But when declarations are many or spread across **big arrays**,195 keep only the value-carrying ones `long long` and leave structural data — indices, depths, node196 ids, visited flags, small counts — as `int` (no point burning memory on a `long long anc[MX][20]`).197198### Indexing: 1-based by default199200Algorithmic data is 1-based: `for (int i = 1; i <= n; i++)`, arrays used as `a[1..n]`,201`sort(a + 1, a + n + 1)`. Across the last 940 files that's 1,481 `for (i = 1; i <= …)` loops202against 168 `for (i = 0; i < …)`. Drop to 0-based only for things that are natively 0-indexed —203string characters, bitmask bits, and a **grid held as `string s[MX]`, which is 0-based in both204dimensions** — and cast `.size()` to `int` when comparing in a loop bound.205206---207208## I/O209210- **`cin`/`cout` by default** (~90% of files). Plain, unadorned.211- **`endl` is the line terminator** — 830 of the last 940 files (88%). Default to it; it's the212 author's reflex. When a file does use a bare newline instead, it's the **char `'\n'`**213 (27 files) more often than the string `"\n"` (17) — and it's a whole-file choice, not a214 per-line one: 828 files use only `endl`, 15 use only a newline literal, 2 mix them.215- **`ios::sync_with_stdio(false);` is occasional, not default** — ~2% of files, reserved for216 genuinely I/O-heavy problems. Written as that **one statement on its own**, sometimes chained as217 `cin.tie(0)->ios::sync_with_stdio(false);` or `cin.tie(0)->sync_with_stdio(false);`. Don't218 sprinkle it on every file; reach for it (or the hand-written `read()` below) only when input219 size actually warrants it.220- **Hand-written `read()`** (getchar loop) is rarer than it used to be — **13 of the last 940221 files (1.4%)**, down from ~9% all-time. When you do use it, **`printf` comes with it**: 13 of222 the 18 recent files that call `printf` also define `read()`. Fast input and fast output travel223 as a pair, and `cin` + `printf` is a mix that appears in only 2 files. See224 `references/templates.md`.225- **Formatted floats** use `cout << fixed << setprecision(n)` (with `<iomanip>`), *not*226 `printf("%.2f")`. This matters on PAT, where output precision is judged.227- **`scanf` is nearly gone** — 5 of the last 940 files (0.5%). **`printf` is not** (18 files,228 1.9%), but almost every occurrence is the output half of the `read()` + `printf` fast-IO pair229 above, or a `%llu`. Don't reach for `printf` on its own; don't pair it with `cin`.230- **An EOF-driven input loop is `while (cin >> n)`** — 17 recent files, against 5 for231 `while (scanf(...) != EOF)`. Use the `scanf` form only when matching an existing file.232- **Multiple test cases:** `int T; cin >> T; while (T--) { ... }` (95 recent files; the variable233 is `T` or `t`).234 Switch to `for (int t = 1; t <= T; t++)` only when the case number is part of the output235 (`cout << "Case #" << t << ": " << ans << endl;`).236237---238239## Idioms that make it look authentic240241- **Comma operator for grouped side-effects on one line** — a signature habit. Use it for tightly242 related updates:243 ```cpp244 g[gu] = gv, cnt++; // DSU merge + count245 anc[nxt][0] = now, h[nxt] = h[now] + 1; // fix parent + depth before recursing246 vis[nx][ny] = 1, dis[nx][ny] = dis[x][y] + 1;247 ans += es[i].w, d -= es[i].cnt; // greedy take248 ```249- **Inline ternary for simple branches**, including as a statement and inside output — this has250 become steadily more common:251 ```cpp252 check(mid) ? l = mid + 1 : r = mid - 1;253 s[now] = (sc[j] >= 60 ? 1 : -1);254 cout << (ans == 1e9 ? -1 : ans) << endl;255 ```256- **Infinity is a literal, not a named constant.** `1e9` for `int` range and `1e18` for257 `long long` are the current default (52 vs 27 recent files against `0x3f3f3f3f`) — assign it258 directly and compare against it directly:259 ```cpp260 int ans = 1e9, l = 1, r = 1e9;261 dp[i] = 1e9;262 if (c0 == 1e9 && c1 == 1e9) { ... }263 ```264 **`memset(x, 0x3f, sizeof(x))`** stays the form when you need to fill a whole array at once265 (needs `<cstring>`); then compare against `0x3f3f3f3f` (or `0x3f3f3f3f3f3f3f3f` for `long long`).266 Never `INT_MAX`/`LLONG_MAX`/`#define INF`.267- **Compute a shared prerequisite once, then merge guard clauses with `||`** — don't write a268 staircase of early-exits. Get the value both guards need first, then OR the conditions:269 ```cpp270 int l = lca(x, y);271 if (a[x] == 0 || z[x] + z[y] - 2 * z[l] > 0) // both "trivially Yes" cases, together272 {273 cout << "Yes" << endl;274 continue;275 }276 ```277 rather than an `if (a[x] == 0) {...continue;}`, then the `lca`, then a separate278 `if (zero on path) {...continue;}`. One guard, all the trivial cases visible at once.279- **STL, used plainly:** `queue`, `priority_queue` (min-heap via a reversed `operator<`, or the280 three-argument `priority_queue<int, vector<int>, greater<int>>` when the element is a bare281 scalar), `map`, `set`.282- **A container is tested for emptiness with `.size()`, never `.empty()`.** `while (q.size())`283 appears in 48 recent files and `while (!q.empty())` in **zero**; `.empty()` shows up anywhere at284 all in only 5. The same reflex applies to a plain truth test: `if (now.size())`, `if (v.size())`.285- **Graph storage — adjacency `vector`, and the name follows the payload.** Unweighted:286 **`vector<int> to[MX]`** — 29 recent files, against `es` 13 and `g` 7. Weighted, or carrying287 extra per-edge data: a small `struct Edge { int to, w; };` held in **`vector<Edge> es[MX]`** —288 25 files, and `vector<Edge> to[]` never appears. Keeping the two names apart is the habit: `to`289 when the element *is* the neighbour, `es` when it's an edge record.290 **Push with `push_back`** — `push_back(Edge{v, w})` 22 files vs `emplace_back(Edge{v, w})` 2,291 and unweighted `push_back(v)` 21 vs `emplace_back(v)` 7. `emplace_back` isn't foreign, but292 `push_back` is the default; stay consistent within a file.293 **链式前向星 (chained forward star) is legacy**: 72 old files use it and **zero** in the last two294 years. Recognize it when reading old code (`last[u]` head, `pre[i]` previous edge, `to[i]`295 endpoint); don't write it in a new file.296- **Iterate edges with range-for when you don't need the edge index:** `for (int nxt : to[now])` or297 `for (Edge e : es[now])`. Only fall back to an indexed loop when the index itself matters, and298 cast the bound: `for (int i = 0; i < (int)es[now].size(); i++)`.299- **A tree DFS carries its parent and skips it — no `vis[]`.** The signature is300 `void dfs(int now, int from)` and the first thing in the loop is the parent guard; ~25 recent301 files do this, and it's the reason `vis[]` is scarce in tree code (it stays for general graphs302 and grids):303 ```cpp304 void dfs(int now, int from)305 {306 for (Edge e : es[now])307 {308 if (e.to == from)309 {310 continue;311 }312 h[e.to] = h[now] + 1;313 dfs(e.to, now);314 }315 }316 ```317- **Grid movement uses a global `ms[4][2]` offsets table and `nx`/`ny`** — never `dx`/`dy`:318 ```cpp319 int ms[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};320 // ...321 for (int i = 0; i < 4; i++)322 {323 int nx = x + ms[i][0], ny = y + ms[i][1];324 if (nx < 1 || nx > n || ny < 1 || ny > m || vis[nx][ny])325 {326 continue;327 }328 }329 ```330 Diagonals go in `ms[8][2]`, or a second table `ms2[4][2]` when the two move sets are distinct.331 **The grid itself is usually a `string` array** — `string s[MX];` read with `cin >> s[i]` and332 indexed `s[x][y]` (19 recent files, against 10 for a `char mp[MX][MX]`). A `string` grid is333 naturally **0-based**, so that's one of the places the 1-based default doesn't apply; bounds334 read `nx >= 0 && nx < n`.335- **`__int128` is the overflow escape hatch** — 11 recent files. Reach for it when an intermediate336 product would blow past `long long` (`__int128 res = min((__int128)b, (__int128)n * a);`) or when337 a comparison has to be done exactly. `cin`/`cout` can't handle it, so printing needs a338 hand-written recursive `write()` on `putchar` — see `references/templates.md`. `unsigned339 __int128` shows up too when the value is known non-negative.340- **One function with a parameter, not near-duplicate functions.** When two helpers would share the341 same body and differ only by a constant, write a single function that takes that constant as an342 argument — `val(x, 2)` / `val(x, 5)`, not separate `val2` / `val5`:343 ```cpp344 int val(long long x, int p) // count how many times prime p divides x345 {346 int c = 0;347 while (x % p == 0)348 {349 x /= p, c++;350 }351 return c;352 }353 ```354- **No redundant casts.** Don't cast an operand wide when the *other* operand is already wide —355 promotion handles it. With `long long scale`, write `fp += (s[i] - '0') * scale;`, not356 `fp += (long long)(s[i] - '0') * scale;`. Only cast when the whole expression would otherwise be357 evaluated narrow (e.g. `(long long)a * b` when both are `int`).358- **Sorting:** a member `bool operator<(const T &oth) const` is the strong default — 79 of the 82359 recent files that define one, always **by const reference, with the parameter named `oth`** (the360 three strays take it by value or name it `other`). A free `bool cmp(T a, T b)` passed to361 `sort(ns + 1, ns + n + 1, cmp)` is the fallback when you sort the same data several ways, and a362 lowercase functor struct (`cmpa`, `cmpb`) when a container needs the ordering as a type. Keep it363 1-based. **No lambdas** — they never appear here, in a sort or anywhere else (0 of 940 files).364- **`auto` is rare** — mostly for iterators. Prefer explicit types.365366### PBDS — the one sanctioned `bits/` header367368For a hash table or an order-statistic tree, the author reaches for GNU PBDS. It's a small but369entirely recent habit (7 files, all from the last two years), and it's the sole exception to the370"no `bits/*` headers" rule:371372```cpp373#include <algorithm>374#include <bits/extc++.h>375#include <iostream>376using namespace std;377using namespace __gnu_pbds;378cc_hash_table<int, int> mp;379```380381The include still sorts alphabetically (`algorithm` < `bits/extc++.h` < `iostream`), and382`using namespace __gnu_pbds;` goes on the line after `using namespace std;`. When PBDS is an383*alternative* solution to a problem you already solved another way, name the file with the384` PBDS` suffix — `P3014 PBDS.cpp`.385386---387388## Comments and the `// TAG:` line389390Comments are **sparse** — 129 of the last 940 files carry one at all. A short Chinese note391(UTF-8) for a non-obvious step, a math derivation, or a struct field's meaning, **trailing on the392same line as the code it explains** (239 trailing lines vs 48 standalone). Block comments393(`/* … */`) never appear. Don't narrate line by line; the code carries itself.394395End a file you create or substantially touch with an algorithm tag — space-separated396techniques, Chinese (sometimes mixed with English names).397398**This one is an aspiration, not a reproduction of the current hand.** Only 17 of the last 940399files (1.8%) carry a `// TAG:` line, and the habit effectively stopped after 2024-10; across the400whole corpus it's 243 of 3,208 (7.6%). The reason to write it anyway is that the repo README401lists "为题解补齐算法标签" as an open Todo — so adding it is helping with a stated goal. Don't402mistake its absence in surrounding files for a signal to drop it, and don't cite it as evidence403of what the author's recent code looks like.404405```cpp406// TAG: 网络流 最大流 最小割 Dinic407// TAG: 数位DP 回文408// TAG: 树状数组 线段树 逆序对 动态维护409// TAG: 双向搜索 meet in the middle410```411412This line is the last thing in the file, with no newline after it.413414---415416## File placement & naming417418- Put the file in the right judge folder: `Luogu/`, `CodeForces/`, `LOJ/`, `PAT/Basic/`,419 `PAT/Advanced/` (or a contest subfolder like `PAT/22Q2/Basic/`), `SP UVA AT/`.420- Name it by the **exact problem id**: `P3372.cpp`, `B3609.cpp`, `CF1009F.cpp`, `#10000.cpp`,421 `ABC403G.cpp`, `ARC085E.cpp`, `UVA10079.cpp`, `SP1043.cpp`. Luogu's other id prefixes keep the422 same rule — `T777250.cpp` (团队/私有题), `U422706.cpp` (用户上传题).423- For an **alternative approach or partial solution**, append `space + Suffix`:424 `P3367 Dfn.cpp`, `B3609 Tarjan.cpp`, `B3609 Kosaraju.cpp`, `P3372 Zkw.cpp`, `P9753 90pts.cpp`,425 `P3014 PBDS.cpp`, `P1486 Seg.cpp`, `P1525 PowDfn.cpp` (带权并查集), `P2216 DQ.cpp`.426427---428429## Verified algorithm templates430431When you need a standard data structure or algorithm, use the canonical forms in432[`references/templates.md`](references/templates.md) — they're lifted directly from this codebase and433already follow every convention above (Allman, no blank lines, 1-based, global pools, `dfn`/`g[]`434DSU, `lc/rc` segment-tree pool, `to[]`/`es[]` graphs with range-for, recursive binary-lifting LCA,435`read()` + `printf`, Dijkstra, grid BFS, struct sort, `__int128` output, fixed-precision float;436链式前向星 included only as a legacy form to read). Read that file when implementing one of those;437don't reinvent the form from memory.438439---440441## Don't do these (they read as foreign here)442443- Blank lines anywhere in the file, or a trailing newline after the last `}` / `// TAG:` line.444- Wrapping a long line to fit 80 or 100 columns — there is no column limit here.445- Reordering an include block out of alphabetical order.446- `#include <bits/stdc++.h>` in a new file — use the specific headers. (`<bits/extc++.h>` for PBDS447 is the one exception.)448- `#define int long long` — declare the wide variables as `long long` instead.449- `#define mid (l + r) / 2` or a macro standing in for ordinary code — recompute450 `int mid = (l + r) / 2;` in each function. (Named *bounds* are fine: `MX`, `MX2`, `M`, `N`,451 plus `MOD`, `LC`/`RC`, `LG`.)452- K&R braces, or omitting braces on a single-line `if`/`for`/`while`/`else` — always Allman, always braced.453- Tab indentation **in a new file** — 4 spaces. (In an existing file, match what's already there;454 about one in ten is tabs.)455- `while (!q.empty())` — the house form is `while (q.size())`.456- 链式前向星 in a new file — `vector<int> to[MX]` unweighted, `vector<Edge> es[MX]` weighted.457- `vector<int> es[MX]` for an *unweighted* graph — that name is reserved for `vector<Edge>`; the458 unweighted list is `to[]`.459- `dx`/`dy` grid offsets — the table is `ms[4][2]`, the targets are `nx`/`ny`.460- `operator<` taking its argument by value, or naming it `o`/`np` — it's `const T &oth`.461- Wrapping an algorithm in a class/namespace/reusable template — keep raw globals + free functions462 (`using namespace __gnu_pbds;` for PBDS is not an exception to this; it's just a using-directive).463- Defaulting every file to `ios::sync_with_stdio(false)` — it's situational here, not a habit.464- Long descriptive identifiers (`parentArray`, `currentNode`) — use `g`/`fa`, `now`.465- `const int maxn = ...`, `typedef long long ll;`, `INT_MAX`/`LLONG_MAX` — match the inline-literal /466 `#define MX` / explicit-`long long` / `1e9`-`1e18` conventions instead.