1/* 2 the third template of the tree array (For CIDR blocks) is not explained!
I don't understand. Here: Here! 3 */4 # include <iostream> 5 # include <cstring> 6 # include <cstdio> 7 # include <algorithm> 8 # define n 100005 9 Using namespace STD; 10 11 typedef long ll; 12 13 LL ss [N], B [N], C [N]; 14 15 int n, m; 16 17 void addb (INT X, int K) {// B [I] indicates being 1... total total I sum 18 for (INT I = x; I <= N; I + = I & (-I )) B [I] + = x * k; 19} 20 21 void ADDC (INT X, int K) {// 1 .... increment 22 for (INT I = x; I> 0; I-= I & (-I) c [I] + = K for each node of the x node; 23} 24 25 Ll sumb (int x) {26 ll s = 0; 27 for (INT I = x; I> 0; I-= I & (-I )) S + = B [I]; 28 return s; 29} 30 31 LL sumc (INT X) {// x node total increment 32 ll s = 0; 33 for (INT I = x; I <= N; I + = I & (-I) S + = C [I]; 34 return S; 35} 36 37 ll sum (int x) {38 return x = 0? 0: sumc (x) * x + sumb (x-1); 39} 40 41 void Update (int A, int B, int c) {42 addb (B, c ); 43 ADDC (B, c); 44 If (A-1> 0) {45 addb (A-1,-C); 46 ADDC (A-1,-C ); 47} 48} 49 50 int main () {51 int m; 52 while (scanf ("% d", & N, & M )! = EOF) {53 for (INT I = 1; I <= N; ++ I) {54 scanf ("% LLD", & SS [I]); 55 ss [I] + = ss [I-1]; 56} 57 char ch [2]; 58 int A, B, C; 59 While (M --) {60 scanf ("% s", CH); 61 If (CH [0] = 'q') {62 scanf ("% d", &, & B); 63 printf ("% LLD \ n", ss [B]-ss [A-1] + sum (B)-sum (A-1 )); 64} 65 else {66 scanf ("% d", & A, & B, & C); 67 update (A, B, C ); 68} 69} 70} 71 return 0; 72}
Poj 3486 a simple problem with integers (the third type of tree array template CIDR Block)