Description
You have N integers, a1, a2, ..., an. You need to deal with both kinds of operations. One type of operation is to add some given number to each number in a given interval. The other are to ask for the sum of numbers in a given interval.
Input
The first line contains the 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 ABC" means adding C to each of AA, aa+1, ..., Ab. -10000≤ C ≤10000.
"Q ab" 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
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.
Test instructions: give you n integers and Q operations, there are two operations, C l R x means to put [L, R] All the numbers are added x,q l R represents the output [L, r] The value of the and.
You can do it with a line tree and a tree-like array.
Method One: line segment tree. Because operation one needs to modify all the values within an interval, the worst case scenario is to modify the entire segment tree, which is too complex. So you can use a line tree to maintain two values, one is the value of the interval together, and the other is a value other than these common values.
#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map > #include <vector> #include <list> #include <set> #include <stack> #include <queue># Include <deque> #include <algorithm> #include <functional> #include <iomanip> #include < limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include < cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime>using Namespace Std;typedef long Long ll;const int maxn = (1 <<)-1;int N, q;//data maintenance interval common values, DATB maintains values other than these common values and LL data[ MAXN], datb[maxn];//to the interval [a, b) plus x//k is the number of the node, the corresponding interval is [L, r] void Add (int a, int b, int x, int k, int l, int r) {if (a <= L && b >= R) data[k] + = x; else if (b > L && a < R) {Datb[k] + = (min (b, R)-Max (A, L)) * x; Add (A, B, X, K*2+1, L, (l+r)/2); Add (A, B, X, k*2+2, (l+r)/2, R); }}//calculates [A, b] and//k is the number of the node, the corresponding interval is [L, R] ll sum (int a, int b, int k, int l, int r) {if (b <= L | | a >= r) retur n 0; if (a <= l && b >= R) return (R-L) * Data[k] + datb[k]; ll res = (min (b, R)-Max (A, L)) * data[k]; Res + = SUM (A, B, k*2+1, L, (l+r)/2); Res + = SUM (A, B, k*2+2, (l+r)/2, R); return res;} int main () {cin >> n >> q; for (int i = 0; i < n; ++i) {int num; scanf ("%d", &num); Add (i, i+1, num, 0, 0, N); } while (q--) {char s[5]; int L, r, X; scanf ("%s", s); if (s[0] = = ' C ') {scanf ("%d%d%d", &l, &r, &x); Add (L-1, R, X, 0, 0, N); } else {scanf ("%d%d", &l, &r); printf ("%lld\n", Sum (l-1, R, 0, 0, N)); }} return 0;}
method Two: A tree-like array. Similarly, it is possible to build a two tree array.
#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map > #include <vector> #include <list> #include <set> #include <stack> #include <queue># Include <deque> #include <algorithm> #include <functional> #include <iomanip> #include < limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include < cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime>using Namespace Std;typedef long Long ll;const int maxn = 100010;int N, q;//bitll bit0[maxn], bit1[maxn];ll sum (ll* b, int i) { ll s = 0; while (i > 0) {s + = b[i]; I &= i-1; } return s;} void Add (ll* b, int i, int x) {while (I <= N) {B[i] + = x; i + = i & (-i); }}int Main () {cin >> n >> q; for (int i = 1; I <= n; ++i) {int num; scanf ("%d",&num); Add (bit0, I, num); } while (q--) {char s[5]; int L, r, X; scanf ("%s", s); if (s[0] = = ' C ') {scanf ("%d%d%d", &l, &r, &x); Add (Bit0, L,-x* (L-1)); Add (Bit1, L, X); Add (Bit0, r+1, x*r); Add (Bit1, R+1,-X); } else {scanf ("%d%d", &l, &r); ll res = 0; Res + = SUM (bit0, R) + sum (bit1, R) * R; Res-= SUM (bit0, l-1) + sum (bit1, L-1) * (L-1); printf ("%lld\n", res); }} return 0;}
Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.
poj3468 (A simple problem with integers) segment tree + tree-like array