线段树分治
简介
线段树分治,又称线段树时间分治。一般用于维护一个操作出现又消失的问题。
线段树分治可以将难以维护的删除转化为容易维护的撤销操作。
模板题
在
思路
我们建立一个
这样,就可以通过前序遍历线段树,到一个点就加边,离开时撤销的思路得到每一个时刻的状态了。
二分图可以通过扩展域并查集(种类并查集)实现,然而,是很难进行删除操作的,而且因为同时存在加边和删边,不能使用惯用的正难则反的思路。
不过,如果不路径压缩,是可以进行撤销的。但是为了
具体的,用一个栈来维护现在的所有边,加入的时候 push,离开的时候 pop。
单调栈参考实现
struct UF
{
private:
int fa[200005];
int sz[200005];
struct frame
{
int x,y;
bool lst;
};
vector<frame> st;
public:
bool isbip;
void init(int n)
{
for(int i = 1;i <= 2*n;i++)
{
fa[i] = i;
sz[i] = 1;
}
isbip = 1;
}
int find(int x)
{
while(fa[x] != x)
{
x = fa[x];
}
return x;
}
bool check(int x,int y)
{
int one = find(x);
int two = find(y);
if(one == two)
{
return false;
}
return true;
}
void merge(int x,int y,bool newi)
{
int one = find(x);
int two = find(y);
bool lst = isbip;
isbip &= newi;
if(one == two)
{
st.push_back({one,two,lst});
return;
}
if(sz[one] > sz[two])
{
swap(one,two);
}
st.push_back({one,two,lst});
fa[one] = two;
sz[two] += sz[one];
return;
}
void undo()
{
frame now = st[st.size()-1];
st.pop_back();
isbip = now.lst;
if(now.x == now.y)return;
fa[now.x] = now.x;
sz[now.y] -= sz[now.x];
return;
}
}uf;
Code
#include <bits/stdc++.h>
using namespace std;
int n,m,k;
struct UF
{
private:
int fa[200005];
int sz[200005];
struct frame
{
int x,y;
bool lst;
};
vector<frame> st;
public:
bool isbip;
void init(int n)
{
for(int i = 1;i <= 2*n;i++)
{
fa[i] = i;
sz[i] = 1;
}
isbip = 1;
}
int find(int x)
{
while(fa[x] != x)
{
x = fa[x];
}
return x;
}
bool check(int x,int y)
{
int one = find(x);
int two = find(y);
if(one == two)
{
return false;
}
return true;
}
void merge(int x,int y,bool newi)
{
int one = find(x);
int two = find(y);
bool lst = isbip;
isbip &= newi;
if(one == two)
{
st.push_back({one,two,lst});
return;
}
if(sz[one] > sz[two])
{
swap(one,two);
}
st.push_back({one,two,lst});
fa[one] = two;
sz[two] += sz[one];
return;
}
void undo()
{
frame now = st[st.size()-1];
st.pop_back();
isbip = now.lst;
if(now.x == now.y)return;
fa[now.x] = now.x;
sz[now.y] -= sz[now.x];
return;
}
}uf;
struct node
{
int l,r;
vector<pair<int,int> > st;
};
node tre[200005];
int lson[200005];
int rson[200005];
int tot;
int build(int l,int r)
{
int now = ++tot;
if(l == r)
{
tre[now] = {l,r};
return now;
}
int mid = (l+r)/2;
lson[now] = build(l,mid);
rson[now] = build(mid+1,r);
tre[now] = {l,r};
return now;
}
void update(int id,int l,int r,int x,int y)
{
if(tre[id].l == l && tre[id].r == r)
{
tre[id].st.push_back({x,y});
return;
}
int mid = (tre[id].l+tre[id].r)/2;
if(r <= mid)
{
update(lson[id],l,r,x,y);
}
else if(l <= mid)
{
update(lson[id],l,mid,x,y);
update(rson[id],mid+1,r,x,y);
}
else
{
update(rson[id],l,r,x,y);
}
return;
}
void query(int id)
{
for(auto it:tre[id].st)
{
bool now = uf.check(it.first,it.second)&&uf.check(it.first+n,it.second+n);
uf.merge(it.first,it.second+n,now);
now = uf.check(it.first,it.second)&&uf.check(it.first+n,it.second+n);
uf.merge(it.first+n,it.second,now);
}//种类并查集(扩展域并查集)实现
if(tre[id].l == tre[id].r)
{
cout << (uf.isbip?"Yes\n":"No\n");
}
else
{
query(lson[id]);
query(rson[id]);
}
for(auto it:tre[id].st)
{
uf.undo();
uf.undo();
}
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
uf.init(n);
build(1,k);
while(m--)
{
int x,y,l,r;
cin >> x >> y >> l >> r;
if(l == r)continue;
update(1,l+1,r,x,y);
}
query(1);
return 0;
}