結果
問題 | No.833 かっこいい電車 |
ユーザー |
|
提出日時 | 2019-05-24 23:05:10 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 52 ms / 2,000 ms |
コード長 | 2,367 bytes |
コンパイル時間 | 2,224 ms |
コンパイル使用メモリ | 172,540 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-02 03:45:29 |
合計ジャッジ時間 | 3,865 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 41 ms
5,376 KB |
testcase_11 | AC | 52 ms
5,376 KB |
testcase_12 | AC | 17 ms
5,376 KB |
testcase_13 | AC | 14 ms
5,376 KB |
testcase_14 | AC | 41 ms
5,376 KB |
testcase_15 | AC | 21 ms
5,376 KB |
testcase_16 | AC | 15 ms
5,376 KB |
testcase_17 | AC | 18 ms
5,376 KB |
testcase_18 | AC | 50 ms
5,376 KB |
testcase_19 | AC | 15 ms
5,376 KB |
testcase_20 | AC | 5 ms
5,376 KB |
testcase_21 | AC | 45 ms
5,376 KB |
testcase_22 | AC | 25 ms
5,376 KB |
testcase_23 | AC | 17 ms
5,376 KB |
testcase_24 | AC | 25 ms
5,376 KB |
testcase_25 | AC | 48 ms
5,376 KB |
testcase_26 | AC | 18 ms
5,376 KB |
testcase_27 | AC | 33 ms
5,376 KB |
testcase_28 | AC | 27 ms
5,376 KB |
testcase_29 | AC | 29 ms
5,376 KB |
testcase_30 | AC | 26 ms
5,376 KB |
testcase_31 | AC | 32 ms
5,376 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; template<class T> struct BIT{ vector<T> bit; BIT(int n) : bit(n, 0){} T range_sum(int i, int j) {return sum(j) - sum(i - 1);} // return sum of v[i] to v[j] including both ends. T sum(int j){ T s = 0; for (; j >= 0; j = (j & (j + 1)) - 1) s += bit[j]; return s; } void add(int k, T a) {for (; k < bit.size(); k |= k + 1) bit[k] += a;} }; template<class T> struct BITrange{ vector<T> bit; BITrange(int n) : bit(n, 0){} T range_sum(int i, int j) {return sum(j - 1) - sum(i - 1);} // return sum of v[i] to v[j - 1]. T sum(int j){ T s = 0; for (; j >= 0; j = (j & (j + 1)) - 1) s += bit[j]; return s; } void add(int k, T a) {for (; k < bit.size(); k |= k + 1) bit[k] += a;} int queryR(int k){ if (this->range_sum(k, bit.size()) == bit.size() - k) return bit.size(); int OK = k; int NG = bit.size(); while (OK + 1 < NG){ int mid = (OK + NG) / 2; if (this->range_sum(k, mid) == mid - k){ OK = mid; }else{ NG = mid; } } return OK; } int queryL(int k){ if (this->range_sum(0, k) == k) return 0; int OK = k; int NG = 0; while (NG + 1 < OK){ int mid = (OK + NG) / 2; if (this->range_sum(mid, k) == k - mid){ OK = mid; }else{ NG = mid; } } return OK; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; BIT<long long> bit(N); for (int i = 0; i < N; ++i){ long long a; cin >> a; bit.add(i, a); } BITrange<int> bitr(N); while (Q--){ int q, x; cin >> q >> x; if (q == 1){ if (bitr.range_sum(x - 1, x) == 0){ bitr.add(x - 1, 1); } }else if (q == 2){ if (bitr.range_sum(x - 1, x) == 1){ bitr.add(x - 1, -1); } }else if (q == 3){ bit.add(x - 1, 1LL); }else if (q == 4){ int R = bitr.queryR(x - 1); int L = bitr.queryL(x - 1); cout << bit.range_sum(L, R) << '\n'; } } return 0; }