CSES - Shared codeLink to this code:
https://cses.fi/paste/567abb6bc35edc71146fae/
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <unistd.h>
#define LL long long
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define X first
#define Y second
#define pb(a) push_back(a);
#define pii pair<int, int>
#define pLi pair<LL, int>
#define piL pair<int, LL>
#define pLL pair<LL, LL>
#define ar array
using namespace std;
const int mxn = 2e5 + 5;
struct itt
{
LL sum, ic, sv;
} it[4*mxn];
LL a[mxn];
void ct (int l, int r, int id)
{
if (l == r)
{
it[id].sum = a[l];
return;
}
int mid = (l+r)>>1;
ct(l, mid, id<<1); ct(mid+1, r, (id<<1)+1);
it[id].sum = it[id<<1].sum + it[(id<<1)+1].sum;
}
void down (int id, int st, int se)
{
int l = id<<1, r = l+1, mid = (st+se)>>1;
if (it[id].sv != 0)
{
it[l].sum = it[id].sv*(mid-st+1);
it[r].sum = it[id].sv*(se-mid);
it[l].ic = it[r].ic = 0;
it[id].sv = 0;
}
it[l].sum += it[id].ic*(mid-st+1);
it[l].ic += it[id].ic;
it[r].sum += it[id].ic*(se-mid);
it[r].ic += it[id].ic;
it[id].ic = 0;
return;
}
void up1 (int l, int r, int st, int se, int val, int id)
{
if (se < l || r < st) return;
if (st <= l && r <= se)
{
it[id].sum += val*(r-l+1);
it[id].ic += val;
return;
}
int mid = (l+r)>>1;
down(id, l, r);
up1(l, mid, st, se, val, id<<1); up1(mid+1, r, st, se, val, (id<<1)+1);
it[id].sum = it[id<<1].sum + it[(id<<1)+1].sum;
}
void up2 (int l, int r, int st, int se, int val, int id)
{
if (se < l || r < st) return;
if (st <= l && r <= se)
{
it[id].sum = val*(r-l+1);
it[id].sv = val; it[id].ic = 0;
return;
}
int mid = (l+r)>>1;
down(id, l, r);
up2(l, mid, st, se, val, id<<1); up2(mid+1, r, st, se, val, (id<<1)+1);
it[id].sum = it[id<<1].sum + it[(id<<1)+1].sum;
}
LL get (int l, int r, int st, int se, int id)
{
if (se < l || r < st) return 0;
if (st <= l && r <= se) return it[id].sum;
int mid = (l+r)>>1;
down(id, l, r);
LL x= get(l, mid, st, se, id<<1), y = get(mid+1, r, st, se, (id<<1)+1);
it[id].sum = it[id<<1].sum + it[(id<<1)+1].sum;
return (x+y);
}
int main()
{
//freopen("D:\\test.txt", "r", stdin);
//freopen("D:\\test2.txt", "w", stdout);
int n, q, ch, x, l, r; cin >> n >> q;
REP(i, 1, n) cin >> a[i]; ct(1, n, 1);
REP(i, 1, q)
{
cin >> ch >> l >> r;
if (ch == 3) {cout << get(1, n, l, r, 1) << endl; continue;}
cin >> x;
if (ch == 1) {up1(1, n, l, r, x, 1); continue;}
up2(1, n, l, r, x, 1);
}
}