题目 › usaco-1475

Photo Op

USACO 2025 January · 金组 · 2025-01 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

Farmer John's farm is full of lush vegetation and every cow wants a photo of its natural beauty. Unfortunately, Bessie still has places to be, but she doesn't want to disrupt any photo ops.

Bessie is currently standing at $(X,0)$ on the XY-plane and she wants to get to $(0,Y)$ ($1\le X,Y\le 10^6$). Unfortunately, $N$ ($1 \leq N \leq 3 \cdot 10^5$) other cows have decided to pose on the $X$ axis. More specifically, cow $i$ will be positioned at $(x_i,0)$ with a photographer at $(0,y_i)$ where $(1 \leq x_i,y_i \leq 10^6)$ ready to take their picture. They will begin posing moments before time $s_i$ ($1 \leq s_i < T$) and they will keep posing for a very long time (they have to get their picture just right). Here, $1\le T\le N+1$.

Bessie knows the schedule for every cow's photo op, and she will take the shortest Euclidean distance to get to her destination, without crossing the line of sight from any photographer to their respective cow (her path will consist of one or more line segments).

If Bessie leaves at time $t$, she will avoid the line of sights for all photographer/cow pairs that started posing at time $s_i \le t$, and let the distance to her final destination be $d_t$. Determine the values of $\lfloor d_t\rfloor$ for each integer $t$ from $0$ to $T-1$ inclusive.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line of input contains $N$ and $T$, representing the number of cows posing on the $x$-axis and the timeframe that Bessie could leave at.

The second line of input contains $X$ and $Y$, representing Bessie's starting $X$ coordinate and her target $Y$ coordinate respectively.

The next $N$ lines contain $s_i$ $x_i$ and $y_i$. It is guaranteed that all $x_i$ are distinct from each other and $X$, and all $y_i$ are distinct from each other and $Y$. All $s_i$ will be given in increasing order, where $s_i \leq s_{i+1}$.

OUTPUT FORMAT (print output to the terminal / stdout):

Print $T$ lines, with the $t$th (0-indexed) line containing $\lfloor d_t\rfloor$.

SAMPLE INPUT:

4 5
6 7
1 7 5
2 4 4
3 1 6
4 2 9

SAMPLE OUTPUT:

9
9
9
10
12

SAMPLE INPUT:

2 3
10 7
1 2 10
1 9 1

SAMPLE OUTPUT:

12
16
16

For $t=0$ the answer is $\lfloor \sqrt{149} \rfloor=12$.

For $t=1$ the answer is $\lfloor 14+\sqrt 5\rfloor=16$.

SAMPLE INPUT:

5 6
8 9
1 3 5
1 4 1
3 10 7
4 9 2
5 6 6

SAMPLE OUTPUT:

12
12
12
12
14
14

For $t=5$ the answer is $\lfloor 1+\sqrt{9^2+7^2}+2\rfloor=14$. Path: $(8,0)\to (9,0)\to (0,7)\to (0,9)$

SCORING:

  • Inputs 4-6: $N\le 100$
  • Inputs 7-9: $N\le 3000$
  • Inputs 10-12: $T\le 10$
  • Inputs 13-18: No additional constraints

Problem credits: Suhas Nagar

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 58.0k平均输出 token(含思考) ↓
  • 24.5输出速度 tokens/s ↑
  • 2218 秒延迟 ↓
  • 1.09×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 92.9k平均输出 token(含思考) ↓
  • 200.3输出速度 tokens/s ↑
  • 414 秒延迟 ↓
  • 1.04×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

1/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 0.0采样 2 · 编译错误
  • 33.3采样 3 · 答案错误
  • 88.6k平均输出 token(含思考) ↓
  • 146.9输出速度 tokens/s ↑
  • 577 秒延迟 ↓
  • 1.69×CPU 时间 / 官方解 ↓
  • 同阶 1时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 395 行
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i128 = __int128_t;

const int INF = 1000000000;

struct Cost {
    ll K, S; // K + sqrt(S)
};

bool costLess(const Cost& A, const Cost& B) {
    ll c = B.K - A.K;
    if (c == 0) return A.S < B.S;

    if (c > 0) {
        if (A.S <= B.S) return true;
        i128 C = c;
        i128 D = (i128)A.S - B.S - C * C;
        if (D < 0) return true;
        return D * D < (i128)4 * C * C * B.S;
    } else {
        i128 D0 = -(i128)c;
        i128 D = (i128)B.S - A.S - D0 * D0;
        if (D <= 0) return false;
        return (i128)4 * D0 * D0 * A.S < D * D;
    }
}

struct Entry {
    Cost c;
    int j, b;
};

struct EntryCmp {
    bool operator()(const Entry& a, const Entry& b) const {
        if (costLess(b.c, a.c)) return true;
        if (costLess(a.c, b.c)) return false;
        if (a.j != b.j) return a.j > b.j;
        return a.b > b.b;
    }
};

