Title:
An array of length n, 4 operations:
(1) C lrd: The numbers in the interval [l, r] are incremented by 1, and the current timestamp is incremented by 1.
(2) Q lr: Query the sum of all numbers in the current timestamp interval [l, r].
(3) H lrt: Query the sum of the timestamp t interval [l, r].
(4) B t: Set the current timestamp to t .
(The first modification of t is 1;
Ideas: I was trapped by https://www.cnblogs.com/hsd-/p/6506175.html for an afternoon, well, I just don’t understand
Then I saw an idea on vjudge; I feel that this idea is clearer;
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <vector> using namespace std; #define pb push_back typedef long long ll; const int maxn = 100009; struct node { int l,r; ll sum,lazy; }T[maxn * 40 ]; int root[maxn],cur,tot; ll a[maxn]; void build(int l,int r,int &pos) { pos = to ++ ; T[pos].sum = T[pos].lazy = 0; // T[pos].l = l,T[pos].r = r; if(l==r) { T[pos].sum = a[l]; return; } int mid = (l+r)>>1; build(l,mid,T[pos].l); build(mid+1,r,T[pos].r); T[pos].sum = T[T[pos].l].sum + T[T[pos].r].sum; } void update( int L, int R, int &x, int y , int l, int r, int d) // x here is similar to pos above { x = all ++ ; T[x] = T[y]; if(l>=L && r<=R) { T[x].sum += 1ll*(r - l + 1 ) * d; // The L and R of the target interval are used here; because T[x].lazy += d is added to each value of the target interval ; return ; } int mid = (l+r)>>1; if(L <= mid) update(L, R, T[x].l, T[y].l, l, mid, d); if(R > mid) update(L, R, T[x].r, T[y].r, mid+1,r, d); T[x].sum = T[T[x].l].sum + T[T[x].r].sum + 1ll*(r-l+1) * T[x].lazy; } ll query(int L,int R,int x,int l,int r) { if(L<=l && R>=r) { return T[x].sum; } ll ans = 1ll*T[x].lazy*(min(R,r)-max(L,l) + 1); int mid = (l+r)>>1; if(R > mid) ans += query(L,R,T[x].r, mid+1, r); if(L<=mid) ans += query(L,R,T[x].l, l,mid); return ans; } intmain () { int n,m; int flag = 0; while(~scanf("%d%d", &n, &m)) { if(flag)puts(""); flag = 1; tot = 0,cur = 0; for(int i=1; i<=n; i++) { scanf("%lld", &a[i]); } // root[0] = tot++; build(1,n,root[0]); while(m--) { char op[ 20 ]; scanf("%s" , op); if(op[0]=='Q') { int l, r; scanf("%d%d",&l,&r); printf("%lld\n",query(l,r,root[cur],1,n)); } else if (at [ 0 ] == ' C ' ) { int l, r; ll d; scanf("%d%d%lld", &l, &r, &d); cur++; update(l,r,root[cur],root[cur-1], 1, n, d); } else if (at [ 0 ] == ' H ' ) { int l,r,t; scanf("%d%d%d",&l,&r,&t); printf("%lld\n",query(l,r,root[t],1,n)); } else if (at [ 0 ] == ' B ' ) { scanf("%d",&cur); } } } return 0; }