結果
| 問題 | No.876 Range Compress Query |
| コンテスト | |
| ユーザー |
noisy_noimin
|
| 提出日時 | 2019-09-07 14:17:13 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,475 bytes |
| 記録 | |
| コンパイル時間 | 960 ms |
| コンパイル使用メモリ | 98,492 KB |
| 実行使用メモリ | 9,252 KB |
| 最終ジャッジ日時 | 2024-06-26 10:03:52 |
| 合計ジャッジ時間 | 3,064 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 16 WA * 2 |
ソースコード
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>
using namespace std;
using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
{ 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};
const int MAX_N = 100000;
template<typename T> class RSQ {
public:
int n;
T zero = 0;
T data[4*MAX_N];
T values[4*MAX_N];
RSQ(int m, T init_value=0){
// 2のべき乗にする
n = 1;
while(n < m) n <<= 1;
zero = init_value;
for(int i=0;i<2*n-1;++i){
data[i] = init_value;
values[i] = init_value;
}
}
void bottom_up_update(int i) {
while(i){
i = (i-1)/2;
data[i] = data[2*i+1] + data[2*i+2];
}
}
void update(int s, int t, T x){
s += n-1;
t += n-1;
if(s > n-1) values[s-1] += x;
if(t > n-1) values[t-1] -= x;
data[s-1] = values[s-1]?1:0;
data[t-1] = values[t-1]?1:0;
bottom_up_update(s-1);
bottom_up_update(t-1);
// for(int i=0;i<2*n-1;++i) {
// cerr << data[i] << " ";
// }
// cerr << endl;
// for(int i=n-1;i<2*n-1;++i) {
// cerr << values[i] << " ";
// }
// cerr << endl;
}
// [s, t)
T find(int s, int t, int k, int kl, int kr){
if(kr <= s || t <= kl) return zero;
if(s <= kl && kr <= t) return data[k];
int kc = (kl+kr)/2;
T vl = find(s, t, 2*k+1, kl, kc);
T vr = find(s, t, 2*k+2, kc, kr);
return vl + vr;
}
T find(int s, int t) {
return find(s, t, 0, 0, n);
}
};
int main() {
std::ios::sync_with_stdio(false); cin.tie(0);
int n, q, query, l, r;
ll a;
cin >> n >> q;
RSQ<ll> tree(n+1);
for(int i=0;i<n;++i) {
cin >> a;
tree.update(i, i+1, a);
}
for(int i=0;i<q;++i) {
cin >> query >> l >> r;
--l; --r;
if(query == 1) {
cin >> a;
tree.update(l, r+1, a);
} else {
cout << tree.find(l, r+1) << endl;
}
}
}
noisy_noimin