jzoj3959 [GDOI2015模拟12.21]鸡腿の乒乓 线段树+并查集

Description


【故事の背景】

鸡腿是CZYZ的著名DS,他为了树立高富帅的伟大形象决定暑假去张江大学学习(游玩)。在呆了一段时间之后,鸡腿突然发现,张江的妹子怎么这么少啊这么少啊这么少啊。为了勾搭妹子,鸡腿决定去玩乒乓游戏。

【问题の描述】

乒乓游戏可不是乒乓!乒乓好像也和这个游戏没啥关系。这个游戏的主角就是——区间。对于两个区间,如果(a,b)和(c,d)区间满足c

Solution


考虑朴素(暴力)做法,对于相交的区间我们并查集连边
注意到线段树是个好东西,我们把区间扔进线段树上对应节点的vector里面,每次查询就logn合并即可

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int INF=1e9;
const int N=100005;

struct treeNode {int l,r;} t[N*58];

std:: vector <int> vec[N*58];

int fa[N],l[N],r[N],p[N],q[N],tot,root;

int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

int get_father(int x) {
    if (!fa[x]) return x;
    return fa[x]=get_father(fa[x]);
}

void merge(int x,int y) {
    x=get_father(x),y=get_father(y);
    if (x==y) return ;
    fa[x]=y;
    l[y]=std:: min(l[y],l[ x]);
    r[y]=std:: max(r[y],r[x]);
}

void ins(int &now,int tl,int tr,int l,int r,int x) {
    if (!now) now=++tot;
    if (tl==l&&tr==r) {
        vec[now].push_back(x);
        return ;
    }
    int mid=(tl+tr)>>1;
    if (r<=mid) ins(t[now].l,tl,mid,l,r,x);
    else if (l>mid) ins(t[now].r,mid+1,tr,l,r,x);
    else {
        ins(t[now].l,tl,mid,l,mid,x);
        ins(t[now].r,mid+1,tr,mid+1,r,x);
    }
}

void work(int now,int tl,int tr,int x,int id) {
    if (!now) now=++tot;
    if (vec[now].size()) {
        for (int i=0;i<vec[now].size();i++) {
            merge(vec[now][i],id);
        }
        vec[now].clear(); vec[now].push_back(id);
    }
    if (tl==tr) return ;
    int mid=(tl+tr)>>1;
    if (x<=mid) work(t[now].l,tl,mid,x,id);
    else work(t[now].r,mid+1,tr,x,id);
}

int main(void) {
    for (int T=read(),cnt=0;T--;) {
        int opt=read(),a=read(),b=read();
        if (opt==1) {
            l[++cnt]=a; r[cnt]=b;
            p[cnt]=a; q[cnt]=b;
            work(root,-INF,INF,a,cnt);
            work(root,-INF,INF,b,cnt);
            ins(root,-INF,INF,a+1,b-1,cnt);
        } else {
            int fb=get_father(b);
            if (get_father(a)==fb) puts("YES");
            else {
                if (p[a]>=l[fb]&&p[a]<=r[fb]||q[a]>=l[fb]&&q[a]<=r[fb]) puts("YES");
                else puts("NO");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jpwang8/article/details/81041042