Training master segment tree T3 P1753: Interval Connectivity

Segment tree / permanent marker

Complexity \ (O (nlogn) \)

Wrong solution to a problem, not all all sections [st] and the segment tree node [l, r] to be stored in vector intersecting

Inserting interval [s, t], we cover the complete length of a section, FIG.

Substantially covered with a layer surface, i.e. less than pass mark

So when we merge vector to consider each layer

When inserted, if exactly two contact sections, also regarded reach each other, so when combined with (s, t) combined with insertion [s + 1, t-1] is inserted

Since the interval inserted in length from small to large, so there is no such a case:

It must merge interval pairwise reachable with a disjoint-set to maintain this relationship

Finally, if the segment inclusion relationship, then A can reach B, B can not reach A (overlapped end endpoint can be considered), the Japanese sentence can, since the endpoints may coincide, it is determined not just one endpoint

The code is very simple:

#include<bits/stdc++.h>
using namespace std;

#define go(i,a,b) for(int i=a;i<=b;++i)
#define com(i,a,b) for(int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define ls rt<<1
#define rs rt<<1|1

const int inf=0x3f3f3f3f,N=2e5+10;

int n,m,op[N],a[N],b[N],c[N],d[N],f[N],rk[N],id,tot;
vector<int>inter[N<<2];

void read(int &x){
    x=0;char c=getchar(),f=1;
    while(!isdigit(c)){ if(c=='-') f=-1; c=getchar(); }
    while(isdigit(c)){ x=x*10+c-'0'; c=getchar(); }
    x*=f;
}
inline int find(int x){ return f[x]=f[x]==x?x:find(f[x]); }

void merge(int rt,int l,int r,int k){
    for(int i=0;i<inter[rt].size();++i){
        int x=inter[rt][i];
        x=find(x);
        f[x]=id;
        c[id]=min(c[id],c[x]);
        d[id]=max(d[id],d[x]);
    }
    inter[rt].clear();
    if(l==r) return;
    int mid=l+r>>1;
    if(k<=mid) merge(ls,l,mid,k);
    else merge(rs,mid+1,r,k);
}

void insert(int rt,int l,int r,int x,int y,int k){
    if(l>=x&&r<=y){
        inter[rt].push_back(k);
        return; 
    }
    int mid=l+r>>1;
    if(x<=mid) insert(ls,l,mid,x,y,k);
    if(y>mid) insert(rs,mid+1,r,x,y,k);
}

int main(){
    //freopen("input.txt","r",stdin);
    read(n);
    go(i,1,n){
        read(op[i]),read(a[i]),read(b[i]);
        if(op[i]==1) rk[++tot]=a[i],rk[++tot]=b[i];
    }
    sort(rk+1,rk+tot+1);
    tot=unique(rk+1,rk+tot+1)-rk-1;
    go(i,1,n) if(op[i]==1){
        a[i]=lower_bound(rk+1,rk+tot+1,a[i])-rk;
        b[i]=lower_bound(rk+1,rk+tot+1,b[i])-rk;
    }
    int x,y;
    go(i,1,n){
        if(op[i]==1){
            f[++id]=id,c[id]=a[i],d[id]=b[i];
            merge(1,1,tot,a[i]);
            merge(1,1,tot,b[i]);
            insert(1,1,tot,c[id]+1,d[id]-1,f[id]);
        }
        else{
            x=find(a[i]),y=find(b[i]);
            if(x==y) puts("YES");
            else{
                if(c[x]>c[y]&&c[x]<d[y]||d[x]>c[y]&&d[x]<d[y]) puts("YES");
                else puts("NO");
            }
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/White-star/p/11583828.html