BZOJ 3514 GERALD07加强版 (LCT+主席树)
题目大意:给定n个点m条边无向图,每次询问求当图中有编号为[L,R]的边时,整个图的联通块个数,强制在线
神题!(发现好久以前的题解没有写完诶)
我们要求图中联通块的个数,似乎不可搞啊。
联通块个数=n-树边条数!
考虑每条边的贡献,我们按编号从小到大暴力枚举每一条边。
考虑用$LCT$维护森林。
设新加入的这条边编号为$e$,连接了$x,y$两个点
如果$x,y$原来不连通,说明加入$e$会让图中多一条树边。边e对$L\in [1,e],R\geq e$的图$[L,R]$产生一点贡献
如果$x,y$原来就联通,说明加入$e$会产生环,并不会影响联通块个数。我们找出$e$所在环里编号最小的边$x$
当$L\leq x$时,删掉边$x$,图$[L,R]$的树边个数不变。
当$L>x$时,删掉边$x$,会让图$[L,R]$少一条树边。那么边$x$会对$L\in[x+1,e],R\geq e$的的图$[L,R]$产生一点贡献
如何处理询问?我们可以用主席树,主席树不同的根作为右端点,线段树维护对左端点的贡献。每次查询,主席树相减,然后单点查询
如何维护贡献?主席树上打差分实现区间修改
如何维护编号最小的边?把边转化成点扔到$LCT$里就行啦
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N1 401000
#define M1 201000
#define S1 (N1<<1)
#define T1 (N1<<2)
#define ll long long
#define uint unsigned int
#define rint register int
#define ull unsigned long long
#define dd double
#define il inline
#define inf 1000000000
using namespace std; int gint()
{
int ret=,fh=;char c=getchar();
while(c<''||c>''){if(c=='-')fh=-;c=getchar();}
while(c>=''&&c<=''){ret=ret*+c-'';c=getchar();}
return ret*fh;
}
int n,m,T,type;
struct Edge{
int x[M1],y[M1],cte;
void ae(int u,int v)
{cte++;x[cte]=u,y[cte]=v;}
}E;
struct SEG{
int ls[N1*],rs[N1*],sum[N1*],root[N1],tot;
void pushup(int rt){sum[rt]=sum[ls[rt]]+sum[rs[rt]];}
void build(int l,int r,int &rt)
{
int mid=(l+r)>>; rt=++tot;
if(l==r) return;
build(l,mid,ls[rt]);
build(mid+,r,rs[rt]);
}
void update(int x,int l,int r,int r1,int &r2,int w)
{
if((!r2)||(r1==r2)){r2=++tot,sum[r2]=sum[r1],ls[r2]=ls[r1],rs[r2]=rs[r1];}
if(l==r) {sum[r2]+=w; return;} int mid=(l+r)>>;
if(x<=mid) update(x,l,mid,ls[r1],ls[r2],w);
else update(x,mid+,r,rs[r1],rs[r2],w);
pushup(r2);
}
int query(int L,int R,int l,int r,int rt)
{
if(!rt||L>R) return ;
if(L<=l&&r<=R) return sum[rt];
int mid=(l+r)>>,ans=;
if(L<=mid) ans+=query(L,R,l,mid,ls[rt]);
if(R>mid) ans+=query(L,R,mid+,r,rs[rt]);
return ans;
}
}s;
namespace lct{
int ch[N1][],fa[N1],mi[N1],id[N1],rev[N1],tot;
int idf(int x){return ch[fa[x]][]==x?:;}
int isroot(int x){return (ch[fa[x]][]==x||ch[fa[x]][]==x)?:;}
void pushup(int x){mi[x]=min(x,min(mi[ch[x][]],mi[ch[x][]]));}
void revers(int x){swap(ch[x][],ch[x][]),rev[x]^=;}
void pushdown(int x){if(rev[x]){revers(ch[x][]),revers(ch[x][]),rev[x]^=;}}
void rot(int x)
{
int y=fa[x],ff=fa[y],px=idf(x),py=idf(y);
if(!isroot(y)) ch[ff][py]=x; fa[x]=ff;
fa[ch[x][px^]]=y,ch[y][px]=ch[x][px^];
ch[x][px^]=y,fa[y]=x;
pushup(y),pushup(x);
}
int stk[N1],tp;
void splay(int x)
{
int y=x; stk[++tp]=x;
while(!isroot(y)){stk[++tp]=fa[y],y=fa[y];}
while(tp){pushdown(stk[tp--]);}
while(!isroot(x))
{
y=fa[x];
if(isroot(y)) rot(x);
else if(idf(y)==idf(x)) rot(y),rot(x);
else rot(x),rot(x);
}
}
void access(int x){for(int y=;x;y=x,x=fa[x]) splay(x),ch[x][]=y,pushup(x);}
void mkroot(int x){access(x),splay(x),revers(x);}
void split(int x,int y){mkroot(x),access(y),splay(y);}
int fdroot(int x){access(x),splay(x);while(ch[x][])pushdown(ch[x][]),x=ch[x][];return x;}
void cut(int x,int y){split(x,y);fa[x]=ch[y][]=,pushup(y);}
void link(int x,int y){split(x,y);fa[x]=y;}
//int isconn(int x,int y){split(x,y);if(!ch[x][1]&&fa[x]=y&&ch[y][0]==x) return 1;}
void solve(int x,int y,int e)
{
split(x+m,y+m);
if(x==y){
int r1=s.root[e-],r2=s.root[e]=++s.tot;
s.sum[r2]=s.sum[r1],s.ls[r2]=s.ls[r1],s.rs[r2]=s.rs[r1];
}else if(fdroot(y+m)!=x+m){
s.update(,,m,s.root[e-],s.root[e],);
if(e<m) s.update(e+,,m,s.root[e-],s.root[e],-);
}else{
int id=mi[y+m],xx=E.x[id],yy=E.y[id];
cut(id,xx+m); cut(id,yy+m);
s.update(id+,,m,s.root[e-],s.root[e],);
if(e<m) s.update(e+,,m,s.root[e-],s.root[e],-);
}
link(e,x+m),link(e,y+m);
}
};
int tot; int main()
{
int i,j,x,y,Q,sx,sy,ans=; tot=n+m;
scanf("%d%d%d%d",&n,&m,&Q,&type);
for(j=;j<=m;j++) x=gint(), y=gint(), E.ae(x,y);
s.build(,m,s.root[]);
for(lct::mi[]=inf,i=;i<=n+m;i++) lct::mi[i]=i;
for(j=;j<=m;j++)
{
x=E.x[j]; y=E.y[j];
lct::solve(x,y,j);
}
for(j=;j<=Q;j++)
{
x=gint(), y=gint();
if(type) x^=ans,y^=ans;
sx=s.query(,x,,m,s.root[x-]);
sy=s.query(,x,,m,s.root[y]);
ans=n-(sy-sx);
printf("%d\n",ans);
}
return ;
}
BZOJ 3514 GERALD07加强版 (LCT+主席树)的更多相关文章
- BZOJ 3514: Codechef MARCH14 GERALD07加强版 [LCT 主席树 kruskal]
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1312 Solved: 501 ...
- BZOJ 3514: Codechef MARCH14 GERALD07加强版( LCT + 主席树 )
从左到右加边, 假如+的边e形成环, 那么记下这个环上最早加入的边_e, 当且仅当询问区间的左端点> _e加入的时间, e对答案有贡献(脑补一下). 然后一开始是N个连通块, 假如有x条边有贡献 ...
- [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 2177 Solved: 834 ...
- 【BZOJ3514】Codechef MARCH14 GERALD07加强版 LCT+主席树
题解: 还是比较简单的 首先我们的思路是 确定起点 然后之后贪心的选择边(也就是越靠前越希望选) 我们发现我们只需要将起点从后向前枚举 然后用lct维护连通性 因为强制在线,所以用主席树记录状态就可以 ...
- BZOJ3514:GERALD07加强版(LCT,主席树)
Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来 ...
- GERALD07加强版:lct,主席树,边化点
Description:N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. 传送门. lct这么神仙的东西一个题解都不写怎么行??? 神仙思路啊. 其实不是很难但是的确不容 ...
- bzoj 3514: GERALD07加强版 lct+可持久化线段树
题目大意: N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. 题解: 这道题考试的时候没想出来 于是便爆炸了 结果今天下午拿出昨天准备的题表准备做题的时候 题表里就有这题 ...
- BZOJ_3514_Codechef MARCH14 GERALD07加强版_主席树+LCT
BZOJ_3514_Codechef MARCH14 GERALD07加强版_主席树+LCT Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. I ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
随机推荐
- hadoop常见操作命令
1.查看指定文件夹下内容 hadoop dfs –ls [文件文件夹] eg: hadoop dfs –ls /user/wangkai.pt 2.打开某个已存在文件 hadoop dfs –cat ...
- linux下启动jekins报错
进入默认安装目录,执行java -jar jekins.war 启动使用端口号: java -jar jenkins.war --httpPort=8085,即启动完成 这个有个弊端,关闭后下次还得这 ...
- poj--1753--Flip Game(dfs好题)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37201 Accepted: 16201 Descr ...
- 比较两个map里的数据
template <class DataType>void ProcessMap(std::map<std::string, std::vector<DataType> ...
- PHP无限级分类实现(递归+非递归)
<?php /** * Created by PhpStorm. * User: qishou * Date: 15-8-2 * Time: 上午12:00 */ //准备数组,代替从数据库中检 ...
- 「LOJ10150」括号配对
[题目] Hecy 又接了个新任务:BE 处理.BE 中有一类被称为 GBE. 以下是 GBE 的定义: 空表达式是 GBE 如果表达式 A 是 GBE,则 [A] 与 (A) 都是 GBE 如果 A ...
- iOS 点击事件传递及响应
1.iOS中的事件 iOS中的事件可以分为3大类型: 触摸事件 加速计事件 远程控制事件这里我们只讨论iOS中的触摸事件. 1.1响应者对象(UIResponder) 在iOS中不是任何对象都能处理事 ...
- BZOJ 4698 差分+后缀数组
思路: 对所有序列差分一下 公共串的长度+1就是答案了 二分 扫一遍height即可,.. //By SiriusRen #include <cstdio> #include <cs ...
- SPOJ GSS1 & GSS3&挂了的GSS5
线段树然后yy一下,搞一搞. GSS1: 题意:求最大区间和. #include <cstdio> #include <algorithm> using namespace s ...
- centos 修改网卡信息命令
vi /etc/sysconfig/network-scripts/ifcfg-eth0