标记永久化,除非想(MLE)
忽然感到主席树不过是函数式的树套树
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
//#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("
----------
")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 100007;
struct ChairmanTree{
int l, r;
long long sum, tag;
}t[N * 40];
int treeIndex, T[N];
inline void Pushup(int &rt, int &l, int &r){
t[rt].sum = t[t[rt].l].sum + t[t[rt].r].sum + 1ll * (r - l + 1) * t[rt].tag;
}
inline void Build(int &rt, int l, int r){
rt = ++treeIndex;
if(l == r){
io >> t[rt].sum;
return;
}
int mid = (l + r) >> 1;
Build(t[rt].l, l, mid), Build(t[rt].r, mid + 1, r);
Pushup(rt, l, r);
}
inline void Updata(int &rt, int pre, int l, int r, int L, int R, long long w) {
rt = ++treeIndex;
t[rt] = t[pre]; // it need to be initialed here
if(L <= l && r <= R){
t[rt].tag += w;
t[rt].sum += 1ll * (r - l + 1) * w;
return;
}
// t[rt] = t[pre]; // not here !
int mid = (l + r) >> 1;
if(L <= mid)
Updata(t[rt].l, t[pre].l, l, mid, L, R, w);
if(R > mid)
Updata(t[rt].r, t[pre].r, mid + 1, r, L, R, w);
Pushup(rt, l, r);
}
inline long long Query(int rt, int l, int r, int L, int R, long long tot){
if(L <= l && r <= R) return t[rt].sum + 1ll * (r - l + 1) * tot;
tot += t[rt].tag;
int mid = (l + r) >> 1;
long long sum = 0;
if(L <= mid)
sum += Query(t[rt].l, l, mid, L, R, tot);
if(R >= mid+1)
sum += Query(t[rt].r, mid+1, r, L, R, tot);
return sum;
}
int tim;
int main(){
FileOpen();
int n, m;
while(~scanf("%d%d", &n, &m)){
treeIndex = tim = 0;
Build(T[tim], 1, n);
while (m--) {
char opt;
for(opt = getchar(); opt != 'C' && opt != 'Q' && opt != 'H' && opt != 'B'; opt = getchar());
if(opt == 'C'){
int l, r, w;
io >> l >> r >> w;
++tim;
Updata(T[tim], T[tim - 1], 1, n, l, r, w);
}
else if(opt == 'Q'){
int l, r;
io >> l >> r;
printf("%lld
", Query(T[tim], 1, n, l, r, 0));
}
else if(opt == 'H'){
int l, r, edition;
io >> l >> r >> edition;
printf("%lld
", Query(T[edition], 1, n, l, r, 0));
}
else if(opt == 'B'){
int newTime;
io >> newTime;
tim = newTime;
}
}
}
return 0;
}