Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 123941 | Accepted: 38439 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
Hint
#include<iostream>
#include<cstring>
using namespace std;
#define ll long long
const int MAX = 5e5;
ll a[MAX], lazy[MAX];
void Update(int rt, int l, int r, int x, int y, int val)
{
if(x <= l && y >= r)
{
a[rt] += val * (r - l + 1);
lazy[rt] += val;
return;
}
if(lazy[rt])
{
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
a[rt << 1] += ((l + r) / 2 - l + 1) * lazy[rt];
a[rt << 1 | 1] += (r - (l + r) / 2) * lazy[rt];
lazy[rt] = 0;
}
int mid = (l + r) >> 1;
if(mid >= x)
Update(rt << 1, l, mid, x, y, val);
if(mid < y)
Update(rt << 1 | 1, mid + 1, r, x, y, val);
a[rt] = a[rt << 1] + a[rt << 1 | 1];
}
ll Query(int rt, int l, int r, int x, int y)
{
if(x <= l && y >= r)
return a[rt];
if(lazy[rt])
{
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
a[rt << 1] += ((l + r) / 2 - l + 1) * lazy[rt];
a[rt << 1 | 1] += (r - (l + r) / 2) * lazy[rt];
lazy[rt] = 0;
}
int mid = (l + r) >> 1;
ll sum = 0;
if(mid >= x)
sum += Query(rt << 1, l, mid, x, y);
if(y > mid)
sum += Query(rt << 1 | 1, mid + 1, r, x, y);
return sum;
}
int main()
{
ios::sync_with_stdio(false);
int n, m;
while(cin >> n >> m)
{
memset(a, 0, sizeof a);
memset(lazy, 0, sizeof lazy);
for(int i = 1, x; i <= n; i++)
{
cin >> x;
Update(1, 1, n, i, i, x);
}
char s[2];
for(int i = 0, x, y, z; i < m; i++)
{
cin >> s;
if(s[0] == 'Q')
{
cin >> x >> y;
cout << Query(1, 1, n, x, y) << endl;
}
else
{
cin >> x >> y >> z;
Update(1, 1, n, x, y, z);
}
}
}
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
#define mms(x) memset(x, 0, sizeof x)
#define ll long long
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define MAX 100005
ll tree[MAX << 2], lazy[MAX << 2];
int n, m;
void PushDown(int rt, int l, int r)
{
if(lazy[rt])
{
int mid = (l + r) >> 1;
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
tree[rt << 1] += (mid - l + 1) * lazy[rt];
tree[rt << 1 | 1] += (r - mid) * lazy[rt];
lazy[rt] = 0;
}
}
void PushUp(int rt)
{
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
void BuildTree(int rt, int l, int r)
{
lazy[rt] = 0;
if(l == r)
{
cin >> tree[rt];
return;
}
int mid = (l + r) >> 1;
BuildTree(lson);
BuildTree(rson);
PushUp(rt);
}
ll Query(int rt, int l, int r, int x, int y)
{
if(x <= l && r <= y)
return tree[rt];
PushDown(rt, l, r);
int mid = (l + r) >> 1;
ll ans = 0;
if(x <= mid)
ans += Query(lson, x, y);
if(mid < y)
ans += Query(rson, x, y);
PushUp(rt);
return ans;
}
void Update(int rt, int l, int r, int x, int y, int val)
{
if(x <= l && r <= y)
{
tree[rt] += val * (r - l + 1);
lazy[rt] += val;
return;
}
PushDown(rt, l, r);
int mid = (l + r) >> 1;
if(x <= mid)
Update(lson, x, y, val);
if(mid < y)
Update(rson, x, y, val);
PushUp(rt);
}
void init()
{
mms(lazy);
mms(tree);
BuildTree(1, 1, n);
}
int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m)
{
init();
for(int i = 0, xx, yy, zz; i < m; i++)
{
char s[5];
cin >> s;
if(s[0] == 'Q')
{
cin >> xx >> yy;
cout << Query(1, 1, n, xx, yy) << endl;
}
else
{
cin >> xx >> yy >> zz;
Update(1, 1, n, xx, yy, zz);
}
}
}
return 0;
}