树状数组就线段树来说 无法进行区间修改和求最值
program pro;
type mine=array[0..100000]of longint;
var
n,m:longint;
a,b:mine;
st,i,j,x,y:longint;
function f(xx:longint):longint;
begin
exit(xx and (-xx));
end;
procedure plus(var sz:mine; xx:longint);//修改
var tmp:longint;
begin
while (xx<=n) do
begin
inc(sz[xx]);
inc(xx,f(xx));
end;
end;
function sum(var sz:mine; xx:longint):longint;//求和
var tot:longint;
begin
tot:=0;
while xx>0 do
begin
inc(tot,sz[xx]);
dec(xx,f(xx));
end;
exit(tot);
end;
begin
readln(n,m);
for i:=1 to m do
begin
read(st,x,y);
if st=1 then
begin
plus(a,x);
plus(b,y);
end
else
begin
writeln(sum(a,y)-sum(b,x-1));
end;
end;
end.
本文介绍了一种称为树状数组的数据结构,并通过一个简单的实现案例对比了其与线段树的区别。树状数组适用于单点更新及前缀和查询等操作,文中提供了具体的Pascal代码示例,包括修改和查询功能。
2357

被折叠的 条评论
为什么被折叠?



