题目:
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 88582 | Accepted: 27535 | |
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
Source
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define MAXN 200000
long long lazy[2 * MAXN];
long long t[2 * MAXN], a[2 * MAXN];
//int be[MAXN], ed[MAXN];
//int hips[2 * MAXN];
//double endhips[4 * MAXN];
//int maxn;
using namespace std;
void BuildTree(int l, int r, int x){
if (l == r)
{
t[x] = a[l];
return;
}
int m = (l + r) >> 1;
BuildTree(l, m, x << 1);
BuildTree(m + 1, r, x << 1 | 1);
t[x] = t[x << 1] + t[x << 1 | 1];
return;
}
void PushDown(int l, int r, int x){
int m = (l + r) >> 1;
if (lazy[x]){
t[x << 1] += lazy[x] * (m - l + 1);
t[x << 1 | 1] += lazy[x] * (r - m);
lazy[x << 1 | 1] += lazy[x];
lazy[x << 1] += lazy[x];
lazy[x] = 0;
}
return;
}
void Modify(int pos, int val, int l, int r, int x){
if (pos == l && r == l)
{
t[x] = val;
return;
}
int m = (l + r) >> 1;
if (pos <= m)
{
Modify(pos, val, l, m, x << 1);
}
else Modify(pos, val, m + 1, r, x << 1 | 1);
t[x] = t[x << 1] + t[x << 1 | 1];
return;
}
void SegModify(int L, int R, int val, int l, int r, int x){
if (l == L&&r == R){
t[x] += (R - L + 1)*val;
lazy[x] += val;
return;
}
PushDown(l, r, x);
int m = (l + r) >> 1;
if (R <= m)SegModify(L, R, val, l, m, x << 1);
else if (L>m)SegModify(L, R, val, m + 1, r, x << 1 | 1);
else {
SegModify(L, m, val, l, m, x << 1);
SegModify(m + 1, R, val, m + 1, r, x << 1 | 1);
}
t[x] = t[x << 1] + t[x << 1 | 1];
return;
}
long long Query(int L, int R, int l, int r, int x){
if (L == l && R == r)
return t[x];
PushDown(l, r, x);
int m = (l + r) >> 1;
if (R <= m)return Query(L, R, l, m, x << 1);
else if (L>m)return Query(L, R, m + 1, r, x << 1 | 1);
else return Query(L, m, l, m, x << 1) + Query(m + 1, R, m + 1, r, x << 1 | 1);
}
int main()
{
int nn, mm;
while (cin >> nn >> mm)
{
memset(t, 0, sizeof(t));
//memset(hips, 0, sizeof(hips));
//memset(endhips, 0, sizeof(endhips));
memset(lazy, 0, sizeof(lazy));
//maxn = 0;
int n = 1;
while (n < MAXN / 2)
{
n *= 2;
}
for (size_t i = 0; i < nn; i++)
{
scanf("%I64d", &a[i + 1]);
}
BuildTree(1, n, 1);
long long be, ed, tran;
char op;
while (mm--)
{
cin >> op;
if (op == 'C')
{
scanf("%I64d%I64d%I64d", &be, &ed, &tran);
SegModify(be, ed, tran, 1, n, 1);
}
else
{
scanf("%I64d%I64d", &be, &ed);
cout << Query(be, ed, 1, n, 1) << "\n";
}
}
}
return 0;
}