您的位置:首页 > 其它

3212: Pku3468 A Simple Problem with Integers

2015-05-13 16:10 387 查看

3212: Pku3468 A Simple Problem with Integers

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1053 Solved: 468
[Submit][Status][Discuss]

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

题解:其实就是个英文版的线段树模板题,连乘法覆盖啥的都没有,也就是说所有的修改操作压根就没有顺序之分,可以爱怎么瞎搞怎么瞎搞= =

PS:话说我的线段树居然全方位怒踩树状数组是什麽节奏啊啊啊QAQ(上面的是树状数组,下面的是线段树)



树状数组:

/**************************************************************
Problem: 3212
User: HansBug
Language: Pascal
Result: Accepted
Time:268 ms
Memory:15852 kb
****************************************************************/

var
b,c:array[0..1000005] of int64;
i,j,k,l,m,n:longint;a1:int64;ch:char;
procedure addb(x:longint;y:int64);
begin
while x>0 do
begin
inc(b[x],y);
dec(x,x and (-x));
end;
end;
procedure addc(x:longint;y:int64);
var z:int64;
begin
z:=x;if x=0 then exit;
while x<=n do
begin
inc(c[x],z*y);
inc(x,x and (-x));
end;
end;
function sumb(x:longint):int64;
begin
sumb:=0;if x=0 then exit;
while x<=n do
begin
inc(sumb,b[x]);
inc(x,x and (-x));
end;
end;
function sumc(x:longint):int64;
begin
sumc:=0;
while x>0 do
begin
inc(sumc,c[x]);
dec(x,x and (-x));
end;
end;
function summ(x:longint):int64;
begin
exit(sumb(x)*int64(x)+sumc(x-1));
end;
procedure add(x,y:longint;z:int64);
begin
addb(y,z);addc(y,z);
addb(x-1,-z);addc(x-1,-z);
end;
function sum(x,y:longint):int64;
begin
exit(summ(y)-summ(x-1));
end;
begin
readln(n,m);
fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0);
for i:=1 to n do
begin
read(a1);
add(i,i,a1);
end;readln;
for i:=1 to m do
begin
read(ch);
case upcase(ch) of
'Q':begin
readln(j,k);
writeln(sum(j,k));
end;
'C':begin
readln(j,k,a1);
add(j,k,a1);
end;
end;
end;
end.


线段树:

/**************************************************************
Problem: 3212
User: HansBug
Language: Pascal
Result: Accepted
Time:84 ms
Memory:15852 kb
****************************************************************/

var
i,j,k,l,m,n:longint;a1:int64;ch:char;
a,b:array[0..1000005] of int64;
function min(x,y:longint):longint;
begin
if x<y then min:=x else min:=y;
end;
function max(x,y:longint):longint;
begin
if x>y then max:=x else max:=y;
end;
procedure built(z,x,y:longint);
begin
if x=y then
read(a[z])
else begin
built(z*2,x,(x+y) div 2);
built(z*2+1,(x+y) div 2+1,y);
a[z]:=a[z*2]+a[z*2+1];
end;
b[z]:=0;
end;
procedure add(z,x,y,l,r:longint;t:int64);
begin
if l>r then exit;
if (x=l) and (y=r) then
begin
inc(b[z],t);
exit;
end;
inc(a[z],t*int64(r-l+1));
add(z*2,x,(x+y) div 2,l,min(r,(x+y) div 2),t);
add(z*2+1,(x+y) div 2+1,y,max((x+y) div 2+1,l),r,t);
end;
function sum(z,x,y,l,r:longint;t:int64):int64;
var a1,a2:int64;
begin
if l>r then exit(0);
inc(t,b[z]);
if (x=l) and (y=r) then exit(a[z]+t*int64(y-x+1));
a1:=sum(z*2,x,(x+y) div 2,l,min(r,(x+y) div 2),t);
a2:=sum(z*2+1,(x+y) div 2+1,y,max((x+y) div 2+1,l),r,t);
exit(a1+a2);
end;
begin
readln(n,m);built(1,1,n);readln;
for i:=1 to m do
begin
read(ch);
case upcase(ch) of
'Q':begin
readln(j,k);
writeln(sum(1,1,n,j,k,0));
end;
'C':begin
readln(j,k,a1);
add(1,1,n,j,k,a1);
end;
end;
end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: