A simple problem with Integers
| Time limit:5000 Ms |
|
Memory limit:131072 K |
| Total submissions:46553 |
|
Accepted:13655 |
| Case time limit:2000 ms |
Description You haveNIntegers,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 numbersNAndQ. 1 ≤N,Q≤ 100000. The second line containsNNumbers, the initial valuesA1,A2 ,...,An.-1000000000 ≤AI≤ 1000000000. Each of the nextQLines represents an operation. "CA B C"Means addingCTo eachAA,AA+ 1 ,...,AB.-10000 ≤C≤ 10000. "QA B"Means querying the sumAA,AA+ 1 ,...,AB. Output You need to answer allQCommands in order. One answer in a line. Sample Input 10 51 2 3 4 5 6 7 8 9 10q 4 4Q 1 10q 2 4C 3 6 3q 2 4 Sample output 455915 Hint The sums may exceed the range of 32-bit integers. Source Poj monthly -- 2007.11.25, Yang Yi |
The questions are classic, but pay attention to them in some places.. For details, seeCode.
# Include <iostream> # include <string. h> # include <stdio. h> using namespace STD; const int maxn = 100100; int N, Q; typedef _ int64 ll; struct node {int L, R; ll addv; ll sum ;} SMT [3 * maxn]; void btree (int l, int R, int K) {int ls, RS, mid; SMT [K]. L = L; SMT [K]. R = r; SMT [K]. addv = 0; If (SMT [K]. L = SMT [K]. r) {scanf ("% i64d", & SMT [K]. sum); return;} ls = k <1; RS = k <1 | 1; Mid = (SMT [K]. L + SMT [K]. r)> 1; btree (L, mid, ls); btree (Mid + 1, R, RS); SMT [K]. sum = SMT [ls]. sum + SMT [RS]. SUM;} void Update (int l, int R, int K, int c) {int ls, RS, mid; If (SMT [K]. L = L & SMT [K]. R = r) {SMT [K]. addv + = C; return;} ls = k <1; RS = k <1 | 1; Mid = (SMT [K]. L + SMT [K]. r)> 1; if (SMT [K]. addv) {SMT [ls]. addv + = SMT [K]. addv; SMT [RS]. addv + = SMT [K]. addv; SMT [K]. addv = 0;} If (L> mid) Update (L, R, RS, c); else if (r <= mid) Update (L, R, ls, c); else {Update (L, mid, ls, c); Update (Mid + 1, R, RS, C );} SMT [K]. sum = SMT [ls]. sum + (SMT [ls]. r-SMT [ls]. L + 1) * SMT [ls]. addv + SMT [RS]. sum + (SMT [RS]. r-SMT [RS]. L + 1) * SMT [RS]. addv;} ll Qu (int l, int R, int K) {int ls, RS, mid; If (SMT [K]. L = L & SMT [K]. R = r) return SMT [K]. sum + (SMT [K]. r-SMT [K]. L + 1) * SMT [K]. addv; LS = k <1; RS = k <1 | 1; Mid = (SMT [K]. L + SMT [K]. r)> 1; if (SMT [K]. addv) {SMT [ls]. addv + = SMT [K]. addv; SMT [RS]. addv + = SMT [K]. addv; SMT [K]. sum + = (SMT [K]. r-SMT [K]. L + 1) * SMT [K]. addv; // Pay attention to update sum after downloading; otherwise SMT [k] will fail during the next query. addv = 0;} If (L> mid) return Qu (L, R, RS); else if (r <= mid) return Qu (L, R, ls ); else return Qu (L, mid, ls) + Qu (Mid + 1, R, RS);} int main () {int A, B, C; char COM [10]; while (~ Scanf ("% d", & N, & Q) {btree (1, n, 1); gets (COM); While (Q --) {scanf ("% s", com); If (COM [0] = 'C') {scanf ("% d", &, & B, & C); Update (A, B, 1, C);} else {scanf ("% d", & A, & B ); printf ("% i64d \ n", Qu (A, B, 1) ;}} return 0 ;}
10 221 2 3 4 5 6 7 8 9 10q 4 4C 1 10 3C 6 10 3C 6 9 3C 8 9-100c 7 9 3C 7 10 3C 1 10 3q 6 10q 6 9Q 8 9Q 7 9Q 7 10q 1 10q 2 4C 3 6 3q 9 9Q 1 1q 5 5q 6 6q 7 7q 6 8ans4-82-104-147-122-100-3727-737142125-28