結果

問題 No.3116 More and more teleporter
ユーザー keigo kuwata
提出日時 2025-04-20 19:14:47
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 1,203 bytes
コンパイル時間 1,780 ms
コンパイル使用メモリ 164,464 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-04-20 19:14:51
合計ジャッジ時間 3,780 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Fenwick {
  int n;
  vector<ll> t;
  Fenwick(int _n): n(_n), t(n+1, INF) {}
  // i∈[1..n] の点更新: t[i] = min(t[i], v)
  void update(int i, ll v) {
    for(; i<=n; i+=i&-i)
      t[i] = min(t[i], v);
  }
  // 1..i の最小値を返す
  ll query(int i) {
    ll r = INF;
    for(; i>0; i-=i&-i)
      r = min(r, t[i]);
    return r;
  }
};

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

  int N, Q;
  cin >> N >> Q;
  Fenwick bit1(N), bit2(N);
  while(Q--){
    int type;
    cin >> type;
    if(type == 1){
      int x;
      cin >> x;
      ll ans = x - 1;  // 直接歩くコスト
      ll bmin = bit1.query(x);
      if(bmin < INF) ans = min(ans, (ll)x + bmin);
      // 逆向きはインデックスを反転して同じクエリ
      int rx = N - x + 1;
      ll dmin = bit2.query(rx);
      if(dmin < INF) ans = min(ans, - (ll)x + dmin);
      cout << ans << "\n";
    } else {
      int x; ll c;
      cin >> x >> c;
      // x→反転インデックス
      int rx = N - x + 1;
      bit1.update(x, c - x);
      bit2.update(rx, c + x);
    }
  }
  return 0;
}
0