struct ActiveSegTree {
    int n;
    vector<int> mn, mx;

    ActiveSegTree(int n_) : n(n_), mn(4 * n_ + 5, INF), mx(4 * n_ + 5, 0) {}

    void update(int node, int l, int r, int p, int v) {
        if (l == r) {
            mn[node] = mx[node] = v;
            return;
        }
        int m = (l + r) >> 1;
        if (p <= m) update(node << 1, l, m, p, v);
        else update(node << 1 | 1, m + 1, r, p, v);
        mn[node] = min(mn[node << 1], mn[node << 1 | 1]);
        mx[node] = max(mx[node << 1], mx[node << 1 | 1]);
    }

    void update(int p, int v) {
        update(1, 1, n, p, v);
    }

    int firstGreater(int node, int l, int r, int ql, int qr, int v) {
        if (r < ql || l > qr || mx[node] <= v) return n + 1;
        if (l == r) return l;
        int m = (l + r) >> 1;
        int res = firstGreater(node << 1, l, m, ql, qr, v);
        if (res != n + 1) return res;
        return firstGreater(node << 1 | 1, m + 1, r, ql, qr, v);
    }

    int firstGreater(int ql, int qr, int v) {
        if (ql > qr) return n + 1;
        return firstGreater(1, 1, n, ql, qr, v);
    }

    int lastLess(int node, int l, int r, int ql, int qr, int v) {
        if (r < ql || l > qr || mn[node] >= v) return 0;
        if (l == r) return l;
        int m = (l + r) >> 1;
        int res = lastLess(node << 1 | 1, m + 1, r, ql, qr, v);
        if (res != 0) return res;
        return lastLess(node << 1, l, m, ql, qr, v);
    }

    int lastLess(int ql, int qr, int v) {
        if (ql > qr) return 0;
        return lastLess(1, 1, n, ql, qr, v);
    }
};

struct RunMap {
    struct Info {
        int s, e, v;
    };

    int n;
    map<int, int> mp;

    RunMap(int n_, int initial) : n(n_) {
        mp[0] = initial;
    }

    map<int, int>::iterator split(int p) {
        if (p == 0) return mp.begin();
        auto it = mp.lower_bound(p);
        if (it != mp.end() && it->first == p) return it;
        auto prv = prev(it);
        return mp.emplace(p, prv->second).first;
    }

    void assignRange(int l, int r, int v) {
        split(l);
        if (r < n) split(r + 1);
        auto it = mp.lower_bound(l);
        auto jt = (r < n ? mp.lower_bound(r + 1) : mp.end());
        mp.erase(it, jt);
        mp[l] = v;
    }

    int get(int p) const {
        return prev(mp.upper_bound(p))->second;
    }

    Info info(int p) const {
        auto it = prev(mp.upper_bound(p));
        auto nx = next(it);
        return {it->first, nx == mp.end() ? n : nx->first - 1, it->second};
    }
};

struct ValidSet {
    int n;
    vector<int> nx, pr;
    vector<char> alive;

    ValidSet(int n_) : n(n_), nx(n_ + 2), pr(n_ + 1), alive(n_ + 1, 1) {
        iota(nx.begin(), nx.end(), 0);
        iota(pr.begin(), pr.end(), 0);
    }

    int findNext(int x) {
        if (x > n) return n + 1;
        return nx[x] == x ? x : nx[x] = findNext(nx[x]);
    }

    int findPrev(int x) {
        if (x < 0) return -1;
        return pr[x] == x ? x : pr[x] = findPrev(pr[x]);
    }

    int successor(int x) {
        return findNext(x);
    }

    int predecessor(int x) {
        return findPrev(x);
    }

    void eraseRange(int l, int r) {
        l = max(l, 0);
        r = min(r, n);
        if (l > r) return;
        int j = findNext(l);
        while (j <= r) {
            alive[j] = 0;
            int nj = findNext(j + 1);
            nx[j] = nj;
            pr[j] = findPrev(j - 1);
            j = nj;
        }
    }
};

struct Cow {
    int s, x, y, idx;
};

struct Solver {
    int n, X, Y, kx;
    vector<ll> a, axisCost;

    ActiveSegTree active;
    RunMap L, R;
    ValidSet valid;

    priority_queue<Entry, vector<Entry>, EntryCmp> lowPQ, highPQ;

    int firstHigh, lastLow;

    Solver(int n_, int X_, int Y_, const vector<int>& xs)
        : n(n_), X(X_), Y(Y_), a(n_ + 1), axisCost(n_ + 1),
          active(n_), L(n_, 0), R(n_, INF), valid(n_),
          firstHigh(n_ + 1), lastLow(0) {

        kx = 0;
        for (int i = 1; i <= n; i++) {
            if (xs[i] < X) kx++;
        }

        for (int j = 0; j <= n; j++) {
            if (j < kx) a[j] = xs[j + 1];
            else if (j == kx) a[j] = X;
            else a[j] = xs[j];
            axisCost[j] = llabs((ll)X - a[j]);
        }
    }

