题面

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

并查集是吧。

现在想要动态地做,那么应该要用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. windows系统如何查看物理cpu核数,内存型号等

    首先,我们需要打开命令行模式,利用win+r键打开运行,输入cmd回车即会出现   然后在命令行界面输入wmic进入命令行系统管理执行脚本界面     然后我们通过cpu get *可以查看cpu的具 ...

  2. hdu 4641K-string SAM的O(n^2)算法 以及 SAM+并查集优化

    转载:http://www.cnblogs.com/hxer/p/5675149.html 题意:有一个长度为n(n < 5e4)的字符串,Q(Q<=2e5)次操作:操作分为:在末尾插入一 ...

  3. 【2019 Multi-University Training Contest 6】

    01: 02:https://www.cnblogs.com/myx12345/p/11650764.html 03: 04: 05:https://www.cnblogs.com/myx12345/ ...

  4. JavaScript点击事件——美女合集

    Js点击事件--美女合集 实例 效果如下图: 代码如下: <!DOCTYPE html> <html lang="en"> <head> < ...

  5. 牛客网暑期ACM多校训练营(第五场) F - take —— 期望+树状数组+逆元

    看到一篇好的博客特意转出来观摩大佬:转:https://blog.csdn.net/greybtfly/article/details/81413526 题目大意:给n个箱子排成一排,从头到尾按顺序依 ...

  6. Your first HTML form

    The first article in our series provides your very first experience of creating an HTML form, includ ...

  7. 牛客提高D4t2 卖羊驼

    分析 不难想到dp[i][j]表示前i个数分了j组的最大值 我们发现这个dp状态有决策单调性 g[i][j]表示对于第i个数它的第j位最近出现的位置 每次一定从这些点转移 预处理即可 似乎还可以做到1 ...

  8. Android O编译前修改文件和目录权限

    当需要修改某文件或路径权限时,我们可以在init.rc开机启动某节点添加chmod命令进行修改.但是对于system分区,由于是ro权限,在init.rc使用chmod修改权限无效.需要在文件编译时, ...

  9. day33—前端开发的模块化和组件化

    转行学开发,代码100天——2018-04-18 今天是记录前端开发中模块化.组件化的知识.关于何为模块化,何为组件化以及为何要如此,目前还是处于一个只可意会不可言传的理解应用阶段. 当然,这样的存在 ...

  10. 003-notepad++插件

    1.下载 https://github.com/bruderstein/nppPluginManager/releases 下载最新的PluginManager_vXXXX_UNI.zip 解压,将里 ...