bzoj3295

时间:2024-01-09 12:42:50

没什么好说的,树套树应该随便搞
我在128MB空间下大胆的写了主席树
当然要把原树和修改树分开来建
没有然后了

 type node=record
l,r,s:longint;
end; var tree:array[..] of node;
c:array[..] of int64;
h,ph,a,b,d1,d2:array[..] of longint;
dd1,dd2:array[..,..] of longint;
d,t1,t2,i,x,y,n,m,t:longint;
ans:int64; function lowbit(x:longint):longint;
begin
exit(x and (-x));
end; procedure add(x:longint);
begin
while x<=n do
begin
c[x]:=c[x]+;
x:=x+lowbit(x);
end;
end; function get(x:longint):int64;
begin
get:=;
while x> do
begin
get:=get+c[x];
x:=x-lowbit(x);
end;
end; procedure update(i:longint);
begin
tree[i].s:=tree[tree[i].l].s+tree[tree[i].r].s;
end; function build(l,r:longint):longint;
var m,q:longint;
begin
inc(t);
if l=r then
exit(t)
else begin
m:=(l+r) shr ;
q:=t;
tree[q].l:=build(l,m);
tree[q].r:=build(m+,r);
exit(q);
end;
end; function insert(l,r,last,x,y:longint):longint;
var m,q:longint;
begin
inc(t);
if l=r then
begin
tree[t].s:=tree[last].s+y;
exit(t);
end
else begin
m:=(l+r) shr ;
q:=t;
if x<=m then
begin
tree[q].r:=tree[last].r;
last:=tree[last].l;
tree[q].l:=insert(l,m,last,x,y);
end
else begin
tree[q].l:=tree[last].l;
last:=tree[last].r;
tree[q].r:=insert(m+,r,last,x,y);
end;
update(q);
exit(q);
end;
end; function getans(l,r,x,y:longint):longint;
var m,s,i:longint;
begin
if (x<=l) and (y>=r) then
begin
s:=;
for i:= to t2 do
s:=s+tree[d2[i]].s;
for i:= to t1 do
s:=s-tree[d1[i]].s;
exit(s);
end
else begin
m:=(l+r) shr ;
s:=;
if (x<=m) then
begin
inc(d);
for i:= to t1 do
begin
dd1[d,i]:=d1[i];
d1[i]:=tree[d1[i]].l;
end;
for i:= to t2 do
begin
dd2[d,i]:=d2[i];
d2[i]:=tree[d2[i]].l;
end;
s:=s+getans(l,m,x,y);
for i:= to t1 do
d1[i]:=dd1[d,i];
for i:= to t2 do
d2[i]:=dd2[d,i];
dec(d);
end;
if y>m then
begin
for i:= to t1 do
d1[i]:=tree[d1[i]].r;
for i:= to t2 do
d2[i]:=tree[d2[i]].r;
s:=s+getans(m+,r,x,y);
end;
exit(s);
end;
end; function ask(l,r,x,y:longint):longint;
var i:longint;
begin
if r= then exit();
if l=n+ then exit();
if x=n+ then exit();
if y= then exit();
dec(l);
t1:=;
t2:=;
d1[]:=ph[l];
d2[]:=ph[r];
i:=l;
while i> do
begin
if h[i]<> then
begin
inc(t1);
d1[t1]:=h[i];
end;
i:=i-lowbit(i);
end;
i:=r;
while i> do
begin
if h[i]<> then
begin
inc(t2);
d2[t2]:=h[i];
end;
i:=i-lowbit(i);
end;
d:=;
exit(getans(,n,x,y));
end; procedure del(x,y:longint);
begin
while x<=n do
begin
h[x]:=insert(,n,h[x],y,-);
x:=x+lowbit(x);
end;
end; begin
readln(n,m);
for i:= to n do
begin
readln(a[i]);
b[a[i]]:=i;
end;
for i:=n downto do
begin
ans:=ans+get(a[i]-);
add(a[i]);
end;
h[]:=build(,n);
ph[]:=h[];
for i:= to n do
ph[i]:=insert(,n,ph[i-],a[i],);
for i:= to m do
begin
readln(x);
y:=b[x];
writeln(ans);
ans:=ans-ask(,y-,x+,n)-ask(y+,n,,x-);
del(y,x);
end;
end.