---
id: 882
lang: en
translation_of: index.md
source_sha256: 2f54c4951a60c3b0a66e62092ddaa4019d4bcefe38031b01550c9d5b6a214b69
title: "[Fortuna OJ] Feb 26th – Group A Problem-Solving Report"
description: "Notes on three problems: expected operations to collect all items via bitmask DP, an easy SCC problem, and counting intervals with divide and conquer."
tags: ["OI","Graph Theory","Expected Value","Expected Value","Strongly Connected Components","Tarjan","SCC Condensation","Probability","Problem-Solving Report","Dumbass"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "画风十分一至: 一至 is a typo for 一致 (consistent) and was translated by its intended meaning."
  - "我* is the author's self-censored 我操; the censoring was mirrored as f*ck."
  - "第二题 refers to the second question asked by problem A (the code prints two answers), translated as the second question."
---

## A - Gift

Honestly, this is my weakest area, and it was laid completely bare during the contest.

We can transform this problem a bit. We can see that the happiness values are completely irrelevant, and the second question just asks

> Given some items $a_i$, each of which may appear on any operation with probability $p_i$. Find the expected number of operations needed for all items to appear.

Since there are very few items, we can use bitmask compression and compress them into $S$. Every state in which item $i$ has appeared can be transitioned to from the state where it has not appeared, i.e. `S^(1<<(i - 1))`. Multiplying that previous state by $p_i$ gives this part's contribution. Then, since $\sum_{i} {p_i} \neq 1$, there are cases where an operation does nothing, which is $(1-\sum_{i\in S} p_i)*E_S$. Then we add $1$ for the operation itself, and the full formula is:

$$
E_S = (\sum_{i\in S} p_i*E_{S'}) + (1-\sum_{i\in S} p_i)*E_S + 1
$$

Moving terms over and combining, we get:

$$
\begin{gathered} (\sum_{i\in S} p_i)*E_S = (\sum_{i\in S} p_i*E_{S'}) + 1 \\ E_S = \frac{(\sum_{i\in S} p_i*E_{S'}) + 1}{\sum_{i\in S} p_i} \end{gathered}
$$

### Code

```cpp
// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 21;
int n, wi[MAX_N];
long long ans1;
double pi[MAX_N], dp[(1 << 20)];
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lf%d", &pi[i], &wi[i]), ans1 += wi[i];
    for (int stat = 0; stat < (1 << n); stat++)
    {
        double psum = 0, ans = 0;
        for (int i = 1; i <= n; i++)
            if (stat & (1 << (i - 1)))
            {
                int prestat = stat ^ (1 << (i - 1));
                psum += pi[i];
                ans += pi[i] * dp[prestat];
            }
        if (psum != 0)
            dp[stat] = (ans + 1) / psum;
    }
    printf("%lld\n%.3lf", ans1, dp[(1 << n) - 1]);
    return 0;
}
```

## B - Communication

Ah, <span class="slang" data-zh="我*" data-gloss="wǒ*, a self-censored 我操 (wǒ cào), a vulgar exclamation roughly “f*ck”. An outburst of frustration.">f\*ck</span>.

This problem is <span class="slang" data-zh="很水" data-gloss="hěn shuǐ, “very watery”. Very easy, said of problems; compare 水题 (shuǐtí), an easy problem.">really easy</span>, so I won't cover it.

### Code

```cpp
/// B.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 100400, MAX_M = 1e5 + 2000;
int head[MAX_N], current, dfn[MAX_N], low[MAX_N], tot, st[MAX_N], hd, aff[MAX_N];
int totp, n, m, tmpx, tmpy, tmpz, ufs[MAX_N], dist[MAX_N];
bool inst[MAX_N];
struct edge
{
    int to, nxt, weight, from;
    bool operator<(const edge &e) const { return weight > e.weight; }
} edges[MAX_M << 1];
int find(int x) { return (ufs[x] == x) ? x : ufs[x] = find(ufs[x]); }
void unite(int x, int y)
{
    if (find(x) != find(y))
        ufs[find(x)] = find(y);
}
int addpath(int u, int v, int weight)
{
    edges[current].to = v, edges[current].nxt = head[u];
    edges[current].from = u;
    edges[current].weight = weight, head[u] = current++;
    return current - 1;
}
void tarjan(int u)
{
    inst[u] = true, st[++hd] = u;
    dfn[u] = ++tot, low[u] = dfn[u];
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (dfn[edges[i].to] == 0)
            tarjan(edges[i].to), low[u] = min(low[u], low[edges[i].to]);
        else if (inst[edges[i].to])
            low[u] = min(low[u], dfn[edges[i].to]);
    if (dfn[u] == low[u])
    {
        int j;
        totp++;
        do
        {
            j = st[hd], aff[j] = totp;
            inst[j] = false;
        } while (st[hd--] != u);
    }
}
int main()
{
    while (scanf("%d%d", &n, &m) && n != 0 && m != 0)
    {
        memset(dist, 0x3f, sizeof(dist));
        memset(st, 0, sizeof(st)), memset(dfn, 0, sizeof(dfn)), hd = 0;
        for (int i = 1; i <= 2 * n; i++)
            ufs[i] = i;
        memset(head, -1, sizeof(head)), memset(aff, 0, sizeof(aff));
        totp = n, current = 0, tot = 0;
        for (int i = 1; i <= m; i++)
            scanf("%d%d%d", &tmpx, &tmpy, &tmpz), addpath(tmpx + 1, tmpy + 1, tmpz);
        tarjan(1);
        for (int i = 1; i <= n; i++)
            for (int e = head[i]; e != -1; e = edges[e].nxt)
                if (aff[i] != aff[edges[e].to])
                    dist[aff[edges[e].to]] = min(dist[aff[edges[e].to]], edges[e].weight);
        long long ans = 0;
        for (int i = n + 1; i <= totp; i++)
            if (aff[1] != i)
                ans += dist[i];
        printf("%lld\n", ans);
    }
    return 0;
}
```

## C - Surprise Attack

This problem comes from Codeforces, and the <span class="slang" data-zh="画风" data-gloss="huàfēng, “art style”. Internet slang for the overall look or vibe of a scene or conversation.">vibe</span> is totally consistent.

First, let's transform the problem. The problem hints that each x-coordinate and each y-coordinate has only one point, and we want the number of intervals of length $len$ whose max and min y-coordinates also give $len$. We can have a go at it with divide and conquer. First fix an interval $[l,r]$ with midpoint $mid$. Let's explore the cases that span the two smaller intervals.

First, before the case analysis, we define a few arrays:

1. Let $minl[i]$ be the minimum of the interval $[i,mid]$, and $maxl[i]$ the maximum of the interval $[i,mid]$;
2. Let $minr[i]$ be the minimum of the interval $[mid+1,i]$, and $maxr[i]$ the maximum of the interval $[mid+1,i]$.

Then we can discuss the spanning cases:

1. Both extremes are in the same interval, left or right
2. The extremes are in two different intervals

The first case is fairly easy to handle. In the left-interval case: enumerate a left endpoint $i$, compute the right endpoint from the difference of the extremes, then check it and count it.

The second is a little more troublesome: across the intervals we have to consider the two ways the extremes can be distributed. Take the minimum on the left and the maximum on the right as an example, and suppose some point satisfies the condition:

$$
\begin{gathered} maxr[j]-minl[i] = j-i \\ \text{moving terms over gives} \\ maxr[j]-j=minl[i]-i \end{gathered}
$$

We can enumerate the split point and put the computed values into buckets (watch out for negatives; add an offset to the value range), then use two pointers to determine the valid range, with the left pointer decreasing by one and the right pointer increasing by one.

### Code

```cpp
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 50200, NUM_DOMAIN = 6e5;
pair<int, int> prs[MAX_N];
int arr[MAX_N], n, maxl[MAX_N], maxr[MAX_N], minl[MAX_N], minr[MAX_N], bucket[2 * NUM_DOMAIN + 1];
long long answer = 0;
void dq(int l, int r)
{
    if (l == r)
    {
        answer++;
        return;
    }
    int mid = (l + r) >> 1;
    // preprocessing;
    minl[mid] = arr[mid], maxl[mid] = arr[mid];
    maxr[mid + 1] = arr[mid + 1], minr[mid + 1] = arr[mid + 1];
    for (int i = mid - 1; i >= l; i--)
        minl[i] = min(minl[i + 1], arr[i]), maxl[i] = max(maxl[i + 1], arr[i]);
    for (int i = mid + 2; i <= r; i++)
        maxr[i] = max(maxr[i - 1], arr[i]), minr[i] = min(minr[i - 1], arr[i]);
    // make judges;
    // at the left:
    for (int i = mid; i >= l; i--)
    {
        int j = i + (maxl[i] - minl[i]);
        if (j > mid && j <= r && minr[j] > minl[i] && maxr[j] < maxl[i])
            answer++;
    }
    // at the right;
    for (int i = mid + 1; i <= r; i++)
    {
        int j = i - (maxr[i] - minr[i]);
        if (j <= mid && j >= l && minl[j] > minr[i] && maxl[j] < maxr[i])
            answer++;
    }
    // in the middle;
    // min|max:
    int ptr1 = mid + 1, ptr2 = mid;
    for (int i = mid; i >= l; i--)
    {
        while (minr[ptr2 + 1] > minl[i] && ptr2 < r)
            ptr2++, bucket[maxr[ptr2] - ptr2 + NUM_DOMAIN]++;
        while (maxl[i] > maxr[ptr1])
        {
            bucket[maxr[ptr1] - ptr1 + NUM_DOMAIN]--, ptr1++;
            if (ptr1 > r)
                break;
        }
        if (ptr1 > r)
            break;
        if (ptr1 <= ptr2)
            answer += bucket[minl[i] - i + NUM_DOMAIN];
    }
    for (int i = mid; i >= l; i--)
        bucket[minl[i] - i + NUM_DOMAIN] = 0;
    for (int i = mid + 1; i <= r; i++)
        bucket[maxr[i] - i + NUM_DOMAIN] = 0;
    // max|min:
    ptr1 = mid,
    ptr2 = mid + 1;
    for (int i = mid + 1; i <= r; i++)
    {
        while (minl[ptr2 - 1] > minr[i] && ptr2 > l)
            ptr2--, bucket[maxl[ptr2] + ptr2 + NUM_DOMAIN]++;
        while (maxr[i] > maxl[ptr1])
        {
            bucket[maxl[ptr1] + ptr1 + NUM_DOMAIN]--, ptr1--;
            if (ptr1 < l)
                break;
        }
        if (ptr1 < l)
            break;
        if (ptr2 <= ptr1)
            answer += bucket[minr[i] + i + NUM_DOMAIN];
    }
    for (int i = mid + 1; i <= r; i++)
        bucket[minr[i] + i + NUM_DOMAIN] = 0;
    for (int i = mid; i >= l; i--)
        bucket[maxl[i] + i + NUM_DOMAIN] = 0;
    dq(l, mid), dq(mid + 1, r);
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d%d", &prs[i].first, &prs[i].second);
    for (int i = 1; i <= n; i++)
        arr[prs[i].first] = prs[i].second;
    dq(1, n);
    printf("%lld", answer);
    return 0;
}
```
