【P2147 [SDOI2008]洞穴勘測】離線+線段樹分治

P2147

離線真是太棒了 %%%

離線爆艹在線數據結構 -> LCT

過幾天補個LCT題解吧

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
    int date = 0,m = 1; char ch = 0;
    while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
    if(ch=='-'){m = -1; ch = getchar();}
    while(ch>='0' && ch<='9')
    {
        date = date*10+ch-'0';
        ch = getchar();
    }return date*m;
}
const int MAX_N = 10025;
const int MAX_M = 200025;
map<int ,int > edge[MAX_N];
struct node{int x,y;}CG[MAX_M],a[MAX_M],st[MAX_M],Q[MAX_M];
int top,fa[MAX_N],sz[MAX_N],vis[MAX_M],f[MAX_M];
int Find(int x)
{
    if(x==fa[x]) return fa[x];
    return x = Find(fa[x]);
}
void Merge(int x,int y)
{
    x = Find(x);y = Find(y);
    if(x!=y)
    {
        if(sz[x]>sz[y]) swap(x,y);
        st[++top] = (node){x,y};
        fa[x] = y;sz[y]+=sz[x];
    }
}
vector<int > vt[MAX_M<<2];
namespace sgt
{
    #define mid ((l+r)>>1)
    void update(int rt,int l,int r,int x,int y,int id)
    {
        if(x<=l&&r<=y)
        {
            vt[rt].push_back(id);
            return;
        }
        if(x>mid) update(rt<<1|1,mid+1,r,x,y,id);
        else if(y<=mid) update(rt<<1,l,mid,x,y,id);
        else update(rt<<1,l,mid,x,y,id),update(rt<<1|1,mid+1,r,x,y,id);
    }
    #undef mid
}
void del(int now)
{
    while(top>now)
    {
        fa[st[top].x] = st[top].x;
        sz[st[top].y] -= sz[st[top].x];
        top--;
    }
}
void dfs(int rt,int l,int r)
{
    int now = top;
    for(int i = 0;i<vt[rt].size();++i)
        Merge(CG[vt[rt][i]].x,CG[vt[rt][i]].y);
    if(l==r)
    {
        if(vis[l]) if(Find(Q[l].x)==Find(Q[l].y)) printf("Yes\n");
        else printf("No\n");
    }
    else
    {
        int mid = (l+r)>>1;
        dfs(rt<<1,l,mid);
        dfs(rt<<1|1,mid+1,r);
    }
    del(now);
}
/*int root[MAX_N],cnt,sz;
namespace hjt
{
    #define mid ((l+r)>>1)
    struct node{int l,r,maxx;}T[MAX_N*40];

    #undef mid
}*/

int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m,x,y,cnt1=0,cnt2=0;
    scanf("%d%d",&n,&m);
    char opt[10];
    for(int i = 1;i<=n;++i) fa[i] = i,sz[i] = 1;
    for(int i = 1;i<=m;++i)
    {
        scanf("%s%d%d",&opt,&x,&y);
        if(x>y) swap(x,y);
        if(opt[0]=='C')
        {
            if(f[edge[x][y]]) continue;
            edge[x][y] = ++cnt1;
            a[cnt1] = (node){x,y};
            f[edge[x][y]] = i;
        }
        else if(opt[0]=='D')
        {
            CG[++cnt2] = (node){x,y};
            sgt::update(1,1,m,f[edge[x][y]],i,cnt2);
            f[edge[x][y]] = 0;
        }
        else Q[i] = (node){x,y},vis[i] = true;
    }
    for(int i = 1;i<=cnt1;++i)
    {
        if(f[i])
        {
            CG[++cnt2] = a[i];
            sgt::update(1,1,m,f[i],m,cnt2);
        }
    }
    dfs(1,1,m);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章