bzoj3514

时间:2023-03-10 03:57:54
bzoj3514

好题+数据结构神题+感人肺腑pascal被卡系列,我下面的代码几乎写到最优
可怎耐bzoj上pascal开的是O1,c++开的是O2,这怎么可能跑得过!!!
还是说说方法吧,这是一道算贡献的好题,因为我们不可能把边加进去依次算连通块个数
我们考虑在区间[l,r]中每条边i是否能使两个连通块变成1个,什么样的边可以呢?
显然,这条边端点所在的连通块在i加入之前不连通或者是使这两个连通块相连的边号<l
这样算法就呼之欲出了,我们首先依次把边i加入,如果构成环,则弹出环上编号最小的边并记录,否则记为0
这里我们可以用link cut tree解决
下面每次询问,我们只要求[l,r]有多少个数小于l即可
这题有是否强制在线两种情况,在线可以直接rush,但是我第一次TLE之后又写了一遍离线,发现比原来快但还是远远慢于只在线的c++,实在没办法只好弃疗,说一下两种情况
在线:显然这是主席树的经典问题
离线:注意这个个数是满足区间减法的,于是我们可以拆成两个求前缀询问并排序(注意这里用计数排序才有显著的提升),然后按边编号的顺序依次加入然后用树状数组统计,具体见程序

 const inf=;