    Cost makeCost(int j, int b) const {
        return {
            axisCost[j] + llabs((ll)Y - b),
            a[j] * a[j] + (ll)b * b
        };
    }

    void addLowCandidate(int g) {
        if (g < 0 || g > n) return;
        auto run = R.info(g);
        if (run.v >= Y) return;

        int st = max(run.s, kx + 1);
        if (st > run.e || !valid.alive[st]) return;

        lowPQ.push({makeCost(st, run.v), st, run.v});
    }

    void addHighCandidate(int g) {
        if (g < 0 || g > n) return;
        auto run = L.info(g);
        if (run.v <= Y) return;

        int en = min(run.e, kx - 1);
        if (en < run.s || en < 0 || !valid.alive[en]) return;

        highPQ.push({makeCost(en, run.v), en, run.v});
    }

    void activate(int k, int y) {
        if (k > 1) {
            int i = active.firstGreater(1, k - 1, y);
            if (i <= k - 1) valid.eraseRange(i, k - 1);
        }
        if (k < n) {
            int i = active.lastLess(k + 1, n, y);
            if (i >= k + 1) valid.eraseRange(k, i - 1);
        }

        if (y > Y) firstHigh = min(firstHigh, k);
        else lastLow = max(lastLow, k);

        bool changedL = false, changedR = false;
        int leftR = 0, rightR = 0;

        auto it = prev(L.mp.upper_bound(k));
        if (it->second < y) {
            auto jt = it;
            while (jt != L.mp.end() && jt->second < y) ++jt;
            int r = (jt == L.mp.end() ? n : jt->first - 1);
            L.assignRange(k, r, y);
            changedL = true;
        }

        int r = k - 1;
        if (r >= 0) {
            it = prev(R.mp.upper_bound(r));
            if (it->second > y) {
                auto first = it;
                while (first != R.mp.begin()) {
                    auto prv = prev(first);
                    if (prv->second <= y) break;
                    first = prv;
                }
                leftR = first->first;
                rightR = r;
                R.assignRange(leftR, rightR, y);
                changedR = true;
            }
        }

        active.update(k, y);

        if (changedL) {
            addHighCandidate(k - 1);
            addHighCandidate(k);
        }
        if (changedR) {
            addLowCandidate(leftR);
            addLowCandidate(rightR + 1);
        }
    }

    static ll floorSqrt(ll s) {
        ll r = sqrtl((long double)s);
        while ((i128)(r + 1) * (r + 1) <= s) r++;
        while ((i128)r * r > s) r--;
        return r;
    }

    ll answer() {
        Cost best{(ll)X + Y, 0};
        bool have = true;

        auto take = [&](const Cost& c) {
            if (!have || costLess(c, best)) {
                best = c;
                have = true;
            }
        };

        if (lastLow < firstHigh) {
            int l = lastLow;
            int r = firstHigh - 1;
            int j = min(max(kx, l), r);
            take(makeCost(j, Y));
        }

        if (lastLow > 0) {
            int lim = min(kx, lastLow - 1);
            if (lim >= 0) {
                int j = valid.predecessor(lim);
                if (j >= 0) take(makeCost(j, R.get(j)));
            }
        }

        if (firstHigh <= n) {
            int lim = max(kx, firstHigh);
            int j = valid.successor(lim);
            if (j <= n) take(makeCost(j, L.get(j)));
        }

        while (!lowPQ.empty()) {
            const Entry& e = lowPQ.top();
            bool ok = e.j > kx && valid.alive[e.j] && R.get(e.j) == e.b;
            if (!ok) lowPQ.pop();
            else break;
        }
        if (!lowPQ.empty()) take(lowPQ.top().c);

        while (!highPQ.empty()) {
            const Entry& e = highPQ.top();
            bool ok = e.j < kx && valid.alive[e.j] && L.get(e.j) == e.b;
            if (!ok) highPQ.pop();
            else break;
        }
        if (!highPQ.empty()) take(highPQ.top().c);

        return best.K + floorSqrt(best.S);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, T;
    cin >> N >> T;

    int X, Y;
    cin >> X >> Y;

    vector<Cow> cows(N);
    for (int i = 0; i < N; i++) {
        cin >> cows[i].s >> cows[i].x >> cows[i].y;
    }

    vector<int> order(N);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) {
        return cows[i].x < cows[j].x;
    });

    vector<int> xs(N + 1);
    for (int i = 0; i < N; i++) {
        int id = order[i];
        xs[i + 1] = cows[id].x;
        cows[id].idx = i + 1;
    }

    Solver solver(N, X, Y, xs);

    int ptr = 0;
    for (int t = 0; t < T; t++) {
        while (ptr < N && cows[ptr].s <= t) {
            solver.activate(cows[ptr].idx, cows[ptr].y);
            ptr++;
        }
        cout << solver.answer() << '\n';
    }

    return 0;
}