Description
N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数。
100%的数据,1≤N、M、K≤200,000。
2016.2.26提高时限至60s
Solution
用last[i]表示加入第i条边后形成的环中最小编号,如果不形成环就是0
那么这里有一个很棒棒性质。对于询问[L,R],当last[i]< L时才会减少连通块的数量,反之则不会
求这个可以可持久化线段树,前面的加边弹边可以LCT维护边上的最小编号
感觉码力十分弱鸡还是得多写些数据结构
Code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
const int INF=0x3f3f3f3f;
const int N=500005;
struct LinkCutTree {
int son[N][2],fa[N],v[N],min[N];
bool rev[N],is_root[N];
void push_up(int x) {
min[x]=std:: min(v[x],std:: min(min[son[x][0]],min[son[x][1]]));
}
void push_down(int x) {
if (!x||!rev[x]) return ;
rev[son[x][0]]^=1;
rev[son[x][1]]^=1;
std:: swap(son[x][0],son[x][1]);
rev[x]=0;
}
void rotate(int x) {
int y=fa[x]; int z=fa[y];
int k=son[y][1]==x;
son[y][k]=son[x][!k];
if (son[x][!k]) fa[son[x][!k]]=y;
son[x][!k]=y; fa[y]=x;
fa[x]=z;
if (is_root[y]) {
is_root[y]=0;
is_root[x]=1;
} else son[z][son[z][1]==y]=x;
push_up(y); push_up(x);
}
void remove(int x) {
if (!is_root[x]) remove(fa[x]);
push_down(x);
}
void splay(int x) {
remove(x);
while (!is_root[x]) {
int y=fa[x]; int z=fa[y];
if (!is_root[y]) {
if ((son[z][1]==y)^(son[y][1]==x)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x) {
int y=0;
do {
splay(x);
is_root[son[x][1]]=1;
is_root[son[x][1]=y]=0;
push_up(x);
y=x; x=fa[x];
} while (x);
}
void mroot(int x) {
access(x); splay(x); rev[x]^=1;
}
void link(int x,int y) {
mroot(x); fa[x]=y;
}
void cut(int x,int y) {
mroot(x); access(y); splay(y);
son[y][0]=fa[x]=0;
is_root[x]=1;
push_up(y);
}
int query(int x,int y) {
mroot(x); access(y); splay(y);
return min[y];
}
} lct;
struct SegTree {
int root[N],L[N*21],R[N*21],sum[N*21],tot;
void modify(int pre,int &now,int tl,int tr,int x) {
now=++tot;
L[now]=L[pre]; R[now]=R[pre]; sum[now]=sum[pre]+1;
if (tl==tr) return ;
int mid=(tl+tr)>>1;
if (x<=mid) modify(L[pre],L[now],tl,mid,x);
else modify(R[pre],R[now],mid+1,tr,x);
}
int query(int pre,int now,int tl,int tr,int l,int r) {
if (r<l) return 0;
if (tl==l&&tr==r) return sum[now]-sum[pre];
int mid=(tl+tr)>>1;
if (r<=mid) return query(L[pre],L[now],tl,mid,l,r);
if (l>mid) return query(R[pre],R[now],mid+1,tr,l,r);
int qx=query(L[pre],L[now],tl,mid,l,mid);
int qy=query(R[pre],R[now],mid+1,tr,mid+1,r);
return qx+qy;
}
} st;
struct edge {int x,y,id;} e[N];
int fa[N],last[N];
int read() {
int x=0,v=1; char ch=getchar();
for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
return x*v;
}
int get_father(int x) {
if (!fa[x]) return x;
return fa[x]=get_father(fa[x]);
}
int main(void) {
freopen("data.in","r",stdin);
freopen("myp.out","w",stdout);
int n=read(),m=read(),k=read(),type=read();
lct.min[0]=lct.v[0]=INF;
rep(i,1,m) {
e[i]=(edge) {read(),read(),i};
lct.min[i+n]=lct.v[i+n]=i;
}
rep(i,1,n+m) lct.is_root[i]=1;
rep(i,1,n) lct.min[i]=lct.v[i]=INF;
rep(i,1,m) {
if (e[i].x==e[i].y) {
last[i]=i; continue;
}
if (get_father(e[i].x)!=get_father(e[i].y)) {
fa[get_father(e[i].x)]=get_father(e[i].y);
} else {
int pos=lct.query(e[i].x,e[i].y);
lct.cut(e[pos].x,pos+n);
lct.cut(e[pos].y,pos+n);
last[i]=pos;
}
lct.link(e[i].x,i+n);
lct.link(e[i].y,i+n);
}
st.root[0]=st.tot=1;
rep(i,1,m) st.modify(st.root[i-1],st.root[i],0,m,last[i]);
int lastans=0;
while (k--) {
int l=read(),r=read();
if (type) l^=lastans,r^=lastans;
printf("%d\n", lastans=n-st.query(st.root[l-1],st.root[r],0,m,0,l-1));
}
return 0;
}