Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 46936 | Accepted: 13755 | |
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
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> using namespace std; //#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , m , pos << 1 #define rson m + 1 , r , pos << 1|1 #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 100000 + 100; const int maxw = 10000000 + 20; const int MAXNNODE = 1000000 +10; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; #define eps 1e-8 #define mod 1000000007 typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pi; int n , m , x , y ; char ch[5]; LL sum[MAXN << 2] , lazy[MAXN << 2]; void push_up(int pos) { sum[pos] = sum [pos << 1] + sum[pos << 1|1]; } void push_down(int l , int r , int pos) { if(lazy[pos]) { lazy[pos << 1] += lazy[pos]; lazy[pos << 1|1] += lazy[pos]; sum[pos << 1] += lazy[pos] * (r - l + 1 - (( r - l + 1) >> 1)); sum[pos << 1|1] += lazy[pos] * ((r - l + 1) >> 1); lazy[pos] = 0; } } void build(int l , int r , int pos) { if(l == r) { scanf("%lld" , &sum[pos]); } else { int m = (l + r) >> 1; build(lson); build(rson); push_up(pos); } } void update(int L , int R , int l , int r , int pos , long long number) { if(L <= l&&r <= R) { lazy[pos] += number; sum[pos] += number * (r - l + 1); return; } push_down(l , r ,pos); int m = (l + r) >> 1; if(L <= m)update(L , R , lson , number); if(R > m)update(L , R , rson , number); push_up(pos); } LL querysum(int L , int R , int l , int r , int pos) { if(L <= l&&R >= r) { return sum[pos]; } push_down(l , r , pos); LL ans = 0; int m = (l + r) >> 1; if(L <= m)ans += querysum(L , R , lson); if(R > m)ans += querysum(L , R ,rson); return ans; } int main() { //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif // Online_Judge while(scanf("%d%d" , &n ,&m) == 2) { build(1 , n , 1); clr(lazy , 0); while(m--) { scanf("%s" , ch); if(ch[0] == 'Q') { scanf("%d%d" , &x , &y); printf("%lld\n" , querysum(x , y , 1 , n , 1)); } else { LL num; scanf("%d%d%lld" , &x , &y , &num); update(x , y , 1 , n , 1 , num); } } } return 0; }