#include <cstdio> #include <cstring> #include <climits> #include <algorithm> #include <new> const int MAXN = 200000; template<size_t SIZE> struct MemoryPool { char buf[SIZE], *cur; MemoryPool(): cur(buf) {} void init() { cur = buf; } void *alloc(const int &size) { char *p = cur; cur += size; return p; } }; MemoryPool<(4 + 4 + 8 + 8 + 8 + 8) * MAXN * 5> pool; struct SegmentTree { int l, r; SegmentTree *lc, *rc; long long sum, tag; SegmentTree(int l, int r, SegmentTree *lc, SegmentTree *rc): l(l), r(r), lc(lc), rc(rc), sum(0), tag(0) {} void cover(long long delta) { sum += delta * (r - l + 1); tag += delta; } void pushDown() { if (tag) { lc->cover(tag); rc->cover(tag); tag = 0; } } void update(int l, int r, long long x) { if (l > this->r || r < this->l) return; else if (l <= this->l && r >= this->r) cover(x); else pushDown(), lc->update(l, r, x), rc->update(l, r, x), sum = lc->sum + rc->sum; } long long querySum(int l, int r) { if (l > this->r || r < this->l) return 0; else if (l <= this->l && r >= this->r) return sum; else return pushDown(), lc->querySum(l, r) + rc->querySum(l, r); } } *root; SegmentTree *build(int l, int r) { if (l == r) return new (pool.alloc(sizeof(SegmentTree))) SegmentTree(l, r, NULL, NULL); else { int mid = l + (r - l) / 2; return new (pool.alloc(sizeof(SegmentTree))) SegmentTree(l, r, build(l, mid), build(mid + 1, r)); } } int main() { int n, m; while (scanf("%d %d", &n, &m) != EOF) { root = NULL; pool.init(); root = build(1, n); for (int i = 1; i <= n; i++) { long long x; scanf("%lld", &x); root->update(i, i, x); } char s[2]; for (int i = 1; i <= m; i++) { scanf("%s", s); if (s[0] == 'Q') { int a, b; scanf("%d %d", &a, &b); printf("%lld\n", root->querySum(a, b)); } else if (s[0] == 'C') { int l, r; long long x; scanf("%d %d %lld", &l, &r, &x); root->update(l, r, x); } } } return 0; }
|