---
id: 1662
lang: en
translation_of: index.md
source_sha256: 0f935286bccd4996b19ba84602ffda281898357808045561829690124ce4dbe8
title: "Educational Codeforces Round 72 Div. 2 Problem-Solving Report (CF1217)"
description: "Solutions to CF1217 C (scan using the nearest previous 1), D (one color without cycles, two with) and E (segment tree with per-digit min and second min)."
tags: ["OI","Greedy","Segment Tree","Codeforces"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "扫描到 [l, r] 大于当前区间 in C is terse; rendered literally as “scan to an [l, r] that is greater than the current interval”."
  - "The O(n log n) bound in C is kept as written, though the code scans at most 20 positions per right end."
  - "tarjan kept lowercase as the author wrote it."
---

## C - The Number Of Good Substrings

This problem is pretty <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">stupid</span>.

Consider recording the position of the nearest previous $1$ as $nxt_i$. Then, clearly: if in the $O(n \log n)$-time scan we scan to an $[l, r]$ that is greater than the current interval, we consider whether there are enough zeros to the left of $l$ to make up the length. And then you can happily get AC on this problem.

<!--more-->

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

using namespace std;

const int MAX_N = 2e5 + 200;

int n, T, nxt[MAX_N];
char opt[MAX_N];

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        int ans = 0;
        scanf("%s", opt);
        n = strlen(opt);
        for (int i = 0; i < n; i++)
            if (opt[i] == '1')
                nxt[i] = i;
            else
                nxt[i] = (i == 0 ? -1 : nxt[i - 1]);
        for (int r = 0; r < n; r++)
        {
            int number = 0;
            for (int l = r; l >= 0 && r - l < 20; l--)
            {
                if (opt[l] == '0')
                    continue;
                number |= 1 << (r - l);
                if (number <= r - (l == 0 ? -1 : nxt[l - 1]))
                    ans++;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
```

## D - Coloring Edges

There is an obvious conclusion: if there is no cycle, just use a single color; if a cycle exists, two colors will do. At the start, color all the cycles black, then consider doing the white coloring when a cycle ends; a tarjan-like method does the job.

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

using namespace std;

const int MAX_N = 5050, MAX_E = 5050;

int head[MAX_N], current, n, m, stat[MAX_N], res[MAX_N];
bool cycle;

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

void dfs(int u)
{
    stat[u] = 1;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (stat[edges[i].to] == 0)
            dfs(edges[i].to), res[edges[i].id] = 1;
        else if (stat[edges[i].to] == 2)
            res[edges[i].id] = 1;
        else
            res[edges[i].id] = 2, cycle = true;
    stat[u] = 2;
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1, src, dst; i <= m; i++)
    {
        scanf("%d%d", &src, &dst);
        edges[current].to = dst, edges[current].nxt = head[src];
        edges[current].id = i, head[src] = current++;
    }
    for (int i = 1; i <= n; i++)
        if (stat[i] == 0)
            dfs(i);
    printf("%d\n", cycle ? 2 : 1);
    for (int i = 1; i <= m; i++)
        printf("%d ", res[i]);
    return 0;
}
```

## E - Sum Queries?

You can probably guess that this problem is maintained with a segment tree.

We actually only need to maintain the “bad intervals”: intervals where the same digit position conflicts, and record the smallest and second-smallest value for each digit position (because later merges only use the smallest and second-smallest information, so this is optimal). Just merge directly in the segment tree; it is fairly obvious.

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

using namespace std;

const ll MAX_N = 2e5 + 200, INF = 0x3f3f3f3f3f3f3f3f;

struct node
{
    ll digits[11][2];
} nodes[MAX_N << 2];

ll buf[11], n, m, ai[MAX_N];

void calc(ll x)
{
    for (int i = 0; i <= 10; i++)
        buf[i] = x % 10, x /= 10;
}

node merge(node ls, node rs)
{
    node res;
    for (int i = 0; i <= 10; i++)
        res.digits[i][0] = res.digits[i][1] = INF;
    for (int i = 0; i <= 10; i++)
    {
        res.digits[i][0] = min(ls.digits[i][0], rs.digits[i][0]);
        res.digits[i][1] = max(ls.digits[i][0], rs.digits[i][0]);
        res.digits[i][1] = min(res.digits[i][1], ls.digits[i][1]);
        res.digits[i][1] = min(res.digits[i][1], rs.digits[i][1]);
    }
    return res;
}

void build(int l, int r, int p)
{
    if (l == r)
    {
        calc(ai[l]);
        for (int i = 0; i <= 10; i++)
        {
            if (buf[i])
                nodes[p].digits[i][0] = ai[l];
            else
                nodes[p].digits[i][0] = INF;
            nodes[p].digits[i][1] = INF;
        }
        return;
    }
    build(l, mid, lson), build(mid + 1, r, rson);
    nodes[p] = merge(nodes[lson], nodes[rson]);
}

void update(ll pos, int l, int r, int p, ll val)
{
    if (l == r)
    {
        calc(val);
        for (int i = 0; i <= 10; i++)
        {
            if (buf[i])
                nodes[p].digits[i][0] = val;
            else
                nodes[p].digits[i][0] = INF;
            nodes[p].digits[i][1] = INF;
        }
        return;
    }
    if (pos <= mid)
        update(pos, l, mid, lson, val);
    else
        update(pos, mid + 1, r, rson, val);
    nodes[p] = merge(nodes[lson], nodes[rson]);
}

node query(ll ql, ll qr, ll l, ll r, ll p)
{
    if (ql <= l && r <= qr)
        return nodes[p];
    node res;
    for (int i = 0; i <= 10; i++)
        res.digits[i][0] = res.digits[i][1] = INF;
    if (ql <= mid)
        res = merge(res, query(ql, qr, l, mid, lson));
    if (mid < qr)
        res = merge(res, query(ql, qr, mid + 1, r, rson));
    return res;
}

int main()
{
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &ai[i]);
    build(1, n, 1);
    while (m--)
    {
        ll opt, l, r;
        scanf("%lld%lld%lld", &opt, &l, &r);
        if (opt == 1)
            update(l, 1, n, 1, r);
        else
        {
            node res = query(l, r, 1, n, 1);
            ll ans = INF;
            for (int i = 0; i <= 10; i++)
                ans = min(ans, res.digits[i][0] + res.digits[i][1]);
            if (ans >= INF)
                puts("-1");
            else
                printf("%lld\n", ans);
        }
    }
    return 0;
}
```
