結果

問題 No.3116 More and more teleporter
ユーザー Mistletoe
提出日時 2025-04-19 09:50:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 2,108 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 195,564 KB
実行使用メモリ 12,776 KB
最終ジャッジ日時 2025-04-19 09:50:46
合計ジャッジ時間 4,472 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (ll)4e18;

struct SegmentTree {
    int n;
    vector<ll> st;
    SegmentTree(int _n) {
        n = 1;
        while (n < _n) n <<= 1;
        st.assign(2*n, INF);
    }
    // point update: set st[p] = min(st[p], val)
    void update(int p, ll val) {
        p += n;
        st[p] = min(st[p], val);
        for (p >>= 1; p > 0; p >>= 1) {
            st[p] = min(st[p<<1], st[p<<1|1]);
        }
    }
    // range query min on [l, r) 0-indexed
    ll query(int l, int r) {
        ll res = INF;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) res = min(res, st[l++]);
            if (r & 1) res = min(res, st[--r]);
        }
        return res;
    }
};

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

    int N, Q;
    cin >> N >> Q;
    SegmentTree segA(N), segB(N);
    vector<ll> bestCost(N, INF);

    while (Q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int x;
            cin >> x;
            --x; // convert to 0-index
            // walk cost
            ll walk = llabs(x - 0);
            // teleport via j with x_j <= x+1
            ll cand = walk;
            // candidate1: A_min + (x+1)
            ll minA = segA.query(0, x+1);
            if (minA < INF) {
                cand = min(cand, minA + (x+1));
            }
            // candidate2: B_min - (x+1)
            ll minB = segB.query(x, N);
            if (minB < INF) {
                cand = min(cand, minB - (x+1));
            }
            cout << cand << '\n';
        } else if (type == 2) {
            int x;
            ll c;
            cin >> x >> c;
            --x;
            // update teleporter at x: keep best cost
            if (c < bestCost[x]) {
                bestCost[x] = c;
                // A = c - (x+1)
                ll A = c - ll(x+1);
                // B = c + (x+1)
                ll B = c + ll(x+1);
                segA.update(x, A);
                segB.update(x, B);
            }
        }
    }
    return 0;
}
0