結果

問題 No.833 かっこいい電車
ユーザー 👑 emthrmemthrm
提出日時 2019-05-24 22:06:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 2,870 bytes
コンパイル時間 1,302 ms
コンパイル使用メモリ 133,652 KB
実行使用メモリ 10,672 KB
最終ジャッジ日時 2023-09-14 20:52:05
合計ジャッジ時間 4,040 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
6,716 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 55 ms
7,108 KB
testcase_11 AC 81 ms
9,564 KB
testcase_12 AC 24 ms
6,016 KB
testcase_13 AC 14 ms
4,380 KB
testcase_14 AC 67 ms
9,716 KB
testcase_15 AC 33 ms
6,812 KB
testcase_16 AC 31 ms
7,696 KB
testcase_17 AC 15 ms
4,376 KB
testcase_18 AC 66 ms
7,324 KB
testcase_19 AC 28 ms
7,332 KB
testcase_20 AC 9 ms
4,684 KB
testcase_21 AC 46 ms
5,260 KB
testcase_22 AC 50 ms
10,136 KB
testcase_23 AC 31 ms
7,200 KB
testcase_24 AC 49 ms
9,744 KB
testcase_25 AC 60 ms
6,708 KB
testcase_26 AC 36 ms
9,056 KB
testcase_27 AC 47 ms
7,060 KB
testcase_28 AC 29 ms
4,956 KB
testcase_29 AC 38 ms
6,480 KB
testcase_30 AC 59 ms
10,672 KB
testcase_31 AC 39 ms
6,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
template <typename Monoid>
struct SegTree {
  using Fn = function<Monoid(Monoid, Monoid)>;

  SegTree(int sz_, const Fn fn_, const Monoid &UNITY_) : fn(fn_), UNITY(UNITY_) {
    init(sz_);
    dat.assign(sz << 1, UNITY);
  }

  SegTree(const vector<Monoid> &seq, const Fn fn_, const Monoid &UNITY_) : fn(fn_), UNITY(UNITY_) {
    int seq_sz = seq.size();
    init(seq_sz);
    dat.resize(sz << 1);
    REP(i, seq_sz) dat[i + sz] = seq[i];
    for (int i = sz - 1; i > 0; --i) dat[i] = fn(dat[i << 1], dat[(i << 1) + 1]);
  }

  void update(int node, const Monoid &value) {
    node += sz;
    dat[node] = value;
    while (node >>= 1) dat[node] = fn(dat[node << 1], dat[(node << 1) + 1]);
  }

  Monoid query(int left, int right) {
    Monoid l_res = UNITY, r_res = UNITY;
    for (left += sz, right += sz; left < right; left >>= 1, right >>= 1) {
      if (left & 1) {
        l_res = fn(l_res, dat[left]);
        ++left;
      }
      if (right & 1) {
        --right;
        r_res = fn(dat[right], r_res);
      }
    }
    return fn(l_res, r_res);
  }

  inline Monoid operator[](const int idx) const { return dat[idx + sz]; }

private:
  int sz = 1;
  const Fn fn;
  const Monoid UNITY;
  vector<Monoid> dat;

  void init(int sz_) { while (sz < sz_) sz <<= 1; }
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, q; cin >> n >> q;
  vector<long long> a(n); REP(i, n) cin >> a[i];
  SegTree<long long> seg(a, [](long long a, long long b) { return a + b; }, 0LL);
  set<int> st;
  REP(i, n + 1) st.insert(i);
  while (q--) {
    int query, x; cin >> query >> x;
    if (query == 1) {
      st.erase(x);
    } else if (query == 2) {
      st.insert(x);
    } else if (query == 3) {
      --x;
      seg.update(x, seg[x] + 1);
    } else {
      --x;
      auto it2 = st.upper_bound(x);
      auto it1 = it2;
      --it1;
      cout << seg.query(*it1, *it2) << '\n';
    }
  }
  return 0;
}
0