线段树维护等差数列,结点维护首项+公差即可

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long ll;
const int maxn = ; #define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << ], rc[maxn << ], v[maxn << ];
ll nd[maxn << ], ad[maxn << ], s[maxn << ]; void pushup(int u);
void pushdown (int u); inline int length(int u) {
return rc[u] - lc[u] + ;
} inline void change (int u, ll a) {
v[u] = ;
ad[u] = ;
nd[u] = a;
s[u] = a * length(u);
} inline void maintain (int u, ll a, ll d) {
if (v[u] && lc[u] != rc[u]) {
pushdown(u);
pushup(u);
} v[u] = ;
nd[u] += a;
ad[u] += d;
ll n = length(u);
s[u] += a * n + (((n-) * n) / ) * d;
} inline void pushup (int u) {
s[u] = s[lson(u)] + s[rson(u)];
} inline void pushdown (int u) {
if (v[u]) {
change(lson(u), nd[u]);
change(rson(u), nd[u]);
v[u] = nd[u] = ;
} else if (nd[u] || ad[u]) {
maintain(lson(u), nd[u], ad[u]);
maintain(rson(u), nd[u] + length(lson(u)) * ad[u], ad[u]);
nd[u] = ad[u] = ;
}
} void build (int u, int l, int r) {
lc[u] = l;
rc[u] = r;
nd[u] = ad[u] = s[u] = ; if (l == r)
return;
int mid = (l + r) / ;
build(lson(u), l, mid);
build(rson(u), mid + , r);
pushup(u);
} void modify(int u, int l, int r, ll a, ll d) {
if (l <= lc[u] && rc[u] <= r) {
maintain(u, a + d * (lc[u] - l), d);
return;
} pushdown(u);
int mid = (lc[u] + rc[u]) / ;
if (l <= mid)
modify(lson(u), l, r, a, d);
if (r > mid)
modify(rson(u), l, r, a, d);
pushup(u);
} void set(int u, int l, int r, ll a) {
if (l <= lc[u] && rc[u] <= r) {
change(u, a);
return;
} pushdown(u);
int mid = (lc[u] + rc[u]) / ;
if (l <= mid)
set(lson(u), l, r, a);
if (r > mid)
set(rson(u), l, r, a);
pushup(u);
} ll query (int u, int l, int r) {
if (l <= lc[u] && rc[u] <= r)
return s[u]; pushdown(u);
ll ret = ;
int mid = (lc[u] + rc[u]) / ;
if (l <= mid)
ret += query(lson(u), l, r);
if (r > mid)
ret += query(rson(u), l, r);
pushdown(u);
return ret;
} int N; int main () {
while (~scanf("%d", &N)) {
char op[];
int l, r, x;
build(, , );
while (N--) {
scanf("%s%d%d", op, &l, &r);
if (op[] == 'A')
modify(, l, r, , );
else if (op[] == 'B')
modify(, l, r, r - l + , -);
else if (op[] == 'C') {
scanf("%d", &x);
set(, l, r, x);
} else
printf("%lld\n", query(, l, r));
}
}
return ;
}
04-17 23:02