type node=record
l,r,s:longint;
end;
way=record
s,e:longint;
end;
point=record
be,op,y:longint;
end;
xxx=record
po,next:longint;
end; var son:array[..,..] of longint;
q,w,v,fa:array[..] of longint;
qq:array[..] of point;
ee:array[..] of xxx;
rev:array[..] of boolean;
tree:array[..*] of node;
e:array[..] of way;
p,f,a,h,anss:array[..] of longint;
len,j,k1,k2,n,m,k,ty,t,i,ans,l,r,mm:longint; function min(a,b:longint):longint;
begin
if w[a]>w[b] then exit(b) else exit(a);
end; function getf(x:longint):longint;
begin
if f[x]<>x then f[x]:=getf(f[x]);
exit(f[x]);
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; function root(x:longint):boolean;
begin
exit((son[fa[x],]<>x) and (son[fa[x],]<>x));
end; procedure update(x:longint);
begin
v[x]:=min(x,min(v[son[x,]],v[son[x,]]));
end; procedure push(x:longint);
begin
if rev[x] then
begin
swap(son[x,],son[x,]);
rev[son[x,]]:=not rev[son[x,]];
rev[son[x,]]:=not rev[son[x,]];
rev[x]:=false;
end;
end; procedure rotate(x,w:longint);
var y:longint;
begin
y:=fa[x];
if not root(y) then
begin
if son[fa[y],]=y then son[fa[y],]:=x
else son[fa[y],]:=x;
end;
fa[x]:=fa[y];
son[y,-w]:=son[x,w];
if son[x,w]<> then fa[son[x,w]]:=y;
son[x,w]:=y;
fa[y]:=x;
update(y);
end; procedure splay(x:longint);
var i,s,y:longint;
fl:boolean;
begin
i:=x;
s:=;
while not root(i) do
begin
inc(s);
q[s]:=i;
i:=fa[i];
end;
inc(s);
q[s]:=i;
for i:=s downto do
push(q[i]);
if s= then exit;
fl:=true;
while fl do
begin
y:=fa[x];
if y=q[s] then
begin
if son[y,]=x then rotate(x,)
else rotate(x,);
fl:=false;
end
else begin
if fa[y]=q[s] then fl:=false;
if son[fa[y],]=y then
begin
if son[y,]=x then rotate(y,)
else rotate(x,);
rotate(x,);
end
else begin
if son[y,]=x then rotate(x,)
else rotate(y,);
rotate(x,);
end;
end;
end;
update(x);
end; procedure access(x:longint);
var y:longint;
begin
y:=;
repeat
splay(x);
son[x,]:=y;
update(x);
y:=x;
x:=fa[x];
until x=;
end; procedure makeroot(x:longint);
begin
access(x);
splay(x);
rev[x]:=not rev[x];
end; procedure link(x,y:longint);
begin
makeroot(x);
fa[x]:=y;
end; procedure path(x,y:longint);
begin
makeroot(x);
access(y);
splay(y);
end; procedure cut(x,y:longint);
begin
path(x,y);
son[y,]:=;
fa[x]:=;
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 add(l,r,last,x:longint):longint;
var m,q:longint;
begin
inc(t);
if l=r then
begin
tree[t].s:=tree[last].s+;
exit(t);
end
else begin
m:=(l+r) shr ;
q:=t;
if x<=m then
begin
tree[q].r:=tree[last].r;
tree[q].l:=add(l,m,tree[last].l,x);
end
else begin
tree[q].l:=tree[last].l;
tree[q].r:=add(m+,r,tree[last].r,x);
end;
tree[q].s:=tree[tree[q].l].s+tree[tree[q].r].s;
exit(q);
end;
end; function ask(l,r,x,y:longint):longint;
var m,s:longint;
begin
if l=r then exit(tree[y].s-tree[x].s)
else begin
m:=(l+r) shr ;
if k1<=m then exit(ask(l,m,tree[x].l,tree[y].l))
else begin
s:=tree[tree[y].l].s-tree[tree[x].l].s;
exit(s+ask(m+,r,tree[x].r,tree[y].r));
end;
end;
end; procedure cha(var a,b:point);
var c:point;
begin
c:=a;
a:=b;
b:=c;
end; function lowbit(x:longint):longint;
begin
exit(x and (-x));
end; procedure jia(x,y:longint);
begin
inc(len);
ee[len].po:=y;
ee[len].next:=p[x];
p[x]:=len;
end; procedure ins(x:longint);
begin
while x<=m+ do
begin
inc(h[x]);
x:=x+lowbit(x);
end;
end; function an(x:longint):longint;
begin
an:=;
while x> do
begin
an:=an+h[x];
x:=x-lowbit(x);
end;
end; begin
readln(n,m,k,ty);
for i:= to n do
begin
f[i]:=i;
w[i]:=inf;
end;
t:=n;
w[]:=inf;
for i:= to m do
begin
readln(e[i].s,e[i].e);
if e[i].s=e[i].e then
begin
a[i]:=i;
continue;
end;
k1:=getf(e[i].s);
k2:=getf(e[i].e);
if k1=k2 then //并查集判连通
begin
path(e[i].s,e[i].e);
l:=v[e[i].e];
a[i]:=w[l];
cut(e[w[l]].s,l);
cut(e[w[l]].e,l);
end
else f[k1]:=k2;
inc(t); w[t]:=i;
link(e[i].s,t);
link(e[i].e,t);
end;
if ty= then //在线做法
begin
t:=;
h[]:=build(,m);
for i:= to m do
begin
h[i]:=add(,m,h[i-],a[i]);
end;
ans:=;
for i:= to k do
begin
readln(l,r);
l:=l xor ans;
r:=r xor ans;
if l>r then swap(l,r);
k1:=l-;
ans:=n-ask(,m,h[l-],h[r]);
writeln(ans);
end;
end
else begin //离线做法
t:=;
for i:= to k do
begin
readln(l,r);
inc(t);
qq[t].op:=-;
qq[t].y:=l;
qq[t].be:=i;
jia(r,t);
inc(t);
qq[t].op:=;
jia(l-,t);
qq[t].y:=l;
qq[t].be:=i;
end;
j:=;
a[m+]:=inf;
for i:= to m do
begin
while j<=i do
begin
ins(a[j]+); //树状数组编号从1开始,所以+
inc(j);
end;
l:=p[i];
while l<> do
begin
r:=ee[l].po;
inc(anss[qq[r].be],qq[r].op*an(qq[r].y));
l:=ee[l].next;
end;
end;
for i:= to k do
writeln(anss[i]+n);
end;
end.