結果

問題 No.833 かっこいい電車
ユーザー ikdikd
提出日時 2019-05-25 00:27:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,570 bytes
コンパイル時間 946 ms
コンパイル使用メモリ 85,392 KB
実行使用メモリ 10,200 KB
最終ジャッジ日時 2023-09-14 21:00:26
合計ジャッジ時間 4,769 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
6,444 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 125 ms
6,708 KB
testcase_11 AC 169 ms
9,024 KB
testcase_12 AC 50 ms
5,596 KB
testcase_13 AC 34 ms
4,380 KB
testcase_14 AC 137 ms
9,412 KB
testcase_15 AC 67 ms
6,564 KB
testcase_16 AC 53 ms
7,272 KB
testcase_17 AC 44 ms
4,376 KB
testcase_18 AC 149 ms
7,116 KB
testcase_19 AC 52 ms
7,116 KB
testcase_20 AC 14 ms
4,676 KB
testcase_21 AC 119 ms
4,856 KB
testcase_22 AC 91 ms
9,680 KB
testcase_23 AC 60 ms
6,912 KB
testcase_24 AC 91 ms
9,340 KB
testcase_25 AC 143 ms
6,508 KB
testcase_26 AC 64 ms
8,376 KB
testcase_27 AC 101 ms
6,664 KB
testcase_28 AC 71 ms
4,828 KB
testcase_29 AC 87 ms
6,300 KB
testcase_30 AC 94 ms
10,200 KB
testcase_31 AC 134 ms
6,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

struct SegmentTree {
  int n = 1;
  vector<int64_t> dat;
  SegmentTree(int N) {
    while (n < N) {
      n *= 2;
    }
    dat.resize(n * 2 - 1, 0);
  }
  void add(int i, int64_t x) {
    dat[i += n - 1] += x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];
    }
  }
  int64_t sum(int l, int r) { return sum(l, r, 0, 0, n); }
  int64_t sum(int ql, int qr, int i, int il, int ir) {
    if (qr <= il or ir <= ql) return 0;
    if (ql <= il and ir <= qr) return dat[i];
    auto m = (il + ir) / 2;
    return sum(ql, qr, i * 2 + 1, il, m) + sum(ql, qr, i * 2 + 2, m, ir);
  }
};

int main() {

  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (auto &e : a) {
    cin >> e;
  }
  SegmentTree s(n);
  rep(i, n) s.add(i, a[i]);
  set<int> tr; // 末尾が連結されていない電車の番号 0-indexed
  rep(i, n) tr.insert(i);
  while (q--) {
    int t, x;
    cin >> t >> x;
    x--;
    if (t == 1) {
      tr.erase(x);
    } else if (t == 2) {
      tr.insert(x);
    } else if (t == 3) {
      s.add(x, 1);
    } else {
      auto it = tr.lower_bound(x);
      if (it == tr.begin()) {
        cout << s.sum(0, *it + 1) << endl;
      } else {
        // 先頭が連結されていない電車を指すための +1
        // [l, r) に合わせるための +1
        cout << s.sum(*prev(it) + 1, *it + 1) << endl;
      }
    }
  }
  return 0;
}
0