题面

考虑没有询问,直接给你一个图问联通块怎么做。

并查集是吧。

现在想要动态地做,那么应该要用LCT。

考虑新加进来一条边,想要让它能够减少一个联通块的条件就是现在边的两个端点还没有联通。

如果联通了,应该会形成一个环,我们其实可以把环中最早加进来的边删掉再加进来这条边,也不影响整个的联通性对不对。

于是我们用LCT维护一下最大生成树,顺便求出一个\(pre[i]\)表示\(i\)这条边加进来以后,环里面最早加进来的边的编号。

可以发现\(pre[i]\leq l\)那就说明,\(i\)这条边是连接了联通块的。\(l..r\)的区间内,有一个这样的边,就会少一个联通块。

这个用主席树维护就好了。

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define lc t[o].c[0]
#define rc t[o].c[1]
#define REP(i,a,n) for(register int i(a);i<=(n);++i)
#define dbg(...) fprintf(stderr,__VA_ARGS__)
const int SZ=(1<<21)+1;char ibuf[SZ],*iS,*iT,obuf[SZ+128],*oS=obuf,*oT=obuf+SZ-1;
#ifdef ONLINE_JUDGE
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,SZ,stdin),(iS==iT?EOF:*iS++)):*iS++)
#else
#define gc() getchar()
#endif
template<typename I>inline void read(I&x){char c=gc();int f=1;for(;c<'0'||c>'9';c=gc())c=='-'?f=-1:0;for(x=0;c>='0'&&c<='9';c=gc())x=(x<<1)+(x<<3)+(c&15);f==-1?x=-x:0;}
inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;}
#define printf(...) oS>oT&&(flush(),1),oS+=sprintf(oS,__VA_ARGS__)
template<typename A,typename B>inline char SMAX(A&a,const B&b){return a<b?a=b,1:0;}
template<typename A,typename B>inline char SMIN(A&a,const B&b){return a>b?a=b,1:0;} const int N=200000+7,INF=0x3f3f3f3f;
int n,m,x,y,Q,tp,la;
struct Edges{int x,y;}e[N]; struct LCT{
struct Node{int min,c[2],fa,rev;inline Node():min(INF){}}t[N<<1];int S[N<<1],top,nod;inline Node&operator[](const int&x){return t[x];}
inline char Isroot(int o){return t[t[o].fa].c[0]!=o&&t[t[o].fa].c[1]!=o;}
inline char Identify(int o){return t[t[o].fa].c[0]!=o;}
inline void Connect(int fa,int o,char d){t[fa].c[d]=o;t[o].fa=fa;}
inline void Pushup(int o){t[o].min=o>n?o:INF;lc&&SMIN(t[o].min,t[lc].min);rc&&SMIN(t[o].min,t[rc].min);}
inline void Rotate(int o){int fa=t[o].fa,pa=t[fa].fa,d1=Identify(o),d2=Identify(fa),b=t[o].c[d1^1];!Isroot(fa)&&(t[pa].c[d2]=o);t[o].fa=pa;Connect(fa,b,d1);Connect(o,fa,d1^1);Pushup(fa),Pushup(o);}
inline void Pushdown(int o){t[o].rev&&(t[o].rev=0,lc&&(t[lc].rev^=1,std::swap(t[lc].c[0],t[lc].c[1]),1),rc&&(t[rc].rev^=1,std::swap(t[rc].c[0],t[rc].c[1]),1));}
inline void Splay(int o){
int x=o;S[top=1]=x;while(!Isroot(x))S[++top]=x=t[x].fa;while(top)Pushdown(S[top--]);
while(!Isroot(o)){int fa=t[o].fa;if(Isroot(fa))Rotate(o);else if(Identify(o)==Identify(fa))Rotate(fa),Rotate(o);else Rotate(o),Rotate(o);}
}
inline void Access(int o){for(register int x=0;o;o=t[x=o].fa)Splay(o),rc=x,Pushup(o);}
inline void Makeroot(int o){Access(o);Splay(o);t[o].rev^=1;std::swap(lc,rc);}
inline int Findroot(int o){Access(o);Splay(o);while(lc)Pushdown(o),o=lc;Splay(o);return o;}
inline void Split(int x,int y){Makeroot(x);Access(y);Splay(y);}
inline void Link(int x,int y){Makeroot(x);if(Findroot(y)^x)t[x].fa=y;}
inline void Cut(int x,int y){Split(x,y);if(t[y].c[0]&&!t[x].c[1])t[y].c[0]=t[x].fa=0;Pushup(y);}//错误笔记: 清错了,把t[x].c[0]清零了
}lct;
#undef lc
#undef rc
struct President_Tree{
struct Node{int lc,rc,val;}t[N*21];int rt[N],nod;
inline void Insert(int &o,int p,int L,int R,int x,int k){t[o=++nod]=t[p];t[o].val+=k;if(L==R)return;int M=(L+R)>>1;x<=M?Insert(t[o].lc,t[p].lc,L,M,x,k):Insert(t[o].rc,t[p].rc,M+1,R,x,k);}
inline int Query(int o,int p,int L,int R,int x){if(L==R)return t[o].val-t[p].val;int M=(L+R)>>1;return x>M?t[t[o].lc].val-t[t[p].lc].val+Query(t[o].rc,t[p].rc,M+1,R,x):Query(t[o].lc,t[p].lc,L,M,x);}
}pt; int main(){
read(n),read(m),read(Q),read(tp);
REP(i,1,m){
read(x),read(y);int pre=x==y?m:0,p;e[i]=Edges{x,y};//错误笔记:自环需要特判掉
if(x!=y&&lct.Findroot(x)==lct.Findroot(y))lct.Split(x,y),p=lct[y].min,pre=p-n,lct.Cut(p,e[p-n].x),lct.Cut(p,e[p-n].y);
if(x!=y)lct[i+n].min=i+n,lct.Link(x,i+n),lct.Link(y,i+n);
pt.Insert(pt.rt[i],pt.rt[i-1],0,m,pre,1);//错误笔记:注意边权和点权之间要用+-n来切换
}
REP(i,1,Q){
read(x),read(y);if(tp)x^=la,y^=la;
printf("%d\n",la=n-pt.Query(pt.rt[y],pt.rt[x-1],0,m,x-1));//错误笔记:漏强制在线
}return flush(),0;
}

BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT维护最大生成树 主席树的更多相关文章

  1. BZOJ 3514: Codechef MARCH14 GERALD07加强版 (LCT维护最大生成树+主席树)

    题意 给出nnn个点,mmm条边.多次询问,求编号在[l,r][l,r][l,r]内的边形成的联通块的数量,强制在线. 分析 LCTLCTLCT维护动态最大生成树,先将每条边依次加进去,若形成环就断掉 ...

  2. [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2177  Solved: 834 ...

  3. bzoj3514 Codechef MARCH14 GERALD07加强版 lct预处理+主席树

    Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1951  Solved: 746[Submi ...

  4. BZOJ3514: Codechef MARCH14 GERALD07加强版(LCT,主席树)

    Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密.接下来M ...

  5. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3514 题意概括 N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. N ...

  6. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT+可持久化线段树

    自己独自想出来并切掉还是很开心的~ Code: #include <bits/stdc++.h> #define N 400005 #define inf 1000000000 #defi ...

  7. 【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2023  Solved: 778 ...

  8. BZOJ 3514: Codechef MARCH14 GERALD07加强版( LCT + 主席树 )

    从左到右加边, 假如+的边e形成环, 那么记下这个环上最早加入的边_e, 当且仅当询问区间的左端点> _e加入的时间, e对答案有贡献(脑补一下). 然后一开始是N个连通块, 假如有x条边有贡献 ...

  9. BZOJ 3514: Codechef MARCH14 GERALD07加强版 [LCT 主席树 kruskal]

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1312  Solved: 501 ...

随机推荐

  1. 【HDOJ6665】Calabash and Landlord(dfs)

    题意:二维平面上有两个框,问平面被分成了几个部分 x,y<=1e9 思路:分类讨论可以 但数据范围实在太小了,离散化以后随便dfs一下 #include<bits/stdc++.h> ...

  2. 20180813-Java 重写(Override)与重载(Overload)

    Java 重写(Override)与重载(Overload) class Animal{ public void move(){ System.out.println("动物可以移动&quo ...

  3. 170820-关于JSP页面的知识点

    1.JSP [1] 简介 > HTML - HTML擅长显示一个静态的网页,但是不能调用Java程序. > Servlet - Servlet擅长调用Java程序和后台进行交互,但是它不擅 ...

  4. elk+kafka+zookeeper+filebeat安装

    ElasticSearch6.0 ElasticSearch6.0安装 #依赖jdk8 rpm -ivh elasticsearch-.rpm vim /etc/elasticsearch/elast ...

  5. Spring MVC--------处理方法返回值的可选类型

    对于Spring MVC处理方法支持支持一系列的返回方式:  (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)String (7)Void ...

  6. Linux┊详解udev

    是一小部分例子,如果你的系统采用了udev方式,那你应该可以看到更多的规则.如果你想修改设备的权限或者创建信的符号连接,那么你需要熟读这些规则,特别是要仔细注意你修改的那些与之相关的设备. 修改你的u ...

  7. MVC终极解释

    之前校招笔面试老师被问起MVC,虽然都知道怎么回事.但每次组织语言总觉得答得的简洁明了和突出重点.下面是我总结,希望未来找工作的学弟学妹们能不在受此大路边上的问题困扰. M-V-C 即Model-Vi ...

  8. python读写excle

    我们可以通过python的一些模块进行excle中用例的读取,或者导出数据到excle 目录 1.安装模块 2.读excle 3.写excle 1.安装模块 python中有第三方模块可以进行excl ...

  9. Entity Framework Code First数据库连接 转载 https://www.cnblogs.com/libingql/p/3351275.html

    Entity Framework Code First数据库连接   1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器 ...

  10. FTP 服务器搭建(基于 CentOS 7)

    参考资料: 檔案伺服器之三: FTP 伺服器 用 vsftpd 配置FTP服务器 vsftpd 的所有选项 注意,如果要所有人同时编辑 FTP 上的所有文件,可以将 vsftpd.conf 配置文件中 ...