A Simple Problem with Integers
Time Limit: 5000MSMemory Limit: 131072K
Total Submissions: 26062Accepted: 7202
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
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
_________________________________________________
给一串数,两种操作:一是将一段区间的每个数都加上一个数。二是询问一段区间的和。
_________________________________________________
线段树,没有其他多余的操作,也是以前学非递归的线段树时做的。
_________________________________________________
1 program Stone; 2 3 const T2=1 shl 17-1; 4 5 var i,j:longint; 6 7 n,q,x,y,z:int64; 8 9 ans:int64; 10 11 a,sx:array[1..2*T2+2]of int64; 12 13 b,sum:array[0..100000]of int64; 14 15 procedure add(x,y,z:int64); //修改 16 17 var s,i:int64; 18 19 begin 20 21 x:=x+T2-1;y:=y+T2+1;s:=1; 22 23 while (x xor y)<>1 do //非递归的线段树,主要采取二进制的思想吧。 24 25 begin 26 27 if (x and 1)=0 then begin 28 29 inc(a[x+1],z);i:=x+1; 30 31 repeat 32 33 sx[i]:=sx[i]+z*s;i:=i div 2; 34 35 until i=1; 36 37 end; 38 39 if (y and 1)=1 then begin 40 41 inc(a[y-1],z);i:=y-1; 42 43 repeat 44 45 sx[i]:=sx[i]+z*s;i:=i div 2; 46 47 until i=1; 48 49 end; 50 51 x:=x div 2;y:=y div 2;s:=s*2; 52 53 end; 54 55 end; 56 57 procedure que(x,y:int64); //询问 58 59 var r,l,s:int64; 60 61 begin 62 63 ans:=sum[y-1]-sum[x-2]; 64 65 x:=x+T2-1;y:=y+T2+1; 66 67 r:=0;l:=0;s:=1; 68 69 while (x xor y)<>1 do 70 71 begin 72 73 if (x and 1)=0 then begin l:=l+s;ans:=ans+sx[x+1];end; 74 75 if (y and 1)=1 then begin r:=r+s;ans:=ans+sx[y-1];end; 76 77 x:=x div 2;y:=y div 2;s:=s*2; 78 79 ans:=ans+l*a[x]+r*a[y]; 80 81 end; 82 83 while y<>1 do 84 85 begin 86 87 y:=y div 2;ans:=ans+a[y]*(l+r);s:=s*2; 88 89 end; 90 91 writeln(ans); 92 93 end; 94 95 procedure init; 96 97 var c,sp:char; 98 99 begin 100 101 readln(n,q); 102 103 for i:=1 to n do 104 105 begin 106 107 read(b[i]);sum[i]:=sum[i-1]+b[i]; //初始化 108 109 end; 110 111 readln; 112 113 for i:=1 to q do 114 115 begin 116 117 read(c,sp,x,y); 118 119 inc(x);inc(y); 120 121 if c=‘C‘ then begin read(z);add(x,y,z);end; 122 123 if c=‘Q‘ then que(x,y); 124 125 readln; 126 127 end; 128 129 end; 130 131 begin 132 133 assign(input,‘pku3468.in‘);assign(output,‘pku3468.out‘); 134 135 reset(input);rewrite(output); 136 137 init; 138 139 close(input);close(output); 140 141 end. 142 143 144 145