結果

問題 No.3116 More and more teleporter
ユーザー tnakao0123
提出日時 2025-04-21 17:21:31
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,907 bytes
コンパイル時間 559 ms
コンパイル使用メモリ 58,888 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-04-21 17:21:35
合計ジャッジ時間 3,914 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:71:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   71 |   scanf("%d%d", &n, &qn);
      |   ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:78:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   78 |     scanf("%d", &op);
      |     ~~~~~^~~~~~~~~~~
main.cpp:82:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   82 |       scanf("%d", &x), x--;
      |       ~~~~~^~~~~~~~~~
main.cpp:91:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   91 |       scanf("%d%d", &x, &c), x--;
      |       ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3116.cc:  No.3116 More and more teleporter - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 200000;
const int INF = 1 << 30;

/* typedef */

template <typename T>
struct SegTreeMin {
  int e2;
  vector<T> nodes;
  T defv;
  SegTreeMin() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

SegTreeMin<int> st0, st1;

/* subroutines */

/* main */

int main() {
  int n, qn;
  scanf("%d%d", &n, &qn);

  st0.init(n, INF);
  st1.init(n, INF);
    
  while (qn--) {
    int op;
    scanf("%d", &op);

    if (op == 1) {
      int x;
      scanf("%d", &x), x--;

      auto c0 = st0.min_range(x, n) - x;
      auto c1 = st1.min_range(0, x + 1) - (n - 1 - x);
      int c = min(x, min(c0, c1));
      printf("%d\n", c);
    }
    else {
      int x, c;
      scanf("%d%d", &x, &c), x--;

      int c0 = c + x, c1 = c + (n - 1 - x);
      if (st0.geti(x) > c0) st0.set(x, c0);
      if (st1.geti(x) > c1) st1.set(x, c1);
    }
  }

  return 0;
}
0