【并查集】Codeforces 566D Restructuring Company

来源:互联网 发布:知乎 真名 编辑:程序博客网 时间:2024/06/05 21:02

题面在这里

对于本题,只需要再维护一个并查集表示i所在联通块的最右位置

因为相邻两个元素之间的关系至多被处理1次

所以均摊是O(qα(n))

示例程序:

#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;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 字花 宋字花式图片 字花36种动物详细资料 三十六字花谜语破解软件 三十六动物字花手册 字花题目及答案 100万存银行一年利息多少 10万存一年利息多少 2019存30万存三年利息多少 50万存银行一年多少利息 1000万存银行一年利息多少 200万存银行一年利息多少 10万存银行好还是余额宝好 十万存银行一年多少利息 一万存一年利息多少 五十万存银行一年有多少利息 40万存银行一年利息多少 20万存定期三年利息 二十万存定期一年利息多少 20万存银行一年利息是多少 10万存3年定期利息是多少 100万存银行一个月利息多少 20万存余额宝一天收益多少 十万存一年定期利息是多少 十万存定期一年利息多少 10万存银行一年利息多少 一万存三个月利息多少 15万存银行一年利息多少 十万存余额宝一年收益多少 5万存银行一年利息多少 2018存款20万存3年利息会有多少 大额存单20万存一年有多少钱 10万存定期还是买理财 3万存银行一年利息多少 800万存银行一年利息多少 2万存银行一年利息多少 1000万存银行一年多少利息 一万存余额宝一天利息 三十万存银行一年利息多少 2018年20万存3年定期利息多少钱 5万存余额宝一天多少钱