// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
#else
#define debug(x...)
#define Debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define NaN (int)(0x8b88e1d0595d51d1)
#define double long double
#define int long long
#define MAXN 200010
template <class T> struct segtree {
struct node {
int l, r;
T val;
node *ch[2];
};
int cnt = 0;
T DEFAULT_VALUE;
node *newnode(int l, int r) {
node *x = &datalist[cnt++];
x->l = l, x->r = r;
x->ch[0] = x->ch[1] = nullptr;
x->val.itvsz = r - l + 1;
return x;
}
vector<int> a;
vector<node> datalist;
node *root;
segtree(vector<int> arr) {
a = arr, build();
}
void pushup(node *x) {
if (!x) return;
if (!x->ch[0] && !x->ch[1]) return;
if (x->ch[0]) pushdown(x->ch[0]);
if (x->ch[1]) pushdown(x->ch[1]);
if (x->ch[0] && x->ch[1]) x->val = x->ch[0]->val + x->ch[1]->val;
else x->val = x->ch[!x->ch[0]]->val;
}
void pushdown(node *x) {
if (!x) return;
if (x->ch[0]) x->val.pushdown(x->ch[0]->val);
if (x->ch[1]) x->val.pushdown(x->ch[1]->val);
x->val.clear();
}
T query(int ql, int qr) {
function<T(node*)> dfs = [&](node *x) -> T {
if (ql > x->r || qr < x->l) return DEFAULT_VALUE;
pushdown(x);
if (x->l >= ql && x->r <= qr) return x->val;
return dfs(x->ch[0]) + dfs(x->ch[1]);
};
return dfs(root);
}
void modify(int ql, int qr, int val, int tg) {
function<void(node*)> dfs = [&](node *x) -> void {
if (ql > x->r || qr < x->l) return;
pushdown(x);
if (x->l >= ql && x->r <= qr) {
// pushdown(x->ch[0]);
// pushdown(x->ch[1]);
x->val.update(val, tg);
return;
}
dfs(x->ch[0]), dfs(x->ch[1]);
pushup(x);
};
dfs(root);
}
void build() {
datalist.resize(sz(a) * 2);
function<node*(int, int)> dfs = [&](int l, int r) {
node *x = newnode(l, r);
if (r > l) x->ch[0] = dfs(l, (l+r) >> 1), x->ch[1] = dfs(((l+r)>>1) + 1, r);
else x->val.set(a[l]);
pushup(x);
return x;
};
root = dfs(0, sz(a)-1);
}
void graphviz_dump(string filename = "graph.dot") {
FILE *f = fopen(filename.c_str(), "w");
fprintf(f, "digraph {\n");
rep(i, 0, cnt) {
node *x = &datalist[i];
fprintf(f, " %lld;\n");
}
rep(i, 0, cnt) {
node *x = &datalist[i];
if (x->ch[0]) fprintf(f, " %lld -> %ld;\n", i, x->ch[0] - &datalist[0]);
if (x->ch[1]) fprintf(f, " %lld -> %ld;\n", i, x->ch[1] - &datalist[0]);
}
fprintf(f, "}\n");
}
};
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
struct value {
int sum = 0, tag1 = NaN, tag2 = NaN, itvsz;
value operator+(const value &other) const {
return {sum + other.sum, NaN, NaN, itvsz + other.itvsz};
}
void set(int rhs) {
this->sum = rhs;
}
void update(int rhs, int tg) {
if (tg == 1) {
if (tag2 == NaN) this->tag1 = this->tag1==NaN ? rhs : this->tag1 + rhs;
else this->tag2 += rhs;
}
else if (tg == 2) {
this->tag1 = NaN;
this->tag2 = rhs;
}
}
void pushdown(value &child) {
if (tag1 == NaN && tag2 == NaN) return;
if (tag1 != NaN) child.update(tag1, 1);
else if (tag2 != NaN) child.update(tag2, 2);
}
void clear() {
if (tag1 == NaN && tag2 == NaN) return;
if (tag1 != NaN) {
sum += tag1 * itvsz;
tag1 = NaN;
}
else if (tag2 != NaN) {
sum = tag2 * itvsz;
tag2 = NaN;
}
}
};
int n, q; cin >> n >> q;
vector<int> a(n);
rep(i, 0, n) cin >> a[i];
segtree<value> t(a);
rep(_, 0, q) {
int op, l, r; cin >> op >> l >> r; --l, --r;
if (op == 1) {
int x; cin >> x;
t.modify(l, r, x, 1);
}
else if (op == 2) {
int x; cin >> x;
t.modify(l, r, x, 2);
}
else if (op == 3) {
printf("%lld\n", t.query(l, r).sum);
}
}
return 0;
}
No Comments