結果
問題 |
No.876 Range Compress Query
|
ユーザー |
![]() |
提出日時 | 2019-09-08 21:05:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 61 ms / 2,000 ms |
コード長 | 2,196 bytes |
コンパイル時間 | 1,002 ms |
コンパイル使用メモリ | 110,644 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-27 15:13:07 |
合計ジャッジ時間 | 2,534 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 18 |
ソースコード
#include <functional> #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <string> #include <vector> #include <random> #include <bitset> #include <queue> #include <cmath> #include <stack> #include <set> #include <map> typedef long long ll; using namespace std; const ll MOD = 1000000007LL; template <typename T> class SegmentTree { using F = function<T(T, T)>; int n; F f; T t; vector <T> dat; public: SegmentTree() {}; SegmentTree(F f, T t) : f(f), t(t) {} void init(int n_) { n = 1; while (n < n_) n <<= 1; dat.assign(n << 1, t); } void build(const vector <T> &v) { auto n_ = (int)v.size(); init(n_); for (int i = 0; i < n_; i++) dat[n + i] = v[i]; for (int i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } void set_val(int k, T x) { dat[k += n] = x; while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]); } T query(int a, int b) { T vl = t, vr = t; for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, dat[l++]); if (r & 1) vr = f(dat[--r], vr); } return f(vl, vr); } }; typedef pair <ll, int> P; int main() { cin.sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; vector <ll> a(n); vector <int> b(n - 1); for (int i = 0; i < n; i++) { cin >> a[i]; if (i) { a[i - 1] = a[i] - a[i-1]; b[i - 1] = a[i - 1] != 0; } } auto f = [](int a, int b) { return a + b; }; SegmentTree<int> seg(f, 0); seg.build(b); while (q--) { int c, l, r; cin >> c >> l >> r; if (c == 1) { ll x; cin >> x; if (l > 1) { a[l - 2] += x; seg.set_val(l - 2, a[l - 2] != 0); } if (r < n) { a[r - 1] -= x; seg.set_val(r - 1, a[r - 1] != 0); } } else { cout << seg.query(l - 1, r - 1) + 1 << '\n'; } } return 0; }