結果
| 問題 |
No.833 かっこいい電車
|
| コンテスト | |
| ユーザー |
Manuel1024
|
| 提出日時 | 2021-12-20 04:07:48 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 135 ms / 2,000 ms |
| コード長 | 1,827 bytes |
| コンパイル時間 | 819 ms |
| コンパイル使用メモリ | 72,256 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-15 15:13:03 |
| 合計ジャッジ時間 | 4,776 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 30 |
ソースコード
#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
template <typename T>
struct FenwickTree{
const int n;
vector<T> arr;
FenwickTree() = default;
FenwickTree(int n): n(n), arr(vector<T>(n+1, 0)){}
void add(int ind, T x){
for(int i = ind+1; i <= n; i += i & (-i)){
arr[i] += x;
}
}
// [0, end)
T sum_sub(int end){
T res = 0;
for(int i = end; i > 0; i -= i & (-i)){
res += arr[i];
}
return res;
}
// [start, end)
T sum(int start, int end){
return sum_sub(end) - sum_sub(start);
}
int lower_bound(T w){
if(w <= 0) return 0;
int x = 0;
int k = 1 << 30;
while(k > n) k /= 2;
for(; k > 0; k /= 2){
if(x+k <= n && arr[x+k] < w){
w -= arr[x+k];
x += k;
}
}
return x;
}
};
int main(){
int n, q;
cin >> n >> q;
vector<int> a(n);
for(auto &it: a) cin >> it;
FenwickTree<ll> sum(n);
for(int i = 0; i < n; i++) sum.add(i, a[i]);
FenwickTree<int> range(n);
for(int i = 1; i < n; i++) range.add(i, 1);
while(q--){
int op, x;
cin >> op >> x;
--x;
if(op == 1){
if(range.sum(x+1, x+2) == 1) range.add(x+1, -1);
}else if(op == 2){
if(range.sum(x+1, x+2) == 0) range.add(x+1, 1);
}else if(op == 3){
sum.add(x, 1);
}else{
// cout << range.sum(0, 1) << " " << range.sum(1, 2) << " ";
int r = range.lower_bound(range.sum(0, x+1)+1);
int l = range.lower_bound(range.sum(0, x+1));
// cout << l << " " << r << " ";
cout << sum.sum(l, r) << '\n';
}
}
return 0;
}
Manuel1024