結果

問題 No.3116 More and more teleporter
ユーザー しとりん
提出日時 2025-04-20 18:39:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,366 bytes
コンパイル時間 2,037 ms
コンパイル使用メモリ 196,924 KB
実行使用メモリ 11,400 KB
最終ジャッジ日時 2025-04-20 18:39:30
合計ジャッジ時間 4,235 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct SegTree {
    int n;
    vector<ll> seg;
    SegTree(int _n): n(1) {
        while (n < _n) n <<= 1;
        seg.assign(2*n, INF);
    }
    void set_val(int k, ll x) {
        k += n;
        seg[k] = min(seg[k], x);
        while (k > 1) {
            k >>= 1;
            seg[k] = min(seg[2*k], seg[2*k+1]);
        }
    }
    ll query(int a, int b) {
        ll res = INF;
        for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
            if (a & 1) res = min(res, seg[a++]);
            if (b & 1) res = min(res, seg[--b]);
        }
        return res;
    }
};

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

    int N, Q;
    cin >> N >> Q;

    SegTree st1(N+1), st2(N+1);

    for(int qi = 0; qi < Q; qi++){
        int type;
        cin >> type;
        if(type == 1){
            int x;
            cin >> x;
            ll ans = x - 1;
            ll m1 = st1.query(1, x+1); 
            if(m1 < INF) ans = min(ans, m1 + x);
            ll m2 = st2.query(x, N+1); 
            if(m2 < INF) ans = min(ans, m2 - x);
            cout << ans << "\n";
        }
        else {
            int x; ll c;
            cin >> x >> c;
            st1.set_val(x, c - x);
            st2.set_val(x, c + x);
        }
    }

    return 0;
}
0