区间修改,单点查询
可以用线段树做也可以用树状数组做
为了练习树状数组,这里采用树状数组方法
修改:向上修改,左端点+1,对于加多了的,右端点右面-1
查询:向下求和,根据我们的变化单点查询就变成了求和
<span style="font-size:18px;">var
n,l,r :longint;
i :longint;
c :array[0..100010] of longint;
procedure change(i,v:longint);
begin
while (i<=n) do
begin
inc(c[i],v);
inc(i,i and (-i));
end;
end;
function sum(i:longint):longint;
var
ans:longint;
begin
ans:=0;
while (i>0) do
begin
inc(ans,c[i]);
dec(i,i and (-i));
end;
exit(ans);
end;
begin
read(n);
while (n<>0) do
begin
fillchar(c,sizeof(c),0);
for i:=1 to n do
begin
read(l,r);
change(l,1);
change(r+1,-1);
end;
for i:=1 to n-1 do write(sum(i),' ');writeln(sum(n));
read(n);
end;
end.
</span>
——by Eirlys