結果

問題 No.833 かっこいい電車
ユーザー ikdikd
提出日時 2019-05-24 23:31:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,858 bytes
コンパイル時間 1,023 ms
コンパイル使用メモリ 87,448 KB
実行使用メモリ 10,196 KB
最終ジャッジ日時 2023-09-14 20:57:30
合計ジャッジ時間 5,069 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
6,540 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 143 ms
6,944 KB
testcase_11 AC 189 ms
9,136 KB
testcase_12 AC 56 ms
5,748 KB
testcase_13 AC 39 ms
4,376 KB
testcase_14 AC 151 ms
9,432 KB
testcase_15 AC 74 ms
6,444 KB
testcase_16 AC 56 ms
7,184 KB
testcase_17 AC 50 ms
4,380 KB
testcase_18 AC 177 ms
7,072 KB
testcase_19 AC 55 ms
7,104 KB
testcase_20 AC 15 ms
4,696 KB
testcase_21 AC 143 ms
4,848 KB
testcase_22 AC 97 ms
9,856 KB
testcase_23 AC 63 ms
7,024 KB
testcase_24 AC 97 ms
9,360 KB
testcase_25 AC 163 ms
6,444 KB
testcase_26 AC 68 ms
8,336 KB
testcase_27 AC 116 ms
6,716 KB
testcase_28 AC 81 ms
4,964 KB
testcase_29 AC 98 ms
6,264 KB
testcase_30 AC 107 ms
10,196 KB
testcase_31 AC 138 ms
6,544 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<pair<int, int>> seqs;
  rep(i, n) seqs.insert({i, i});
  while (q--) {
    int t, x;
    cin >> t >> x;
    x--;
    if (t == 1) {
      auto itl = seqs.upper_bound({x, n}), itr = seqs.upper_bound({x + 1, n});
      itl--;
      itr--;
      if (itl == itr) continue;
      auto nxt = make_pair(itl->first, itr->second);
      seqs.erase(itl);
      seqs.erase(itr);
      seqs.insert(nxt);
    } else if (t == 2) {
      auto itl = seqs.upper_bound({x, n}), itr = seqs.upper_bound({x + 1, n});
      itl--;
      itr--;
      if (itl != itr) continue;
      auto nxtl = make_pair(itl->first, x),
           nxtr = make_pair(x + 1, itr->second);
      seqs.erase(itl);
      seqs.insert(nxtl);
      seqs.insert(nxtr);
    } else if (t == 3) {
      s.add(x, 1);
    } else {
      auto it = seqs.upper_bound({x, n});
      it--;
      cout << s.sum(it->first, it->second + 1) << endl;
    }
  }
  return 0;
}
0