【刷题】BZOJ 3514 Codechef MARCH14 GERALD07加强版
Description
N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数。
Input
第一行四个整数N、M、K、type,代表点数、边数、询问数以及询问是否加密。
接下来M行,代表图中的每条边。
接下来K行,每行两个整数L、R代表一组询问。对于type=0的测试点,读入的L和R即为询问的L、R;对于type=1的测试点,每组询问的L、R应为L xor lastans和R xor lastans。
Output
K行每行一个整数代表该组询问的联通块个数。
Sample Input
3 5 4 0
1 3
1 2
2 1
3 2
2 2
2 3
1 5
5 5
1 2
Sample Output
2
1
3
1
HINT
对于100%的数据,1≤N、M、K≤200,000。
Solution
题目很神,还是太弱了,想不出来。LCT加主席树
答案的求法:依次考虑每一条边,如果加入这条边 \(i\) 会生成环,那就删除这个环里最早加入的边 \(j\) ,并且记录下来 \(fout[i]=j\),代表 \(i\) 的加入弹掉了 \(j\) 号边
然后发现对于一个询问 \(l,r\),就看在 \(l\) 到 \(r\) 之间有多少条边 \(i\) ,\(fout[i]<l\),然后用 \(n\) 减掉这个数,就是一次询问的答案
对于方法的解释就是,如果 \(i\) 边的 \(fout\) 小于 \(l\) ,那么如果只存在 \(l\) 到 \(r\) 的边话,\(i\) 边必定会连接上两个联通块,答案就要-1;反之,如果它的 \(fout\) 大于等于 \(l\) ,那么这条边连的是一个联通块里的两个点,不会对答案产生贡献
然后维护 \(fout\) 数组,可以用LCT维护生成树的方法
查询 \(l\) 到 \(r\) 之间有多少 \(fout\) 小于 \(l\) 的,可以用主席树维护
很巧妙,要加强思维啊
#include<bits/stdc++.h>
#define ll long long
#define db double
#define ld long double
const int MAXN=200000+10,MAXM=200000+10,inf=0x3f3f3f3f;
int n,m,k,type,fa[MAXN],fout[MAXM],ans;
struct edge{
int u,v;
};
edge side[MAXM];
#define lc(x) ch[(x)][0]
#define rc(x) ch[(x)][1]
struct LCT{
int ch[MAXN+MAXM][2],fa[MAXN+MAXM],rev[MAXN+MAXM],id[MAXN+MAXM],stack[MAXN+MAXM],cnt,Mn[MAXN+MAXM],val[MAXN+MAXM];
inline void init()
{
memset(Mn,inf,sizeof(Mn));
memset(val,inf,sizeof(val));
}
inline bool nroot(int x)
{
return lc(fa[x])==x||rc(fa[x])==x;
}
inline void reverse(int x)
{
std::swap(lc(x),rc(x));
rev[x]^=1;
}
inline void pushup(int x)
{
Mn[x]=val[x],id[x]=x;
if(Mn[lc(x)]<Mn[x])Mn[x]=Mn[lc(x)],id[x]=id[lc(x)];
if(Mn[rc(x)]<Mn[x])Mn[x]=Mn[rc(x)],id[x]=id[rc(x)];
}
inline void pushdown(int x)
{
if(rev[x])
{
if(lc(x))reverse(lc(x));
if(rc(x))reverse(rc(x));
rev[x]=0;
}
}
inline void rotate(int x)
{
int f=fa[x],p=fa[f],c=(rc(f)==x);
if(nroot(f))ch[p][rc(p)==f]=x;
fa[ch[f][c]=ch[x][c^1]]=f;
fa[ch[x][c^1]=f]=x;
fa[x]=p;
pushup(f);
pushup(x);
}
inline void splay(int x)
{
cnt=0;
stack[++cnt]=x;
for(register int i=x;nroot(i);i=fa[i])stack[++cnt]=fa[i];
while(cnt)pushdown(stack[cnt--]);
for(register int y=fa[x];nroot(x);rotate(x),y=fa[x])
if(nroot(y))rotate((lc(y)==x)==(lc(fa[y])==y)?y:x);
pushup(x);
}
inline void access(int x)
{
for(register int y=0;x;x=fa[y=x])splay(x),rc(x)=y,pushup(x);
}
inline int findroot(int x)
{
access(x);splay(x);
while(lc(x))pushdown(x),x=lc(x);
splay(x);
return x;
}
inline void makeroot(int x)
{
access(x);splay(x);reverse(x);
}
inline void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
inline void link(int x,int y)
{
makeroot(x);fa[x]=y;
}
inline void cut(int x,int y)
{
split(x,y);fa[x]=lc(y)=0;pushup(y);
}
};
LCT T1;
#undef lc
#undef rc
#define Mid ((l+r)>>1)
#define lson l,Mid
#define rson Mid+1,r
struct ChairMan_Tree{
int sum[MAXN<<5],lc[MAXN<<5],rc[MAXN<<5],root[MAXN],cnt;
inline void Build(int &rt,int l,int r)
{
rt=++cnt;
sum[rt]=0;
if(l==r)return ;
Build(lc[rt],lson);
Build(rc[rt],rson);
}
inline void Insert(int &rt,int l,int r,int last,int pos)
{
rt=++cnt;
lc[rt]=lc[last];
rc[rt]=rc[last];
sum[rt]=sum[last]+1;
if(l==r)return ;
else
{
if(pos<=Mid)Insert(lc[rt],lson,lc[last],pos);
else Insert(rc[rt],rson,rc[last],pos);
}
}
inline int Query(int now,int last,int l,int r,int k)
{
if(r==k)return sum[now]-sum[last];
else
{
if(k<=Mid)return Query(lc[now],lc[last],lson,k);
else return sum[lc[now]]-sum[lc[last]]+Query(rc[now],rc[last],rson,k);
}
}
};
ChairMan_Tree T2;
#undef Mid
#undef lson
#undef rson
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char c='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(c!='\0')putchar(c);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline int found(int x)
{
if(fa[x]!=x)fa[x]=found(fa[x]);
return fa[x];
}
int main()
{
read(n);read(m);read(k);read(type);
T1.init();
for(register int i=1;i<=n;++i)fa[i]=i;
for(register int i=1;i<=m;++i)
{
int u,v,sn=i+n,x,y;
read(u);read(v);
side[i].u=u;side[i].v=v;
if(u==v)
{
fout[i]=i;
continue;
}
x=found(u),y=found(v);
if(x!=y)
{
fa[x]=y;
T1.val[sn]=i;
T1.link(sn,u);T1.link(sn,v);
}
else
{
T1.split(u,v);
int so=T1.id[v];
fout[i]=so-n;
T1.cut(so,side[so-n].u);T1.cut(so,side[so-n].v);
T1.val[sn]=i;
T1.link(sn,u);T1.link(sn,v);
}
}
T2.Build(T2.root[0],0,m);
for(register int i=1;i<=m;++i)T2.Insert(T2.root[i],0,m,T2.root[i-1],fout[i]);
while(k--)
{
int l,r;
read(l);read(r);
if(type)l^=ans,r^=ans;
ans=n-T2.Query(T2.root[r],T2.root[l-1],0,m,l-1);
write(ans,'\n');
}
return 0;
}
【刷题】BZOJ 3514 Codechef MARCH14 GERALD07加强版的更多相关文章
- BZOJ 3514: Codechef MARCH14 GERALD07加强版( LCT + 主席树 )
从左到右加边, 假如+的边e形成环, 那么记下这个环上最早加入的边_e, 当且仅当询问区间的左端点> _e加入的时间, e对答案有贡献(脑补一下). 然后一开始是N个连通块, 假如有x条边有贡献 ...
- 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加强版 (CHEF AND GRAPH QUERIES)
[BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...
- BZOJ 3514: Codechef MARCH14 GERALD07加强版(LCT + 主席树)
题意 \(N\) 个点 \(M\) 条边的无向图,询问保留图中编号在 \([l,r]\) 的边的时候图中的联通块个数. \(K\) 次询问强制在线. \(1\le N,M,K \le 200,000\ ...
- BZOJ 3514 Codechef MARCH14 GERALD07加强版
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3514 题意:给出一个图m条边.每次询问只加入编号在区间[L,R]之内的边有多少连通 ...
- BZOJ 3514 Codechef MARCH14 GERALD07加强版 Link-Cut-Tree+划分树
题目大意: 给定n个点m条边的无向图.求问当图中仅仅有[编号在[l,r]区间内]的边存在时图中的联通块个数 强制在线 注意联通块是指联通了就是同一块,不是Tarjan求的那种块 看到这题的那一刻我就想 ...
- BZOJ 3514: Codechef MARCH14 GERALD07加强版 (LCT维护最大生成树+主席树)
题意 给出nnn个点,mmm条边.多次询问,求编号在[l,r][l,r][l,r]内的边形成的联通块的数量,强制在线. 分析 LCTLCTLCT维护动态最大生成树,先将每条边依次加进去,若形成环就断掉 ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- 【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 2023 Solved: 778 ...
随机推荐
- electron快速开始
初学electron 接触了两周的electron,感觉还不错,以后pc端基本上可以用electron加壳写pc端应用了,可以用nodejs的模块,也可以用es6.7,还可以直接操作系统文件.基本上可 ...
- 《Redis设计与实现》阅读笔记(三)--链表
链表 定义 链表分为两部分,链表节点和持有链表的list结构. 每个链表节点包含前置节点指针,后置节点指针,节点值void*用于保存各种不同类型的值 list结构包含表头节点指针,表尾节点指针,节点数 ...
- Netty源码分析第2章(NioEventLoop)---->第4节: NioEventLoop线程的启动
Netty源码分析第二章: NioEventLoop 第四节: NioEventLoop线程的启动 之前的小节我们学习了NioEventLoop的创建以及线程分配器的初始化, 那么NioEvent ...
- Oracle之带参存储过程(存储过程中for循环调用存储过程)
--带参存储过程create or replace procedure testdate(v in number) is i number; begin i:=v; insert into test_ ...
- 如何配置php客户端(phpredis)并连接Redis--华为DCS for Redis使用经验系列
使用php连接Redis.Memcache等都需要进行扩展,以CentOS为例,介绍phpredis的客户端环境搭建. 第0步:准备工作 华为云上购买1台弹性云服务器ECS(我选了CentOS 6.3 ...
- Codeforces Round #524 (Div. 2) C. Masha and two friends(矩形相交)
C. Masha and two friends time limit per test 1 second memory limit per test 256 megabytes input stan ...
- [持久更新] 剑指offer题目Python做题记录
第一题 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路:先快速定位到 ...
- 阿里nas挂载错误
报错如下,解决:yum install nfs-utils 即可 mount: wrong fs type, bad option, bad superblock on 12080482f3-qra4 ...
- centos安装eclise启动报错
A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be avail http://blog.csdn.net/u0 ...
- Percona XtraDB Cluster 5.7
附加:相关在线文档https://www.percona.com/software/documentation 安装要求: 1.root权限2.保证开放3306.4444.4567.4568端口3.关 ...