結果
問題 |
No.833 かっこいい電車
|
ユーザー |
![]() |
提出日時 | 2019-05-25 11:17:34 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 39 ms / 2,000 ms |
コード長 | 1,697 bytes |
コンパイル時間 | 802 ms |
コンパイル使用メモリ | 79,348 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-02 03:52:54 |
合計ジャッジ時間 | 2,719 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> using namespace std; using ll = long long; template <class T> struct BIT { BIT(int n) : b(n + 1), n(n) {} void add(int i, T v) { for (int k = i + 1; k <= n; k += k & -k) b[k] += v; } T sum(int k) { T s = 0; for (; k > 0; k -= k & -k) s += b[k]; return s; } //合計がs以上になる最小のk n個の合計より大きければn+1が返る int lower_bound(T s) { if (s <= 0) return 0; int x = 0, w = 1; while (w << 1 <= n) w <<= 1; do { int k = x + w; if (k <= n && b[k] < s) { s -= b[k]; x += w; } } while (w >>= 1); return x + 1; } vector<T> b; int n; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; BIT<ll> ba(n); BIT<int> bb(n); vector<char> c(n - 1, 0); for (int i = 0; i < n; i++) { int a; cin >> a; ba.add(i, a); bb.add(i, 1); } for (int h = 0; h < q; h++) { int t, x; cin >> t >> x; x--; if (t == 1) { if (c[x] == 0) bb.add(x, -1), c[x] = 1; } else if (t == 2) { if (c[x] != 0) bb.add(x, +1), c[x] = 0; } else if (t == 3) { ba.add(x, 1); } else if (t == 4) { int s = bb.sum(x); int k0 = bb.lower_bound(s); int k1 = bb.lower_bound(s + 1); cout << ba.sum(k1) - ba.sum(k0) << '\n'; } } return 0; }