結果

問題 No.3116 More and more teleporter
ユーザー 37zigen
提出日時 2025-04-19 16:42:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 1,817 bytes
コンパイル時間 2,373 ms
コンパイル使用メモリ 200,268 KB
実行使用メモリ 14,164 KB
最終ジャッジ日時 2025-04-19 16:42:39
合計ジャッジ時間 4,620 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct SegmentTree {
    int n;
    vector<ll> st;
    SegmentTree(int _n): n(_n) {
        st.assign(4*n, INF);
    }
    void update(int p, ll val) { update(1,1,n,p,val); }
    ll query(int l, int r) { return query(1,1,n,l,r); }
private:
    void update(int node, int L, int R, int p, ll val) {
        if (L == R) {
            st[node] = min(st[node], val);
            return;
        }
        int mid = (L+R)/2;
        if (p <= mid) update(node*2, L, mid, p, val);
        else           update(node*2+1, mid+1, R, p, val);
        st[node] = min(st[node*2], st[node*2+1]);
    }
    ll query(int node, int L, int R, int i, int j) {
        if (j < L || R < i) return INF;
        if (i <= L && R <= j) return st[node];
        int mid = (L+R)/2;
        return min(query(node*2, L, mid, i, j),
                   query(node*2+1, mid+1, R, i, j));
    }
};

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

    int N, Q;
    cin >> N >> Q;
    SegmentTree segL(N), segR(N);

    while (Q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int x;
            cin >> x;
            // walk cost
            ll ans = (ll)x - 1;
            // teleport then walk from y <= x
            ll v1 = segL.query(1, x);
            if (v1 < INF) ans = min(ans, v1 + x);
            // teleport then walk from y >= x
            ll v2 = segR.query(x, N);
            if (v2 < INF) ans = min(ans, v2 - x);
            cout << ans << '\n';
        } else {
            int x;
            ll c;
            cin >> x >> c;
            // insert teleporter at x with cost c
            segL.update(x, c - x);
            segR.update(x, c + x);
        }
    }
    return 0;
}
0