---
id: 2168
lang: en
translation_of: index.md
source_sha256: 790af540d03fe46839312a67692dce78ca03ad4f5d88836e3be212167ebc6098
title: "AtCoder Regular Contest 101F: Robots and Exits — Solution"
description: "Each point can only move so far left and right; on a scatter plot the distinct operations form a non-decreasing polyline, counted with a Fenwick tree."
tags: ["OI","DP","Ad Hoc"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "帧 (literally “frame”) in “经过某个点的 x 帧或是 y 帧” is the author's unusual wording; kept literally as “the x frame or the y frame”."
---

## Main Idea

Great problem, great problem.

We notice that every point can only go so far to the left and to the right; computing this gives $(x_i, y_i)$, where $x$ stands for the left and $y$ for the right. Then, putting this thing on a scatter plot, we find that our essentially different operations are a monotonically non-decreasing polyline: because if we take $x$ as this path’s “longest leftward move distance” and $y$ as this path’s “longest rightward move distance”, then this polyline passing through the $x$ frame or the $y$ frame of any point will make this thing go <span class="slang" data-zh="GG" data-gloss="Gamer shorthand for “good game”, typed when conceding a match. Here: it is over, I am done for.">GG</span>, so by counting such polylines we can count the corresponding schemes.

We can just do the transitions directly with a Fenwick tree (BIT).

<!--more-->

## Code

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

using namespace std;

const int MAX_N = 2e5 + 200, mod = 1e9 + 7;

typedef long long ll;

int n, m, ai[MAX_N], bi[MAX_N], nodes[MAX_N << 2], pts[MAX_N], lft[MAX_N], rig[MAX_N], pcnt;
struct node
{
    int x, y;
    bool operator<(const node &rhs) const { return x < rhs.x || (x == rhs.x && y > rhs.y); }
    bool operator==(const node &rhs) const { return x == rhs.x && y == rhs.y; }
} nds[MAX_N];
vector<int> mp;

int ripe(int x) { return lower_bound(mp.begin(), mp.end(), x) - mp.begin() + 1; }

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

void update(int x, int d)
{
    for (; x <= mp.size(); x += lowbit(x))
        nodes[x] = (0LL + nodes[x] + d) % mod;
}

int query(int x, int ret = 0)
{
    for (; x; x -= lowbit(x))
        ret = (0LL + nodes[x] + ret) % mod;
    return ret;
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    for (int i = 1; i <= m; i++)
        scanf("%d", &bi[i]);
    for (int i = 1; i <= n; i++)
        if (ai[i] >= bi[1] && ai[i] <= bi[m])
        {
            pts[++pcnt] = ai[i];
            int pos = lower_bound(bi + 1, bi + 1 + m, ai[i]) - bi;
            nds[pcnt] = node{ai[i] - bi[pos - 1], bi[pos] - ai[i]};
            mp.push_back(bi[pos] - ai[i]);
        }
    sort(mp.begin(), mp.end()), mp.erase(unique(mp.begin(), mp.end()), mp.end());
    for (int i = 1; i <= pcnt; i++)
        nds[i].y = ripe(nds[i].y);
    sort(nds + 1, nds + 1 + pcnt), pcnt = unique(nds + 1, nds + 1 + pcnt) - nds - 1;
    int ans = 1;
    for (int i = 1; i <= pcnt; i++)
    {
        int sum = (query(nds[i].y - 1) + 1) % mod;
        ans = (0LL + sum + ans) % mod, update(nds[i].y, sum);
    }
    printf("%d\n", ans);
    return 0;
}
```
