結果
| 問題 |
No.833 かっこいい電車
|
| コンテスト | |
| ユーザー |
lowr
|
| 提出日時 | 2019-09-07 18:54:53 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 204 ms / 2,000 ms |
| コード長 | 1,712 bytes |
| コンパイル時間 | 2,533 ms |
| コンパイル使用メモリ | 199,036 KB |
| 最終ジャッジ日時 | 2025-01-07 17:15:22 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 30 |
ソースコード
#include <bits/stdc++.h>
using i64 = long long;
template <class T = long long int>
class Bit {
private:
int len;
T *arr;
T init;
public:
Bit(int length, T initialValue = 0) : len(length), init(initialValue) {
arr = new T[length + 1];
for (int i = 0; i <= length; i++) {
arr[i] = initialValue;
}
}
~Bit() {
delete[] arr;
}
void update(int a, T newval) {
for (int x = a; x <= len; x += x & -x) arr[x] += newval;
}
T query(int a) const {
T ret = init;
for (int x = a; x > 0; x -= x & -x) ret += arr[x];
return ret;
}
};
int main() {
int n, q;
std::cin >> n >> q;
std::set<int> s;
Bit<> bit(n + 10);
for (int i = 1; i <= n; i++) {
i64 in;
std::cin >> in;
bit.update(i, in);
s.insert(i);
}
while (q--) {
int t, x;
std::cin >> t >> x;
if (t == 1) {
auto it = s.find(x + 1);
if (it != s.end()) s.erase(it);
} else if (t == 2) {
auto it = s.find(x + 1);
if (it == s.end()) s.insert(x + 1);
} else if (t == 3) {
bit.update(x, 1);
} else {
auto it = s.lower_bound(x), jt = it;
int lb = it == s.end() || *it > x ? *--it : *it, ub;
if (jt == s.end()) {
ub = n + 1;
} else if (*jt == x) {
jt++;
if (jt == s.end()) ub = n + 1;
else ub = *jt;
} else {
ub = *jt;
}
std::cout << bit.query(ub - 1) - bit.query(lb - 1) << std::endl;
}
}
return 0;
}
lowr