【并查集】Codeforces 566D Restructuring Company
来源:互联网 发布:知乎 真名 编辑:程序博客网 时间:2024/06/05 21:02
题面在这里
对于本题,只需要再维护一个并查集表示i所在联通块的最右位置
因为相邻两个元素之间的关系至多被处理1次
所以均摊是
示例程序:
#include<cstdio>#define pc putcharinline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline int red(){ int res=0,f=1;char ch=nc(); while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();} while ('0'<=ch&&ch<='9') res=res*10+ch-48,ch=nc(); return res*f;}const int maxn=200005;int n,q,fa[maxn],nxt[maxn];int getfa(int x){ return fa[x]==x?x:fa[x]=getfa(fa[x]);}int getnxt(int x){ return nxt[x]==x?x:nxt[x]=getnxt(nxt[x]);}int main(){ n=red(),q=red(); for (int i=1;i<=n;i++) fa[i]=nxt[i]=i; while (q--){ int c=red(),x=red(),y=red(); if (c==1) fa[getfa(x)]=getfa(y); else if (c==2){ for (int j=getnxt(x);j<y;j=getnxt(j+1)) fa[getfa(j)]=getfa(y),nxt[getnxt(j)]=getnxt(j+1); }else if (getfa(x)==getfa(y)) pc('Y'),pc('E'),pc('S'),pc('\n');else pc('N'),pc('O'),pc('\n'); } return 0;}
阅读全文
1 0
- CodeForces 566 D.Restructuring Company(并查集)
- CodeForces 566 D.Restructuring Company(并查集)
- 【并查集】Codeforces 566D Restructuring Company
- Codeforces 566D Restructuring Company
- CodeForces 566D Restructuring Company
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 stl应用
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 set 二分
- codeforces Restructuring Company
- Restructuring Company CodeForces
- Restructuring Company
- Restructuring Company
- Codeforces 366D 贪心+并查集
- Codeforces 371D. Vessels【并查集】
- codeforces 366D 并查集
- codeforces 593D(并查集 + LCA)
- codeforces 691D 并查集
- [Codeforces 325 D. Reclamation]并查集
- Codeforces 766D 并查集
- 使用Lock来实现生产者和消费者问题
- 事件是如何工作的?
- K-近邻算法(KNN)
- git服务器gitlab之搭建和使用
- Mybatis中#{}和${}传参的区别及#和$的区别小结
- 【并查集】Codeforces 566D Restructuring Company
- 数组排序(交换法与选择法)
- java虚拟机架构
- HDU4287 Intelligent IME(Trie树,map)
- 连接数据库SQL并验证账号密码(2)
- 前后端数据对接
- 学生信息管理系统优化
- C++风格_规则特例
- 程序流程控制