結果
| 問題 | No.876 Range Compress Query |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-09 20:00:49 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 281 ms / 2,000 ms |
| コード長 | 3,951 bytes |
| 記録 | |
| コンパイル時間 | 1,882 ms |
| コンパイル使用メモリ | 181,716 KB |
| 実行使用メモリ | 15,976 KB |
| 最終ジャッジ日時 | 2024-06-28 14:13:54 |
| 合計ジャッジ時間 | 4,922 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
template<class P, class Q>
struct LazySegTree {
int dataSize;
vector<P> value;
vector<Q> lazy;
function<P(P, P)> f;
function<P(P, Q)> g;
function<Q(Q, Q)> h;
LazySegTree(vector<P> dat,
function<P(P, P)> f_, function<P(P, Q)> g_, function<Q(Q, Q)> h_) {
f = f_;
g = g_;
h = h_;
dataSize = 1;
int n = (int)dat.size();
while (dataSize < n)dataSize *= 2;
int treeSize = 2 * dataSize;
value = vector<P>(treeSize, P());
lazy = vector<Q>(treeSize, Q());
for (int i = 0; i < n; ++i) {
value[dataSize + i] = dat[i];
}
for (int i = dataSize - 1; i >= 0; --i) {
value[i] = f(value[i * 2], value[i * 2 + 1]);
}
}
void propagate(int index, int curL, int curR) {
if (lazy[index] != Q()) {
int left = index * 2, right = index * 2 + 1;
value[index] = g(value[index], lazy[index]);
if (curR - curL > 1) {
lazy[left] = h(lazy[left], lazy[index]);
lazy[right] = h(lazy[right], lazy[index]);
}
lazy[index] = Q();
}
}
void update(int index, int curL, int curR, int givenL, int givenR, Q x) {
propagate(index, curL, curR);
if (curR <= givenL || givenR <= curL)return;
if (givenL <= curL && curR <= givenR) {
lazy[index] = h(lazy[index], x);
propagate(index, curL, curR);
} else {
int mid = (curL + curR) / 2;
update(index * 2, curL, mid, givenL, givenR, x);
update(index * 2 + 1, mid, curR, givenL, givenR, x);
value[index] = f(value[index * 2], value[index * 2 + 1]);
}
}
void update(int l, int r, Q x) {
update(1, 0, dataSize, l, r, x);
}
P query(int l, int r) {
return query(1, 0, dataSize, l, r);
}
P query(int index, int curL, int curR, int givenL, int givenR) {
if (curR <= givenL || givenR <= curL)return P();
propagate(index, curL, curR);
if (givenL <= curL && curR <= givenR) {
return value[index];
} else {
int mid = (curL + curR) / 2;
P resL = query(index * 2, curL, mid, givenL, givenR);
P resR = query(index * 2 + 1, mid, curR, givenL, givenR);
return f(resL, resR);
}
}
};
struct P {
ll vl, vr, cnt;
P(ll vl_, ll vr_, ll cnt_) :vl(vl_), vr(vr_), cnt(cnt_) {}
P() :P(0, 0, 0) {}
};
P f(P a, P b) {
if (a.vl == 0)return b;
if (b.vl == 0)return a;
P c(a.vl, b.vr, a.cnt + b.cnt);
if (a.vr != b.vl)c.cnt++;
return c;
}
P g(P p, ll q) {
p.vl += q;
p.vr += q;
return p;
}
void solve() {
int N, Q;
cin >> N >> Q;
vector<P> ps(N);
rep(i, N) {
int a;
cin >> a;
ps[i].vl = ps[i].vr = a;
}
LazySegTree<P, ll> lst(ps, f, g, plus<ll>());
rep(ITER, Q) {
int q, l, r;
cin >> q >> l >> r;
--l;
if (q == 1) {
int x;
cin >> x;
lst.update(l, r, x);
} else {
ll ans = lst.query(l, r).cnt + 1;
cout << ans << endl;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
solve();
return 0;
}