{
  "author": "QFLOO",
  "license": "Free to use in contests and in your own projects. No warranty.",
  "zip": "qfloo-competitive-programming-templates.zip",
  "zipBytes": 79579,
  "readme": "README.md",
  "stress": [
    "stress/checker.py",
    "stress/gen.py",
    "stress/gen_gen.cpp",
    "stress/selftest.sh",
    "stress/stress.py",
    "stress/stress.sh"
  ],
  "levels": [
    {
      "id": "beginner",
      "label": "Beginner",
      "blurb": "The first file to keep: fast input and output, sorting, prefix sums, binary search, modular arithmetic, strings and grids. A solid base for the early problems of a contest.",
      "files": {
        "cpp": {
          "path": "cpp/beginner.cpp",
          "lines": 335,
          "bytes": 16729
        },
        "py": {
          "path": "py/beginner.py",
          "lines": 316,
          "bytes": 11290
        }
      },
      "sections": {
        "cpp": [
          {
            "id": "s1",
            "title": "Setup, macros and debug print",
            "names": [
              "ll",
              "vi",
              "pii",
              "all",
              "sz",
              "rep",
              "chmin",
              "chmax",
              "INF",
              "MOD",
              "dbg"
            ],
            "cost": "O(1)",
            "trap": "INF is 1e18, so INF + INF still fits in long long. dbg() prints to stderr only when you compile with -DLOCAL.",
            "level": "beginner",
            "solution": false,
            "line": 38
          },
          {
            "id": "s2",
            "title": "Fast input",
            "names": [
              "read_vec",
              "FastIn",
              "read",
              "get"
            ],
            "cost": "O(input size)",
            "trap": "After sync_with_stdio(false) never mix cin with scanf, getchar or FastIn on the same stream. Pick one reader per program.",
            "level": "beginner",
            "solution": false,
            "line": 115
          },
          {
            "id": "s3",
            "title": "Output helpers and YES/NO",
            "names": [
              "print_vec",
              "yn",
              "print_double"
            ],
            "cost": "O(output size)",
            "trap": "endl flushes the buffer on every line and can time out a big output. Use '\\n'. Print doubles with fixed and a set precision.",
            "level": "beginner",
            "solution": false,
            "line": 153
          },
          {
            "id": "s4",
            "title": "Arrays, sorting and comparators",
            "names": [
              "sort_unique",
              "argsort",
              "vsum",
              "vmax",
              "vmin",
              "comparator"
            ],
            "cost": "O(n log n) for sorting",
            "trap": "accumulate(all(v), 0) sums in int and overflows: start from 0LL. A comparator must be a strict order: use < or >, never <= or >=.",
            "level": "beginner",
            "solution": false,
            "line": 168
          },
          {
            "id": "s5",
            "title": "Prefix sums and difference arrays",
            "names": [
              "prefix_sums",
              "Prefix2D",
              "diff_add"
            ],
            "cost": "O(n) build, O(1) per range sum",
            "trap": "Ranges are half open: sum of a[l, r) is p[r] - p[l]. The prefix array has n + 1 entries. Use long long for the sums.",
            "level": "beginner",
            "solution": false,
            "line": 191
          },
          {
            "id": "s6",
            "title": "Binary search helpers",
            "names": [
              "first_true",
              "last_true",
              "first_true_real",
              "count_less",
              "count_leq"
            ],
            "cost": "O(log(hi - lo)) predicate calls",
            "trap": "The predicate must go false, false, true, true. hi + 1 must fit in the type. For doubles use a fixed loop count, not an epsilon.",
            "level": "beginner",
            "solution": false,
            "line": 217
          },
          {
            "id": "s7",
            "title": "gcd, lcm, powers and modular arithmetic",
            "names": [
              "lcm_ll",
              "pow_ll",
              "mod_norm",
              "mod_add",
              "mod_sub",
              "mod_mul",
              "mod_pow",
              "mod_inv",
              "ceil_div",
              "floor_div",
              "isqrt_ll",
              "is_prime_trial"
            ],
            "cost": "O(log) each, O(sqrt n) for is_prime_trial",
            "trap": "In C++ (-7) % 3 is -1: use mod_norm. mod_inv needs a prime modulus. Moduli up to about 3e9 are safe here; for bigger ones see the 64-bit section.",
            "level": "beginner",
            "solution": false,
            "line": 243
          },
          {
            "id": "s8",
            "title": "String helpers",
            "names": [
              "split",
              "is_palindrome",
              "to_lower",
              "to_upper",
              "freq26",
              "starts_with"
            ],
            "cost": "O(length)",
            "trap": "s.size() is unsigned: s.size() - 1 on an empty string wraps around. Cast with sz(s) first. Characters are not always lower case letters, check the statement.",
            "level": "beginner",
            "solution": false,
            "line": 278
          },
          {
            "id": "s9",
            "title": "2D grids and direction arrays",
            "names": [
              "read_grid",
              "DX4",
              "DY4",
              "DX8",
              "DY8",
              "inside",
              "find_char"
            ],
            "cost": "O(n * m)",
            "trap": "Rows are r, columns are c. Check inside(r, c, n, m) before you index. dx pairs with the row, dy with the column.",
            "level": "beginner",
            "solution": false,
            "line": 297
          },
          {
            "id": "s10",
            "title": "Your solution: solve() and main()",
            "names": [
              "solve",
              "main"
            ],
            "cost": "your code",
            "trap": "With many test cases, reset every global and container at the start of solve(). Read all of the input of a test even when you can answer early.",
            "level": "beginner",
            "solution": true,
            "line": 314
          }
        ],
        "py": [
          {
            "id": "s1",
            "title": "Setup, constants and debug print",
            "names": [
              "INF",
              "MOD",
              "debug",
              "LOCAL"
            ],
            "cost": "O(1)",
            "trap": "INF is 10**18, a plain int, so INF + INF is still exact in Python. debug() prints to stderr only when the LOCAL environment variable is set.",
            "level": "beginner",
            "solution": false,
            "line": 39
          },
          {
            "id": "s2",
            "title": "Fast input",
            "names": [
              "ri",
              "rii",
              "ria",
              "rs",
              "read_tokens",
              "rl"
            ],
            "cost": "O(input size)",
            "trap": "readline helpers and read_tokens() share one stream: choose one style per program. Do not call input(), it is slow and mixes badly with these.",
            "level": "beginner",
            "solution": false,
            "line": 62
          },
          {
            "id": "s3",
            "title": "Output helpers and YES/NO",
            "names": [
              "out",
              "yn",
              "show",
              "fixed"
            ],
            "cost": "O(output size)",
            "trap": "Many print() calls are slow. Append to out and let main() write everything once. A float prints with fixed(x, digits), not with str(x).",
            "level": "beginner",
            "solution": false,
            "line": 91
          },
          {
            "id": "s4",
            "title": "Arrays, sorting and comparators",
            "names": [
              "sort_unique",
              "argsort",
              "key",
              "cmp_to_key"
            ],
            "cost": "O(n log n) for sorting",
            "trap": "sort(key=...) is much faster than cmp_to_key. For a descending key on numbers use key=lambda x: -x. Sorting is stable.",
            "level": "beginner",
            "solution": false,
            "line": 112
          },
          {
            "id": "s5",
            "title": "Prefix sums and difference arrays",
            "names": [
              "prefix_sums",
              "Prefix2D",
              "diff_add"
            ],
            "cost": "O(n) build, O(1) per range sum",
            "trap": "Ranges are half open: the sum of a[l:r] is p[r] - p[l]. The prefix list has n + 1 entries.",
            "level": "beginner",
            "solution": false,
            "line": 133
          },
          {
            "id": "s6",
            "title": "Binary search helpers",
            "names": [
              "first_true",
              "last_true",
              "first_true_real",
              "count_less",
              "count_leq"
            ],
            "cost": "O(log(hi - lo)) predicate calls",
            "trap": "The predicate must go False, False, True, True. For floats use a fixed loop count, not an epsilon. bisect works only on sorted lists.",
            "level": "beginner",
            "solution": false,
            "line": 161
          },
          {
            "id": "s7",
            "title": "gcd, lcm, powers and modular arithmetic",
            "names": [
              "gcd",
              "lcm",
              "mod_pow",
              "mod_inv",
              "ceil_div",
              "floor_div",
              "isqrt",
              "is_prime_trial"
            ],
            "cost": "O(log) each, O(sqrt n) for is_prime_trial",
            "trap": "Python ints never overflow, but pow(a, e, m) is far faster than a ** e % m. mod_inv needs gcd(a, m) = 1. // floors toward minus infinity, so -7 // 2 is -4.",
            "level": "beginner",
            "solution": false,
            "line": 200
          },
          {
            "id": "s8",
            "title": "String helpers",
            "names": [
              "is_palindrome",
              "freq26",
              "run_lengths"
            ],
            "cost": "O(length)",
            "trap": "s.split() splits on any run of spaces, s.split(\" \") does not. Strings are immutable: build a list of characters and join once instead of s += c in a loop.",
            "level": "beginner",
            "solution": false,
            "line": 238
          },
          {
            "id": "s9",
            "title": "2D grids and direction arrays",
            "names": [
              "read_grid",
              "DX4",
              "DY4",
              "DX8",
              "DY8",
              "inside",
              "find_char"
            ],
            "cost": "O(n * m)",
            "trap": "Rows are r, columns are c. Check inside(r, c, n, m) before you index: a negative index silently wraps around in Python.",
            "level": "beginner",
            "solution": false,
            "line": 266
          },
          {
            "id": "s10",
            "title": "Your solution: solve() and main()",
            "names": [
              "solve",
              "main"
            ],
            "cost": "your code",
            "trap": "With many test cases, create fresh lists inside solve() and read all the input of a test even when you can answer early.",
            "level": "beginner",
            "solution": true,
            "line": 292
          }
        ]
      }
    },
    {
      "id": "medium",
      "label": "Medium",
      "blurb": "Adds the standard toolbox: DSU, BFS and DFS without recursion, Dijkstra, topological sort, sieve, nCr, Fenwick tree, sparse table, two pointers, coordinate compression, bit tricks, hashing and KMP. It includes everything in Beginner.",
      "files": {
        "cpp": {
          "path": "cpp/medium.cpp",
          "lines": 818,
          "bytes": 38612
        },
        "py": {
          "path": "py/medium.py",
          "lines": 926,
          "bytes": 31322
        }
      },
      "sections": {
        "cpp": [
          {
            "id": "s1",
            "title": "Setup, macros and debug print",
            "names": [
              "ll",
              "vi",
              "pii",
              "all",
              "sz",
              "rep",
              "chmin",
              "chmax",
              "INF",
              "MOD",
              "dbg"
            ],
            "cost": "O(1)",
            "trap": "INF is 1e18, so INF + INF still fits in long long. dbg() prints to stderr only when you compile with -DLOCAL.",
            "level": "beginner",
            "solution": false,
            "line": 59
          },
          {
            "id": "s2",
            "title": "Fast input",
            "names": [
              "read_vec",
              "FastIn",
              "read",
              "get"
            ],
            "cost": "O(input size)",
            "trap": "After sync_with_stdio(false) never mix cin with scanf, getchar or FastIn on the same stream. Pick one reader per program.",
            "level": "beginner",
            "solution": false,
            "line": 136
          },
          {
            "id": "s3",
            "title": "Output helpers and YES/NO",
            "names": [
              "print_vec",
              "yn",
              "print_double"
            ],
            "cost": "O(output size)",
            "trap": "endl flushes the buffer on every line and can time out a big output. Use '\\n'. Print doubles with fixed and a set precision.",
            "level": "beginner",
            "solution": false,
            "line": 174
          },
          {
            "id": "s4",
            "title": "Arrays, sorting and comparators",
            "names": [
              "sort_unique",
              "argsort",
              "vsum",
              "vmax",
              "vmin",
              "comparator"
            ],
            "cost": "O(n log n) for sorting",
            "trap": "accumulate(all(v), 0) sums in int and overflows: start from 0LL. A comparator must be a strict order: use < or >, never <= or >=.",
            "level": "beginner",
            "solution": false,
            "line": 189
          },
          {
            "id": "s5",
            "title": "Prefix sums and difference arrays",
            "names": [
              "prefix_sums",
              "Prefix2D",
              "diff_add"
            ],
            "cost": "O(n) build, O(1) per range sum",
            "trap": "Ranges are half open: sum of a[l, r) is p[r] - p[l]. The prefix array has n + 1 entries. Use long long for the sums.",
            "level": "beginner",
            "solution": false,
            "line": 212
          },
          {
            "id": "s6",
            "title": "Binary search helpers",
            "names": [
              "first_true",
              "last_true",
              "first_true_real",
              "count_less",
              "count_leq"
            ],
            "cost": "O(log(hi - lo)) predicate calls",
            "trap": "The predicate must go false, false, true, true. hi + 1 must fit in the type. For doubles use a fixed loop count, not an epsilon.",
            "level": "beginner",
            "solution": false,
            "line": 238
          },
          {
            "id": "s7",
            "title": "gcd, lcm, powers and modular arithmetic",
            "names": [
              "lcm_ll",
              "pow_ll",
              "mod_norm",
              "mod_add",
              "mod_sub",
              "mod_mul",
              "mod_pow",
              "mod_inv",
              "ceil_div",
              "floor_div",
              "isqrt_ll",
              "is_prime_trial"
            ],
            "cost": "O(log) each, O(sqrt n) for is_prime_trial",
            "trap": "In C++ (-7) % 3 is -1: use mod_norm. mod_inv needs a prime modulus. Moduli up to about 3e9 are safe here; for bigger ones see the 64-bit section.",
            "level": "beginner",
            "solution": false,
            "line": 264
          },
          {
            "id": "s8",
            "title": "String helpers",
            "names": [
              "split",
              "is_palindrome",
              "to_lower",
              "to_upper",
              "freq26",
              "starts_with"
            ],
            "cost": "O(length)",
            "trap": "s.size() is unsigned: s.size() - 1 on an empty string wraps around. Cast with sz(s) first. Characters are not always lower case letters, check the statement.",
            "level": "beginner",
            "solution": false,
            "line": 299
          },
          {
            "id": "s9",
            "title": "2D grids and direction arrays",
            "names": [
              "read_grid",
              "DX4",
              "DY4",
              "DX8",
              "DY8",
              "inside",
              "find_char"
            ],
            "cost": "O(n * m)",
            "trap": "Rows are r, columns are c. Check inside(r, c, n, m) before you index. dx pairs with the row, dy with the column.",
            "level": "beginner",
            "solution": false,
            "line": 318
          },
          {
            "id": "s10",
            "title": "Disjoint set union (DSU)",
            "names": [
              "DSU",
              "find",
              "unite",
              "same",
              "size",
              "cnt"
            ],
            "cost": "O(alpha(n)) amortized per find or unite",
            "trap": "Union by size must compare the sizes of the roots, not of the raw nodes. Call find before reading s[]. Use size(x) instead.",
            "level": "medium",
            "solution": false,
            "line": 335
          },
          {
            "id": "s11",
            "title": "Graph search: BFS, iterative DFS, components",
            "names": [
              "read_graph",
              "bfs_dist",
              "dfs_tree",
              "components",
              "DfsInfo"
            ],
            "cost": "O(n + m)",
            "trap": "Recursive DFS overflows the stack on a path of 1e5 or more vertices, so these are iterative. dfs_tree is for trees: on a general graph it would walk back along cycles.",
            "level": "medium",
            "solution": false,
            "line": 357
          },
          {
            "id": "s12",
            "title": "Grid BFS",
            "names": [
              "grid_bfs",
              "grid_path_exists"
            ],
            "cost": "O(n * m)",
            "trap": "Mark a cell visited when you push it, not when you pop it, or the queue grows huge. -1 means unreachable.",
            "level": "medium",
            "solution": false,
            "line": 415
          },
          {
            "id": "s13",
            "title": "Dijkstra and 0-1 BFS",
            "names": [
              "Wg",
              "add_edge",
              "dijkstra",
              "bfs01",
              "path_to"
            ],
            "cost": "O((n + m) log m) for Dijkstra, O(n + m) for 0-1 BFS",
            "trap": "Weights must be non-negative. Skip stale heap entries. Never add to a distance that is INF. Unreachable stays INF.",
            "level": "medium",
            "solution": false,
            "line": 439
          },
          {
            "id": "s14",
            "title": "Topological sort",
            "names": [
              "topo_sort"
            ],
            "cost": "O(n + m)",
            "trap": "Returns false when the graph has a cycle, and then the order is incomplete. Check the result before you use it.",
            "level": "medium",
            "solution": false,
            "line": 486
          },
          {
            "id": "s15",
            "title": "Sieve, primes and factorisation",
            "names": [
              "Sieve",
              "is_prime",
              "factorize",
              "factorize_trial",
              "divisors"
            ],
            "cost": "O(n) sieve, O(log x) per factorize, O(sqrt x) for trial division",
            "trap": "Size the sieve to the largest query, inclusive. factorize(x) with x above n reads out of range. 0 and 1 are not prime.",
            "level": "medium",
            "solution": false,
            "line": 504
          },
          {
            "id": "s16",
            "title": "nCr with factorials",
            "names": [
              "Fact",
              "C",
              "P",
              "inv_fact"
            ],
            "cost": "O(n) build, O(1) per query",
            "trap": "Build the tables up to the largest n you will ask about. The modulus must be a prime bigger than n. C(n, r) is 0 when r > n or r < 0.",
            "level": "medium",
            "solution": false,
            "line": 551
          },
          {
            "id": "s17",
            "title": "Fenwick tree (BIT)",
            "names": [
              "Fenwick",
              "add",
              "prefix",
              "range",
              "kth",
              "FenwickRangeAdd"
            ],
            "cost": "O(log n) per operation",
            "trap": "Internally 1-indexed, so index 0 would loop forever (add and prefix hide that). kth needs every stored value to be non-negative.",
            "level": "medium",
            "solution": false,
            "line": 570
          },
          {
            "id": "s18",
            "title": "Sparse table",
            "names": [
              "SparseTable",
              "make_sparse",
              "query"
            ],
            "cost": "O(n log n) build, O(1) per query",
            "trap": "Only for idempotent operations (min, max, gcd): the two blocks overlap, so a sum would count twice. Query needs l < r.",
            "level": "medium",
            "solution": false,
            "line": 599
          },
          {
            "id": "s19",
            "title": "Two pointers helpers",
            "names": [
              "longest_window",
              "shortest_window_ge",
              "count_pairs_sum_leq"
            ],
            "cost": "O(n) after sorting where needed",
            "trap": "Sliding windows need a monotone condition: if a window is good, every smaller window inside it is good. shortest_window_ge needs non-negative numbers and s >= 1.",
            "level": "medium",
            "solution": false,
            "line": 625
          },
          {
            "id": "s20",
            "title": "Coordinate compression",
            "names": [
              "Compress",
              "id",
              "value",
              "size"
            ],
            "cost": "O(n log n) build, O(log n) per id",
            "trap": "id(x) is only meaningful if x was in the input. Equal values get the same id, ids start at 0.",
            "level": "medium",
            "solution": false,
            "line": 662
          },
          {
            "id": "s21",
            "title": "Bit tricks",
            "names": [
              "popcnt",
              "ctz64",
              "msb64",
              "lowbit",
              "is_pow2",
              "next_pow2",
              "bit"
            ],
            "cost": "O(1)",
            "trap": "Use 1LL << k (or 1ULL) for k >= 31, plain 1 << k overflows int. ctz64 and msb64 are undefined for 0.",
            "level": "medium",
            "solution": false,
            "line": 677
          },
          {
            "id": "s22",
            "title": "64-bit modular arithmetic, extended gcd",
            "names": [
              "mulmod64",
              "powmod64",
              "ext_gcd",
              "inv_mod_any"
            ],
            "cost": "O(log) each",
            "trap": "a * b overflows long long once the modulus passes 3e9, so mulmod64 widens to 128 bits. inv_mod_any gives -1 when gcd(a, m) is not 1.",
            "level": "medium",
            "solution": false,
            "line": 695
          },
          {
            "id": "s23",
            "title": "String hashing",
            "names": [
              "Hash61",
              "get"
            ],
            "cost": "O(n) build, O(1) per get",
            "trap": "A fixed base or a modulus of 2^64 can be broken on purpose. Equal hashes only suggest equal strings: compare when it is cheap. get(l, r) is half open.",
            "level": "medium",
            "solution": false,
            "line": 720
          },
          {
            "id": "s24",
            "title": "KMP, Z function and pattern search",
            "names": [
              "prefix_function",
              "z_function",
              "find_all"
            ],
            "cost": "O(n) each",
            "trap": "z[0] is n here, not 0. To search with Z, join pattern + separator + text and use a separator that is in neither string.",
            "level": "medium",
            "solution": false,
            "line": 754
          },
          {
            "id": "s25",
            "title": "Your solution: solve() and main()",
            "names": [
              "solve",
              "main"
            ],
            "cost": "your code",
            "trap": "With many test cases, reset every global and container at the start of solve(). Read all of the input of a test even when you can answer early.",
            "level": "beginner",
            "solution": true,
            "line": 797
          }
        ],
        "py": [
          {
            "id": "s1",
            "title": "Setup, constants and debug print",
            "names": [
              "INF",
              "MOD",
              "debug",
              "LOCAL"
            ],
            "cost": "O(1)",
            "trap": "INF is 10**18, a plain int, so INF + INF is still exact in Python. debug() prints to stderr only when the LOCAL environment variable is set.",
            "level": "beginner",
            "solution": false,
            "line": 59
          },
          {
            "id": "s2",
            "title": "Fast input",
            "names": [
              "ri",
              "rii",
              "ria",
              "rs",
              "read_tokens",
              "rl"
            ],
            "cost": "O(input size)",
            "trap": "readline helpers and read_tokens() share one stream: choose one style per program. Do not call input(), it is slow and mixes badly with these.",
            "level": "beginner",
            "solution": false,
            "line": 82
          },
          {
            "id": "s3",
            "title": "Output helpers and YES/NO",
            "names": [
              "out",
              "yn",
              "show",
              "fixed"
            ],
            "cost": "O(output size)",
            "trap": "Many print() calls are slow. Append to out and let main() write everything once. A float prints with fixed(x, digits), not with str(x).",
            "level": "beginner",
            "solution": false,
            "line": 111
          },
          {
            "id": "s4",
            "title": "Arrays, sorting and comparators",
            "names": [
              "sort_unique",
              "argsort",
              "key",
              "cmp_to_key"
            ],
            "cost": "O(n log n) for sorting",
            "trap": "sort(key=...) is much faster than cmp_to_key. For a descending key on numbers use key=lambda x: -x. Sorting is stable.",
            "level": "beginner",
            "solution": false,
            "line": 132
          },
          {
            "id": "s5",
            "title": "Prefix sums and difference arrays",
            "names": [
              "prefix_sums",
              "Prefix2D",
              "diff_add"
            ],
            "cost": "O(n) build, O(1) per range sum",
            "trap": "Ranges are half open: the sum of a[l:r] is p[r] - p[l]. The prefix list has n + 1 entries.",
            "level": "beginner",
            "solution": false,
            "line": 153
          },
          {
            "id": "s6",
            "title": "Binary search helpers",
            "names": [
              "first_true",
              "last_true",
              "first_true_real",
              "count_less",
              "count_leq"
            ],
            "cost": "O(log(hi - lo)) predicate calls",
            "trap": "The predicate must go False, False, True, True. For floats use a fixed loop count, not an epsilon. bisect works only on sorted lists.",
            "level": "beginner",
            "solution": false,
            "line": 181
          },
          {
            "id": "s7",
            "title": "gcd, lcm, powers and modular arithmetic",
            "names": [
              "gcd",
              "lcm",
              "mod_pow",
              "mod_inv",
              "ceil_div",
              "floor_div",
              "isqrt",
              "is_prime_trial"
            ],
            "cost": "O(log) each, O(sqrt n) for is_prime_trial",
            "trap": "Python ints never overflow, but pow(a, e, m) is far faster than a ** e % m. mod_inv needs gcd(a, m) = 1. // floors toward minus infinity, so -7 // 2 is -4.",
            "level": "beginner",
            "solution": false,
            "line": 220
          },
          {
            "id": "s8",
            "title": "String helpers",
            "names": [
              "is_palindrome",
              "freq26",
              "run_lengths"
            ],
            "cost": "O(length)",
            "trap": "s.split() splits on any run of spaces, s.split(\" \") does not. Strings are immutable: build a list of characters and join once instead of s += c in a loop.",
            "level": "beginner",
            "solution": false,
            "line": 258
          },
          {
            "id": "s9",
            "title": "2D grids and direction arrays",
            "names": [
              "read_grid",
              "DX4",
              "DY4",
              "DX8",
              "DY8",
              "inside",
              "find_char"
            ],
            "cost": "O(n * m)",
            "trap": "Rows are r, columns are c. Check inside(r, c, n, m) before you index: a negative index silently wraps around in Python.",
            "level": "beginner",
            "solution": false,
            "line": 286
          },
          {
            "id": "s10",
            "title": "Disjoint set union (DSU)",
            "names": [
              "DSU",
              "find",
              "unite",
              "same",
              "size",
              "cnt"
            ],
            "cost": "O(alpha(n)) amortized per find or unite",
            "trap": "Union by size must compare the sizes of the roots, not of the raw nodes. Use size(x) instead of reading s[x] directly.",
            "level": "medium",
            "solution": false,
            "line": 312
          },
          {
            "id": "s11",
            "title": "Graph search: BFS, iterative DFS, components",
            "names": [
              "read_graph",
              "bfs_dist",
              "dfs_tree",
              "components"
            ],
            "cost": "O(n + m)",
            "trap": "Recursion is not used: Python stops at about 1000 frames. dfs_tree is for trees: on a general graph it would walk back along cycles.",
            "level": "medium",
            "solution": false,
            "line": 351
          },
          {
            "id": "s12",
            "title": "Grid BFS",
            "names": [
              "grid_bfs"
            ],
            "cost": "O(n * m)",
            "trap": "Mark a cell visited when you push it, not when you pop it. -1 means unreachable.",
            "level": "medium",
            "solution": false,
            "line": 418
          },
          {
            "id": "s13",
            "title": "Dijkstra and 0-1 BFS",
            "names": [
              "add_edge",
              "dijkstra",
              "bfs01",
              "path_to"
            ],
            "cost": "O((n + m) log m) for Dijkstra, O(n + m) for 0-1 BFS",
            "trap": "Weights must be non-negative. Skip stale heap entries. Unreachable stays INF: test for it before you add to a distance.",
            "level": "medium",
            "solution": false,
            "line": 439
          },
          {
            "id": "s14",
            "title": "Topological sort",
            "names": [
              "topo_sort"
            ],
            "cost": "O(n + m)",
            "trap": "Returns None when the graph has a cycle. Check the result before you use it.",
            "level": "medium",
            "solution": false,
            "line": 502
          },
          {
            "id": "s15",
            "title": "Sieve, primes and factorisation",
            "names": [
              "sieve",
              "primes_upto",
              "build_spf",
              "factorize",
              "factorize_trial",
              "divisors"
            ],
            "cost": "O(n log log n) sieve, O(log x) per factorize, O(sqrt x) for trial division",
            "trap": "Size the sieve to the largest query, inclusive. factorize(x, spf) with x above n is out of range. 0 and 1 are not prime.",
            "level": "medium",
            "solution": false,
            "line": 522
          },
          {
            "id": "s16",
            "title": "nCr with factorials",
            "names": [
              "Fact",
              "C",
              "P",
              "inv_fact"
            ],
            "cost": "O(n) build, O(1) per query",
            "trap": "Build the tables up to the largest n you will ask about. The modulus must be a prime bigger than n. C(n, r) is 0 when r > n or r < 0.",
            "level": "medium",
            "solution": false,
            "line": 595
          },
          {
            "id": "s17",
            "title": "Fenwick tree (BIT)",
            "names": [
              "Fenwick",
              "add",
              "prefix",
              "range_sum",
              "kth",
              "FenwickRangeAdd"
            ],
            "cost": "O(log n) per operation",
            "trap": "Internally 1-indexed, so index 0 would loop forever (add and prefix hide that). kth needs every stored value to be non-negative.",
            "level": "medium",
            "solution": false,
            "line": 623
          },
          {
            "id": "s18",
            "title": "Sparse table",
            "names": [
              "SparseTable",
              "query"
            ],
            "cost": "O(n log n) build, O(1) per query",
            "trap": "Only for idempotent operations (min, max, gcd): the two blocks overlap, so a sum would count twice. Query needs l < r.",
            "level": "medium",
            "solution": false,
            "line": 681
          },
          {
            "id": "s19",
            "title": "Two pointers helpers",
            "names": [
              "longest_window",
              "shortest_window_ge",
              "count_pairs_sum_leq"
            ],
            "cost": "O(n) after sorting where needed",
            "trap": "Sliding windows need a monotone condition: if a window is good, every smaller window inside it is good. shortest_window_ge needs non-negative numbers and s >= 1.",
            "level": "medium",
            "solution": false,
            "line": 703
          },
          {
            "id": "s20",
            "title": "Coordinate compression",
            "names": [
              "Compress",
              "id",
              "value",
              "size"
            ],
            "cost": "O(n log n) build, O(log n) per id",
            "trap": "id(x) is only meaningful if x was in the input. Equal values get the same id, ids start at 0.",
            "level": "medium",
            "solution": false,
            "line": 741
          },
          {
            "id": "s21",
            "title": "Bit tricks",
            "names": [
              "popcnt",
              "lowbit",
              "msb",
              "is_pow2",
              "next_pow2",
              "bit"
            ],
            "cost": "O(1)",
            "trap": "Python ints have no fixed width, so ~x is negative: use x ^ mask to flip a fixed number of bits. msb(0) is -1.",
            "level": "medium",
            "solution": false,
            "line": 761
          },
          {
            "id": "s22",
            "title": "Modular tools for any modulus: extended gcd",
            "names": [
              "mulmod64",
              "powmod64",
              "ext_gcd",
              "inv_mod_any"
            ],
            "cost": "O(log) each",
            "trap": "inv_mod_any gives -1 when gcd(a, m) is not 1. mulmod64 and powmod64 are here so the Pollard rho section reads the same as in C++.",
            "level": "medium",
            "solution": false,
            "line": 793
          },
          {
            "id": "s23",
            "title": "String hashing",
            "names": [
              "Hash61",
              "get"
            ],
            "cost": "O(n) build, O(1) per get",
            "trap": "A fixed base can be broken on purpose, so the base is random. Equal hashes only suggest equal strings. get(l, r) is half open.",
            "level": "medium",
            "solution": false,
            "line": 822
          },
          {
            "id": "s24",
            "title": "KMP, Z function and pattern search",
            "names": [
              "prefix_function",
              "z_function",
              "find_all"
            ],
            "cost": "O(n) each",
            "trap": "z[0] is n here, not 0. To search with Z, join pattern + separator + text and use a separator that is in neither string.",
            "level": "medium",
            "solution": false,
            "line": 851
          },
          {
            "id": "s25",
            "title": "Your solution: solve() and main()",
            "names": [
              "solve",
              "main"
            ],
            "cost": "your code",
            "trap": "With many test cases, create fresh lists inside solve() and read all the input of a test even when you can answer early.",
            "level": "beginner",
            "solution": true,
            "line": 902
          }
        ]
      }
    },
    {
      "id": "hard",
      "label": "Hard",
      "blurb": "Adds the heavy tools: lazy segment tree, LCA, SCC and 2-SAT, bridges, bitmask DP, matrix power, NTT, Mo's algorithm, convex hull, Dinic max flow, Floyd, MST, heavy-light decomposition, suffix array and Pollard rho. It includes everything in Beginner and Medium.",
      "files": {
        "cpp": {
          "path": "cpp/hard.cpp",
          "lines": 1552,
          "bytes": 73046
        },
        "py": {
          "path": "py/hard.py",
          "lines": 1855,
          "bytes": 63715
        }
      },
      "sections": {
        "cpp": [
          {
            "id": "s1",
            "title": "Setup, macros and debug print",
            "names": [
              "ll",
              "vi",
              "pii",
              "all",
              "sz",
              "rep",
              "chmin",
              "chmax",
              "INF",
              "MOD",
              "dbg"
            ],
            "cost": "O(1)",
            "trap": "INF is 1e18, so INF + INF still fits in long long. dbg() prints to stderr only when you compile with -DLOCAL.",
            "level": "beginner",
            "solution": false,
            "line": 77
          },
          {
            "id": "s2",
            "title": "Fast input",
            "names": [
              "read_vec",
              "FastIn",
              "read",
              "get"
            ],
            "cost": "O(input size)",
            "trap": "After sync_with_stdio(false) never mix cin with scanf, getchar or FastIn on the same stream. Pick one reader per program.",
            "level": "beginner",
            "solution": false,
            "line": 154
          },
          {
            "id": "s3",
            "title": "Output helpers and YES/NO",
            "names": [
              "print_vec",
              "yn",
              "print_double"
            ],
            "cost": "O(output size)",
            "trap": "endl flushes the buffer on every line and can time out a big output. Use '\\n'. Print doubles with fixed and a set precision.",
            "level": "beginner",
            "solution": false,
            "line": 192
          },
          {
            "id": "s4",
            "title": "Arrays, sorting and comparators",
            "names": [
              "sort_unique",
              "argsort",
              "vsum",
              "vmax",
              "vmin",
              "comparator"
            ],
            "cost": "O(n log n) for sorting",
            "trap": "accumulate(all(v), 0) sums in int and overflows: start from 0LL. A comparator must be a strict order: use < or >, never <= or >=.",
            "level": "beginner",
            "solution": false,
            "line": 207
          },
          {
            "id": "s5",
            "title": "Prefix sums and difference arrays",
            "names": [
              "prefix_sums",
              "Prefix2D",
              "diff_add"
            ],
            "cost": "O(n) build, O(1) per range sum",
            "trap": "Ranges are half open: sum of a[l, r) is p[r] - p[l]. The prefix array has n + 1 entries. Use long long for the sums.",
            "level": "beginner",
            "solution": false,
            "line": 230
          },
          {
            "id": "s6",
            "title": "Binary search helpers",
            "names": [
              "first_true",
              "last_true",
              "first_true_real",
              "count_less",
              "count_leq"
            ],
            "cost": "O(log(hi - lo)) predicate calls",
            "trap": "The predicate must go false, false, true, true. hi + 1 must fit in the type. For doubles use a fixed loop count, not an epsilon.",
            "level": "beginner",
            "solution": false,
            "line": 256
          },
          {
            "id": "s7",
            "title": "gcd, lcm, powers and modular arithmetic",
            "names": [
              "lcm_ll",
              "pow_ll",
              "mod_norm",
              "mod_add",
              "mod_sub",
              "mod_mul",
              "mod_pow",
              "mod_inv",
              "ceil_div",
              "floor_div",
              "isqrt_ll",
              "is_prime_trial"
            ],
            "cost": "O(log) each, O(sqrt n) for is_prime_trial",
            "trap": "In C++ (-7) % 3 is -1: use mod_norm. mod_inv needs a prime modulus. Moduli up to about 3e9 are safe here; for bigger ones see the 64-bit section.",
            "level": "beginner",
            "solution": false,
            "line": 282
          },
          {
            "id": "s8",
            "title": "String helpers",
            "names": [
              "split",
              "is_palindrome",
              "to_lower",
              "to_upper",
              "freq26",
              "starts_with"
            ],
            "cost": "O(length)",
            "trap": "s.size() is unsigned: s.size() - 1 on an empty string wraps around. Cast with sz(s) first. Characters are not always lower case letters, check the statement.",
            "level": "beginner",
            "solution": false,
            "line": 317
          },
          {
            "id": "s9",
            "title": "2D grids and direction arrays",
            "names": [
              "read_grid",
              "DX4",
              "DY4",
              "DX8",
              "DY8",
              "inside",
              "find_char"
            ],
            "cost": "O(n * m)",
            "trap": "Rows are r, columns are c. Check inside(r, c, n, m) before you index. dx pairs with the row, dy with the column.",
            "level": "beginner",
            "solution": false,
            "line": 336
          },
          {
            "id": "s10",
            "title": "Disjoint set union (DSU)",
            "names": [
              "DSU",
              "find",
              "unite",
              "same",
              "size",
              "cnt"
            ],
            "cost": "O(alpha(n)) amortized per find or unite",
            "trap": "Union by size must compare the sizes of the roots, not of the raw nodes. Call find before reading s[]. Use size(x) instead.",
            "level": "medium",
            "solution": false,
            "line": 353
          },
          {
            "id": "s11",
            "title": "Graph search: BFS, iterative DFS, components",
            "names": [
              "read_graph",
              "bfs_dist",
              "dfs_tree",
              "components",
              "DfsInfo"
            ],
            "cost": "O(n + m)",
            "trap": "Recursive DFS overflows the stack on a path of 1e5 or more vertices, so these are iterative. dfs_tree is for trees: on a general graph it would walk back along cycles.",
            "level": "medium",
            "solution": false,
            "line": 375
          },
          {
            "id": "s12",
            "title": "Grid BFS",
            "names": [
              "grid_bfs",
              "grid_path_exists"
            ],
            "cost": "O(n * m)",
            "trap": "Mark a cell visited when you push it, not when you pop it, or the queue grows huge. -1 means unreachable.",
            "level": "medium",
            "solution": false,
            "line": 433
          },
          {
            "id": "s13",
            "title": "Dijkstra and 0-1 BFS",
            "names": [
              "Wg",
              "add_edge",
              "dijkstra",
              "bfs01",
              "path_to"
            ],
            "cost": "O((n + m) log m) for Dijkstra, O(n + m) for 0-1 BFS",
            "trap": "Weights must be non-negative. Skip stale heap entries. Never add to a distance that is INF. Unreachable stays INF.",
            "level": "medium",
            "solution": false,
            "line": 457
          },
          {
            "id": "s14",
            "title": "Topological sort",
            "names": [
              "topo_sort"
            ],
            "cost": "O(n + m)",
            "trap": "Returns false when the graph has a cycle, and then the order is incomplete. Check the result before you use it.",
            "level": "medium",
            "solution": false,
            "line": 504
          },
          {
            "id": "s15",
            "title": "Sieve, primes and factorisation",
            "names": [
              "Sieve",
              "is_prime",
              "factorize",
              "factorize_trial",
              "divisors"
            ],
            "cost": "O(n) sieve, O(log x) per factorize, O(sqrt x) for trial division",
            "trap": "Size the sieve to the largest query, inclusive. factorize(x) with x above n reads out of range. 0 and 1 are not prime.",
            "level": "medium",
            "solution": false,
            "line": 522
          },
          {
            "id": "s16",
            "title": "nCr with factorials",
            "names": [
              "Fact",
              "C",
              "P",
              "inv_fact"
            ],
            "cost": "O(n) build, O(1) per query",
            "trap": "Build the tables up to the largest n you will ask about. The modulus must be a prime bigger than n. C(n, r) is 0 when r > n or r < 0.",
            "level": "medium",
            "solution": false,
            "line": 569
          },
          {
            "id": "s17",
            "title": "Fenwick tree (BIT)",
            "names": [
              "Fenwick",
              "add",
              "prefix",
              "range",
              "kth",
              "FenwickRangeAdd"
            ],
            "cost": "O(log n) per operation",
            "trap": "Internally 1-indexed, so index 0 would loop forever (add and prefix hide that). kth needs every stored value to be non-negative.",
            "level": "medium",
            "solution": false,
            "line": 588
          },
          {
            "id": "s18",
            "title": "Sparse table",
            "names": [
              "SparseTable",
              "make_sparse",
              "query"
            ],
            "cost": "O(n log n) build, O(1) per query",
            "trap": "Only for idempotent operations (min, max, gcd): the two blocks overlap, so a sum would count twice. Query needs l < r.",
            "level": "medium",
            "solution": false,
            "line": 617
          },
          {
            "id": "s19",
            "title": "Two pointers helpers",
            "names": [
              "longest_window",
              "shortest_window_ge",
              "count_pairs_sum_leq"
            ],
            "cost": "O(n) after sorting where needed",
            "trap": "Sliding windows need a monotone condition: if a window is good, every smaller window inside it is good. shortest_window_ge needs non-negative numbers and s >= 1.",
            "level": "medium",
            "solution": false,
            "line": 643
          },
          {
            "id": "s20",
            "title": "Coordinate compression",
            "names": [
              "Compress",
              "id",
              "value",
              "size"
            ],
            "cost": "O(n log n) build, O(log n) per id",
            "trap": "id(x) is only meaningful if x was in the input. Equal values get the same id, ids start at 0.",
            "level": "medium",
            "solution": false,
            "line": 680
          },
          {
            "id": "s21",
            "title": "Bit tricks",
            "names": [
              "popcnt",
              "ctz64",
              "msb64",
              "lowbit",
              "is_pow2",
              "next_pow2",
              "bit"
            ],
            "cost": "O(1)",
            "trap": "Use 1LL << k (or 1ULL) for k >= 31, plain 1 << k overflows int. ctz64 and msb64 are undefined for 0.",
            "level": "medium",
            "solution": false,
            "line": 695
          },
          {
            "id": "s22",
            "title": "64-bit modular arithmetic, extended gcd",
            "names": [
              "mulmod64",
              "powmod64",
              "ext_gcd",
              "inv_mod_any"
            ],
            "cost": "O(log) each",
            "trap": "a * b overflows long long once the modulus passes 3e9, so mulmod64 widens to 128 bits. inv_mod_any gives -1 when gcd(a, m) is not 1.",
            "level": "medium",
            "solution": false,
            "line": 713
          },
          {
            "id": "s23",
            "title": "String hashing",
            "names": [
              "Hash61",
              "get"
            ],
            "cost": "O(n) build, O(1) per get",
            "trap": "A fixed base or a modulus of 2^64 can be broken on purpose. Equal hashes only suggest equal strings: compare when it is cheap. get(l, r) is half open.",
            "level": "medium",
            "solution": false,
            "line": 738
          },
          {
            "id": "s24",
            "title": "KMP, Z function and pattern search",
            "names": [
              "prefix_function",
              "z_function",
              "find_all"
            ],
            "cost": "O(n) each",
            "trap": "z[0] is n here, not 0. To search with Z, join pattern + separator + text and use a separator that is in neither string.",
            "level": "medium",
            "solution": false,
            "line": 772
          },
          {
            "id": "s25",
            "title": "Modular integer class (modint)",
            "names": [
              "ModInt",
              "mint",
              "pow",
              "inv"
            ],
            "cost": "O(1) per + - *, O(log P) per pow or inverse",
            "trap": "Division needs a prime P. Do not write (a - b) % P with plain ints: it can be negative. Keep values inside the class.",
            "level": "hard",
            "solution": false,
            "line": 815
          },
          {
            "id": "s26",
            "title": "Segment tree (point update, range query)",
            "names": [
              "SegTree",
              "set",
              "get",
              "query"
            ],
            "cost": "O(n) build, O(log n) per set or query",
            "trap": "The operation must be associative and e must be its identity (0 for sum, -INF for max, INF for min). Order is kept, so it also works for non-commutative operations. query is half open.",
            "level": "hard",
            "solution": false,
            "line": 845
          },
          {
            "id": "s27",
            "title": "Segment tree with lazy propagation (range add, range sum and min)",
            "names": [
              "LazySeg",
              "add",
              "range_sum",
              "range_min"
            ],
            "cost": "O(n) build, O(log n) per add, range_sum or range_min",
            "trap": "Push the tag down before you visit the children, and scale the tag by the segment length when you add to a sum. All ranges are half open.",
            "level": "hard",
            "solution": false,
            "line": 873
          },
          {
            "id": "s28",
            "title": "LCA by binary lifting",
            "names": [
              "Lca",
              "lca",
              "dist",
              "kth_ancestor"
            ],
            "cost": "O(n log n) build, O(log n) per query",
            "trap": "The tree must be connected and rooted at r. The build is iterative, so a path of 1e5 vertices is safe. kth_ancestor returns -1 above the root.",
            "level": "hard",
            "solution": false,
            "line": 923
          },
          {
            "id": "s29",
            "title": "Strongly connected components and 2-SAT",
            "names": [
              "scc_tarjan",
              "TwoSat",
              "add_clause",
              "solve",
              "value"
            ],
            "cost": "O(n + m)",
            "trap": "Component ids come out in reverse topological order (0 is a sink). In 2-SAT add both implications for a clause: node 2i is \"x_i true\", 2i+1 is \"x_i false\".",
            "level": "hard",
            "solution": false,
            "line": 960
          },
          {
            "id": "s30",
            "title": "Bridges and articulation points",
            "names": [
              "find_bridges",
              "BridgeInfo",
              "bridges",
              "is_cut"
            ],
            "cost": "O(n + m)",
            "trap": "Skip the parent edge by its id, not by its endpoint, or parallel edges are missed. A root is a cut vertex only with two or more DFS children.",
            "level": "hard",
            "solution": false,
            "line": 1014
          },
          {
            "id": "s31",
            "title": "2D Fenwick tree",
            "names": [
              "Fenwick2D",
              "add",
              "prefix",
              "rect"
            ],
            "cost": "O(log n * log m) per operation",
            "trap": "add and prefix are in 0-based cells. prefix(r, c) covers rows [0, r) and columns [0, c). Memory is n * m numbers.",
            "level": "hard",
            "solution": false,
            "line": 1056
          },
          {
            "id": "s32",
            "title": "Bitmask DP helpers",
            "names": [
              "for_submasks",
              "sos_subsets",
              "sos_supersets",
              "tsp_cycle"
            ],
            "cost": "O(3^n) over all submasks, O(n * 2^n) for the sums, O(n^2 * 2^n) for tsp_cycle",
            "trap": "1 << n overflows int for n >= 31 and memory is the real limit near n = 20. The submask loop must handle mask 0, and must stop after it.",
            "level": "hard",
            "solution": false,
            "line": 1071
          },
          {
            "id": "s33",
            "title": "Matrix exponentiation",
            "names": [
              "Mat",
              "pow",
              "identity",
              "fib_mod"
            ],
            "cost": "O(k^3) per multiply, O(k^3 log e) per pow",
            "trap": "Entries must be in [0, mod). Products use 128 bits, so a modulus up to 9e18 is safe. With mod 1 the identity is all zeros.",
            "level": "hard",
            "solution": false,
            "line": 1109
          },
          {
            "id": "s34",
            "title": "NTT: polynomial multiplication mod 998244353",
            "names": [
              "ntt",
              "ntt_multiply",
              "convolution"
            ],
            "cost": "O(n log n)",
            "trap": "Works for modulus 998244353 only. The result length is len(a) + len(b) - 1. Inputs may be negative or large: they are reduced first.",
            "level": "hard",
            "solution": false,
            "line": 1140
          },
          {
            "id": "s35",
            "title": "Mo's algorithm",
            "names": [
              "mo_run",
              "add",
              "rem",
              "get"
            ],
            "cost": "O((n + q) sqrt n) add and rem calls",
            "trap": "Queries are half open [l, r). add(i) and rem(i) get an index into the array. Answers come back in the input order, not the processed order.",
            "level": "hard",
            "solution": false,
            "line": 1187
          },
          {
            "id": "s36",
            "title": "Geometry: points, convex hull, segments",
            "names": [
              "Pt",
              "cross",
              "dot",
              "orient",
              "convex_hull",
              "area2",
              "segments_intersect",
              "on_segment"
            ],
            "cost": "O(n log n) for the hull",
            "trap": "Use integer coordinates and exact cross products where you can. Never compare doubles with ==. The hull drops collinear points and duplicates.",
            "level": "hard",
            "solution": false,
            "line": 1220
          },
          {
            "id": "s37",
            "title": "Dinic max flow",
            "names": [
              "Dinic",
              "add_edge",
              "max_flow",
              "min_cut_side"
            ],
            "cost": "O(V^2 E) worst case, far faster on typical graphs and O(E sqrt V) on unit networks",
            "trap": "Add each edge once with its capacity, the reverse edge is created for you. Flow on edge id is cap - e[id].c. min_cut_side only after max_flow.",
            "level": "hard",
            "solution": false,
            "line": 1268
          },
          {
            "id": "s38",
            "title": "Floyd-Warshall (all pairs shortest paths)",
            "names": [
              "floyd_warshall",
              "has_negative_cycle"
            ],
            "cost": "O(n^3)",
            "trap": "Set d[i][i] = 0 and INF where there is no edge. A negative d[i][i] afterwards means a negative cycle. Only for n up to a few hundred.",
            "level": "hard",
            "solution": false,
            "line": 1331
          },
          {
            "id": "s39",
            "title": "Minimum spanning tree (Kruskal)",
            "names": [
              "Edge",
              "kruskal"
            ],
            "cost": "O(m log m)",
            "trap": "Returns -1 when the graph is not connected. Sort by weight only, the endpoints do not matter. Needs the DSU section.",
            "level": "hard",
            "solution": false,
            "line": 1351
          },
          {
            "id": "s40",
            "title": "Heavy-light decomposition",
            "names": [
              "HLD",
              "build",
              "path",
              "subtree",
              "lca",
              "HldMax",
              "path_max",
              "subtree_max"
            ],
            "cost": "O(n) build, O(log^2 n) per path query",
            "trap": "Position ranges from path() are inclusive on both ends and come in no fixed order, so only use operations that do not care about direction. Values sit on vertices.",
            "level": "hard",
            "solution": false,
            "line": 1368
          },
          {
            "id": "s41",
            "title": "Suffix array and LCP",
            "names": [
              "suffix_array",
              "kasai",
              "distinct_substrings"
            ],
            "cost": "O(n log n) build, O(n) for the LCP array",
            "trap": "lcp[i] is the common prefix of the suffixes sa[i - 1] and sa[i], so lcp[0] is 0. Do not append a sentinel yourself, the build handles the end of the string.",
            "level": "hard",
            "solution": false,
            "line": 1429
          },
          {
            "id": "s42",
            "title": "Miller-Rabin primality and Pollard rho factoring",
            "names": [
              "is_prime_u64",
              "pollard_rho",
              "factorize_u64"
            ],
            "cost": "O(log n) per primality test, about O(n^(1/4)) per factor found",
            "trap": "Needs the 64-bit modular section (mulmod64, powmod64): plain a * b % m overflows. The 12 fixed bases are exact for every n below 2^64.",
            "level": "hard",
            "solution": false,
            "line": 1485
          },
          {
            "id": "s43",
            "title": "Your solution: solve() and main()",
            "names": [
              "solve",
              "main"
            ],
            "cost": "your code",
            "trap": "With many test cases, reset every global and container at the start of solve(). Read all of the input of a test even when you can answer early.",
            "level": "beginner",
            "solution": true,
            "line": 1531
          }
        ],
        "py": [
          {
            "id": "s1",
            "title": "Setup, constants and debug print",
            "names": [
              "INF",
              "MOD",
              "debug",
              "LOCAL"
            ],
            "cost": "O(1)",
            "trap": "INF is 10**18, a plain int, so INF + INF is still exact in Python. debug() prints to stderr only when the LOCAL environment variable is set.",
            "level": "beginner",
            "solution": false,
            "line": 76
          },
          {
            "id": "s2",
            "title": "Fast input",
            "names": [
              "ri",
              "rii",
              "ria",
              "rs",
              "read_tokens",
              "rl"
            ],
            "cost": "O(input size)",
            "trap": "readline helpers and read_tokens() share one stream: choose one style per program. Do not call input(), it is slow and mixes badly with these.",
            "level": "beginner",
            "solution": false,
            "line": 99
          },
          {
            "id": "s3",
            "title": "Output helpers and YES/NO",
            "names": [
              "out",
              "yn",
              "show",
              "fixed"
            ],
            "cost": "O(output size)",
            "trap": "Many print() calls are slow. Append to out and let main() write everything once. A float prints with fixed(x, digits), not with str(x).",
            "level": "beginner",
            "solution": false,
            "line": 128
          },
          {
            "id": "s4",
            "title": "Arrays, sorting and comparators",
            "names": [
              "sort_unique",
              "argsort",
              "key",
              "cmp_to_key"
            ],
            "cost": "O(n log n) for sorting",
            "trap": "sort(key=...) is much faster than cmp_to_key. For a descending key on numbers use key=lambda x: -x. Sorting is stable.",
            "level": "beginner",
            "solution": false,
            "line": 149
          },
          {
            "id": "s5",
            "title": "Prefix sums and difference arrays",
            "names": [
              "prefix_sums",
              "Prefix2D",
              "diff_add"
            ],
            "cost": "O(n) build, O(1) per range sum",
            "trap": "Ranges are half open: the sum of a[l:r] is p[r] - p[l]. The prefix list has n + 1 entries.",
            "level": "beginner",
            "solution": false,
            "line": 170
          },
          {
            "id": "s6",
            "title": "Binary search helpers",
            "names": [
              "first_true",
              "last_true",
              "first_true_real",
              "count_less",
              "count_leq"
            ],
            "cost": "O(log(hi - lo)) predicate calls",
            "trap": "The predicate must go False, False, True, True. For floats use a fixed loop count, not an epsilon. bisect works only on sorted lists.",
            "level": "beginner",
            "solution": false,
            "line": 198
          },
          {
            "id": "s7",
            "title": "gcd, lcm, powers and modular arithmetic",
            "names": [
              "gcd",
              "lcm",
              "mod_pow",
              "mod_inv",
              "ceil_div",
              "floor_div",
              "isqrt",
              "is_prime_trial"
            ],
            "cost": "O(log) each, O(sqrt n) for is_prime_trial",
            "trap": "Python ints never overflow, but pow(a, e, m) is far faster than a ** e % m. mod_inv needs gcd(a, m) = 1. // floors toward minus infinity, so -7 // 2 is -4.",
            "level": "beginner",
            "solution": false,
            "line": 237
          },
          {
            "id": "s8",
            "title": "String helpers",
            "names": [
              "is_palindrome",
              "freq26",
              "run_lengths"
            ],
            "cost": "O(length)",
            "trap": "s.split() splits on any run of spaces, s.split(\" \") does not. Strings are immutable: build a list of characters and join once instead of s += c in a loop.",
            "level": "beginner",
            "solution": false,
            "line": 275
          },
          {
            "id": "s9",
            "title": "2D grids and direction arrays",
            "names": [
              "read_grid",
              "DX4",
              "DY4",
              "DX8",
              "DY8",
              "inside",
              "find_char"
            ],
            "cost": "O(n * m)",
            "trap": "Rows are r, columns are c. Check inside(r, c, n, m) before you index: a negative index silently wraps around in Python.",
            "level": "beginner",
            "solution": false,
            "line": 303
          },
          {
            "id": "s10",
            "title": "Disjoint set union (DSU)",
            "names": [
              "DSU",
              "find",
              "unite",
              "same",
              "size",
              "cnt"
            ],
            "cost": "O(alpha(n)) amortized per find or unite",
            "trap": "Union by size must compare the sizes of the roots, not of the raw nodes. Use size(x) instead of reading s[x] directly.",
            "level": "medium",
            "solution": false,
            "line": 329
          },
          {
            "id": "s11",
            "title": "Graph search: BFS, iterative DFS, components",
            "names": [
              "read_graph",
              "bfs_dist",
              "dfs_tree",
              "components"
            ],
            "cost": "O(n + m)",
            "trap": "Recursion is not used: Python stops at about 1000 frames. dfs_tree is for trees: on a general graph it would walk back along cycles.",
            "level": "medium",
            "solution": false,
            "line": 368
          },
          {
            "id": "s12",
            "title": "Grid BFS",
            "names": [
              "grid_bfs"
            ],
            "cost": "O(n * m)",
            "trap": "Mark a cell visited when you push it, not when you pop it. -1 means unreachable.",
            "level": "medium",
            "solution": false,
            "line": 435
          },
          {
            "id": "s13",
            "title": "Dijkstra and 0-1 BFS",
            "names": [
              "add_edge",
              "dijkstra",
              "bfs01",
              "path_to"
            ],
            "cost": "O((n + m) log m) for Dijkstra, O(n + m) for 0-1 BFS",
            "trap": "Weights must be non-negative. Skip stale heap entries. Unreachable stays INF: test for it before you add to a distance.",
            "level": "medium",
            "solution": false,
            "line": 456
          },
          {
            "id": "s14",
            "title": "Topological sort",
            "names": [
              "topo_sort"
            ],
            "cost": "O(n + m)",
            "trap": "Returns None when the graph has a cycle. Check the result before you use it.",
            "level": "medium",
            "solution": false,
            "line": 519
          },
          {
            "id": "s15",
            "title": "Sieve, primes and factorisation",
            "names": [
              "sieve",
              "primes_upto",
              "build_spf",
              "factorize",
              "factorize_trial",
              "divisors"
            ],
            "cost": "O(n log log n) sieve, O(log x) per factorize, O(sqrt x) for trial division",
            "trap": "Size the sieve to the largest query, inclusive. factorize(x, spf) with x above n is out of range. 0 and 1 are not prime.",
            "level": "medium",
            "solution": false,
            "line": 539
          },
          {
            "id": "s16",
            "title": "nCr with factorials",
            "names": [
              "Fact",
              "C",
              "P",
              "inv_fact"
            ],
            "cost": "O(n) build, O(1) per query",
            "trap": "Build the tables up to the largest n you will ask about. The modulus must be a prime bigger than n. C(n, r) is 0 when r > n or r < 0.",
            "level": "medium",
            "solution": false,
            "line": 612
          },
          {
            "id": "s17",
            "title": "Fenwick tree (BIT)",
            "names": [
              "Fenwick",
              "add",
              "prefix",
              "range_sum",
              "kth",
              "FenwickRangeAdd"
            ],
            "cost": "O(log n) per operation",
            "trap": "Internally 1-indexed, so index 0 would loop forever (add and prefix hide that). kth needs every stored value to be non-negative.",
            "level": "medium",
            "solution": false,
            "line": 640
          },
          {
            "id": "s18",
            "title": "Sparse table",
            "names": [
              "SparseTable",
              "query"
            ],
            "cost": "O(n log n) build, O(1) per query",
            "trap": "Only for idempotent operations (min, max, gcd): the two blocks overlap, so a sum would count twice. Query needs l < r.",
            "level": "medium",
            "solution": false,
            "line": 698
          },
          {
            "id": "s19",
            "title": "Two pointers helpers",
            "names": [
              "longest_window",
              "shortest_window_ge",
              "count_pairs_sum_leq"
            ],
            "cost": "O(n) after sorting where needed",
            "trap": "Sliding windows need a monotone condition: if a window is good, every smaller window inside it is good. shortest_window_ge needs non-negative numbers and s >= 1.",
            "level": "medium",
            "solution": false,
            "line": 720
          },
          {
            "id": "s20",
            "title": "Coordinate compression",
            "names": [
              "Compress",
              "id",
              "value",
              "size"
            ],
            "cost": "O(n log n) build, O(log n) per id",
            "trap": "id(x) is only meaningful if x was in the input. Equal values get the same id, ids start at 0.",
            "level": "medium",
            "solution": false,
            "line": 758
          },
          {
            "id": "s21",
            "title": "Bit tricks",
            "names": [
              "popcnt",
              "lowbit",
              "msb",
              "is_pow2",
              "next_pow2",
              "bit"
            ],
            "cost": "O(1)",
            "trap": "Python ints have no fixed width, so ~x is negative: use x ^ mask to flip a fixed number of bits. msb(0) is -1.",
            "level": "medium",
            "solution": false,
            "line": 778
          },
          {
            "id": "s22",
            "title": "Modular tools for any modulus: extended gcd",
            "names": [
              "mulmod64",
              "powmod64",
              "ext_gcd",
              "inv_mod_any"
            ],
            "cost": "O(log) each",
            "trap": "inv_mod_any gives -1 when gcd(a, m) is not 1. mulmod64 and powmod64 are here so the Pollard rho section reads the same as in C++.",
            "level": "medium",
            "solution": false,
            "line": 810
          },
          {
            "id": "s23",
            "title": "String hashing",
            "names": [
              "Hash61",
              "get"
            ],
            "cost": "O(n) build, O(1) per get",
            "trap": "A fixed base can be broken on purpose, so the base is random. Equal hashes only suggest equal strings. get(l, r) is half open.",
            "level": "medium",
            "solution": false,
            "line": 839
          },
          {
            "id": "s24",
            "title": "KMP, Z function and pattern search",
            "names": [
              "prefix_function",
              "z_function",
              "find_all"
            ],
            "cost": "O(n) each",
            "trap": "z[0] is n here, not 0. To search with Z, join pattern + separator + text and use a separator that is in neither string.",
            "level": "medium",
            "solution": false,
            "line": 868
          },
          {
            "id": "s25",
            "title": "Modular integer class (modint)",
            "names": [
              "ModInt",
              "pow",
              "inv"
            ],
            "cost": "O(1) per + - *, O(log MOD) per division",
            "trap": "Division needs a prime modulus. This class is convenient but about ten times slower than plain ints with % MOD: use it for clarity, not in a hot loop.",
            "level": "hard",
            "solution": false,
            "line": 919
          },
          {
            "id": "s26",
            "title": "Segment tree (point update, range query)",
            "names": [
              "SegTree",
              "set",
              "get",
              "query"
            ],
            "cost": "O(n) build, O(log n) per set or query",
            "trap": "The operation must be associative and e must be its identity (0 for sum, -INF for max, INF for min). Order is kept, so it also works for non-commutative operations. query is half open.",
            "level": "hard",
            "solution": false,
            "line": 974
          },
          {
            "id": "s27",
            "title": "Segment tree with lazy propagation (range add, range sum and min)",
            "names": [
              "LazySeg",
              "add",
              "range_sum",
              "range_min"
            ],
            "cost": "O(n) build, O(log n) per add, range_sum or range_min",
            "trap": "Push the tag down before you visit the children, and scale the tag by the segment length when you add to a sum. All ranges are half open. The recursion is only log n deep.",
            "level": "hard",
            "solution": false,
            "line": 1023
          },
          {
            "id": "s28",
            "title": "LCA by binary lifting",
            "names": [
              "LCA",
              "lca",
              "dist",
              "kth_ancestor",
              "depth"
            ],
            "cost": "O(n log n) build, O(log n) per query",
            "trap": "The tree must be connected and rooted at root. The build is iterative. kth_ancestor returns -1 above the root.",
            "level": "hard",
            "solution": false,
            "line": 1104
          },
          {
            "id": "s29",
            "title": "Strongly connected components and 2-SAT",
            "names": [
              "scc_tarjan",
              "TwoSat",
              "add_clause",
              "solve"
            ],
            "cost": "O(n + m)",
            "trap": "Component ids come out in reverse topological order (0 is a sink). In 2-SAT add both implications for a clause: node 2i is \"x_i true\", 2i+1 is \"x_i false\".",
            "level": "hard",
            "solution": false,
            "line": 1154
          },
          {
            "id": "s30",
            "title": "Bridges and articulation points",
            "names": [
              "find_bridges"
            ],
            "cost": "O(n + m)",
            "trap": "Skip the parent edge by its id, not by its endpoint, or parallel edges are missed. A root is a cut vertex only with two or more DFS children.",
            "level": "hard",
            "solution": false,
            "line": 1217
          },
          {
            "id": "s31",
            "title": "2D Fenwick tree",
            "names": [
              "Fenwick2D",
              "add",
              "prefix",
              "rect"
            ],
            "cost": "O(log n * log m) per operation",
            "trap": "add and prefix are in 0-based cells. prefix(r, c) covers rows [0, r) and columns [0, c). Memory is n * m numbers.",
            "level": "hard",
            "solution": false,
            "line": 1268
          },
          {
            "id": "s32",
            "title": "Bitmask DP helpers",
            "names": [
              "for_submasks",
              "sos_subsets",
              "sos_supersets",
              "tsp_cycle"
            ],
            "cost": "O(3^n) over all submasks, O(n * 2^n) for the sums, O(n^2 * 2^n) for tsp_cycle",
            "trap": "Memory is the limit near n = 20 and Python lists are large: 2^20 * 20 numbers will not fit. The submask loop must handle mask 0, and must stop after it.",
            "level": "hard",
            "solution": false,
            "line": 1302
          },
          {
            "id": "s33",
            "title": "Matrix exponentiation",
            "names": [
              "mat_mul",
              "mat_identity",
              "mat_pow",
              "fib"
            ],
            "cost": "O(k^3) per multiply, O(k^3 log e) per power",
            "trap": "Entries are reduced modulo m. With m = 1 the identity is all zeros. Pure Python handles k up to about 50 quickly.",
            "level": "hard",
            "solution": false,
            "line": 1358
          },
          {
            "id": "s34",
            "title": "NTT: polynomial multiplication mod 998244353",
            "names": [
              "ntt",
              "convolve"
            ],
            "cost": "O(n log n)",
            "trap": "Works for modulus 998244353 only. The result length is len(a) + len(b) - 1. Pure Python does lists of about 1e5 in a few seconds.",
            "level": "hard",
            "solution": false,
            "line": 1389
          },
          {
            "id": "s35",
            "title": "Mo's algorithm",
            "names": [
              "mo_run",
              "add",
              "rem",
              "get"
            ],
            "cost": "O((n + q) sqrt n) add and rem calls",
            "trap": "Queries are half open (l, r). add(i) and rem(i) get an index into the array. Answers come back in the input order, not the processed order.",
            "level": "hard",
            "solution": false,
            "line": 1442
          },
          {
            "id": "s36",
            "title": "Geometry: points, convex hull, segments",
            "names": [
              "cross",
              "dot",
              "orient",
              "convex_hull",
              "polygon_area2",
              "segments_intersect",
              "on_segment"
            ],
            "cost": "O(n log n) for the hull",
            "trap": "Use integer coordinates so every test is exact. Never compare floats with ==. cross(o, a, b) is positive for a left turn at o. The hull drops collinear points and duplicates.",
            "level": "hard",
            "solution": false,
            "line": 1474
          },
          {
            "id": "s37",
            "title": "Dinic max flow",
            "names": [
              "Dinic",
              "add_edge",
              "max_flow",
              "min_cut_side",
              "flow_on"
            ],
            "cost": "O(V^2 E) worst case, far faster on typical graphs",
            "trap": "Add each edge once with its capacity, the reverse edge is created for you. flow_on(id) is the flow on an edge. min_cut_side only after max_flow.",
            "level": "hard",
            "solution": false,
            "line": 1522
          },
          {
            "id": "s38",
            "title": "Floyd-Warshall (all pairs shortest paths)",
            "names": [
              "floyd_warshall",
              "has_negative_cycle"
            ],
            "cost": "O(n^3)",
            "trap": "Set d[i][i] = 0 and INF where there is no edge. A negative d[i][i] afterwards means a negative cycle. Only for n up to about 150 in Python.",
            "level": "hard",
            "solution": false,
            "line": 1601
          },
          {
            "id": "s39",
            "title": "Minimum spanning tree (Kruskal)",
            "names": [
              "kruskal"
            ],
            "cost": "O(m log m)",
            "trap": "Returns (-1, []) when the graph is not connected. Sort by weight only. Needs the DSU section.",
            "level": "hard",
            "solution": false,
            "line": 1627
          },
          {
            "id": "s40",
            "title": "Heavy-light decomposition",
            "names": [
              "HLD",
              "lca",
              "path_ranges",
              "path_query",
              "subtree_range"
            ],
            "cost": "O(n) build, O(log^2 n) per path query",
            "trap": "Position ranges are half open [l, r) here (the C++ version is inclusive). Values sit on vertices. path_query combines ranges in no fixed order, so use an operation that does not care about direction.",
            "level": "hard",
            "solution": false,
            "line": 1643
          },
          {
            "id": "s41",
            "title": "Suffix array and LCP",
            "names": [
              "suffix_array",
              "kasai",
              "count_distinct"
            ],
            "cost": "O(n log^2 n) build, O(n) for the LCP array",
            "trap": "lcp[i] is the common prefix of the suffixes sa[i] and sa[i + 1] in this version, so it has n - 1 entries. Pure Python handles n of about 1e5 in a few seconds.",
            "level": "hard",
            "solution": false,
            "line": 1711
          },
          {
            "id": "s42",
            "title": "Miller-Rabin primality and Pollard rho factoring",
            "names": [
              "is_prime_u64",
              "pollard_rho",
              "factorize_u64"
            ],
            "cost": "O(log n) per primality test, about O(n^(1/4)) per factor found",
            "trap": "The 12 fixed bases are exact for every n below 2^64. Pollard rho needs a composite input: factorize_u64 checks that for you. Needs the 64-bit modular section.",
            "level": "hard",
            "solution": false,
            "line": 1763
          },
          {
            "id": "s43",
            "title": "Your solution: solve() and main()",
            "names": [
              "solve",
              "main"
            ],
            "cost": "your code",
            "trap": "With many test cases, create fresh lists inside solve() and read all the input of a test even when you can answer early.",
            "level": "beginner",
            "solution": true,
            "line": 1831
          }
        ]
      }
    }
  ]
}
