Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>
- #include<set>
- #include<vector>
- #include<algorithm>
- #include<set>
- #include<vector>
- #include<string>
- #include<stdlib.h>
- #include<math.h>
- #include<stdio.h>
- using namespace std;
- long long U[500013];
- long long tr[1000013];
- long long up[1000013];
- long long sumq[500013];
- void push(long long v,long long l, long long r)
- {
- if (l > r)
- {
- return;
- }
- if (l == r)
- {
- tr[v] += up[v];
- up[v] = 0;
- return;
- }
- up[2 * v] += up[v];
- up[2 * v + 1] += up[v];
- tr[v] += up[v] * (r - l + 1);
- up[v] = 0;
- }
- void build(long long v, long long l, long long r)
- {
- if (l > r)
- {
- return;
- }
- if (l == r)
- {
- tr[v] = U[l];
- return;
- }
- long long m = (l + r) / 2;
- build(2 * v, l, m);
- build(2 * v + 1, m + 1, r);
- tr[v] = tr[2 * v] + tr[2 * v + 1];
- }
- long long sum(long long v, long long l, long long r, long long l1, long long r1)
- {
- push(v, l, r);
- if (l > r)
- {
- return 0;
- }
- if (l1 > r)
- {
- return 0;
- }
- if (r1 < l)
- {
- return 0;
- }
- if ((l1 == l) && (r1 == r))
- {
- return tr[v];
- }
- long long x, y;
- long long m = (l + r) / 2;
- x = sum(2 * v, l, m, l1, min(m, r1));
- y = sum(2 * v + 1, m + 1, r, max(m+1,l1), r1);
- push(v, l, r);
- return x + y;
- }
- void upd_slo(long long v, long long l, long long r, long long l1, long long r1,long long x)
- {
- push(v, l, r);
- if (l > r)
- {
- return;
- }
- if (l1 > r)
- {
- return;
- }
- if (r1 < l)
- {
- return;
- }
- if ((l1 == l) && (r1 == r))
- {
- up[v] += x;
- push(v, l1, r1);
- return;
- }
- long long m = (l + r) / 2;
- upd_slo(2 * v, l, m, l1, min(m, r1), x);
- upd_slo(2 * v + 1, m + 1, r, max(m + 1, l1), r1, x);
- push(v, l, r);
- tr[v] = tr[2 * v] + tr[2 * v + 1];
- return;
- }
- int main()
- {
- int a,b;
- cin >> a >> b;
- for (int i = 1; i <= a; i++)
- {
- cin >> U[i];
- }
- build(1, 1, a);
- char y;
- long long l;
- long long r;
- long long x;
- long long f = 0;
- for (int i = 0; i < b; i++)
- {
- cin >> y;
- if (y == 'U')
- {
- cin >> l;
- cin >> r;
- cin >> x;
- upd_slo(1, 1, a, l, r, x);
- }
- if (y == 'S')
- {
- cin >> l;
- cin >> r;
- sumq[f]=sum(1, 1, a, l, r);
- f++;
- }
- }
- for (int i = 0; i < f; i++)
- {
- cout << sumq[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement