其他分享
首页 > 其他分享> > [BZOJ4025]二分图(线段树分治+并查集)

[BZOJ4025]二分图(线段树分治+并查集)

作者:互联网

Solution

Code

#include <bits/stdc++.h>

using namespace std;

#define p2 p << 1
#define p3 p << 1 | 1

template <class t>
inline void read(t & res)
{
    char ch;
    while (ch = getchar(), !isdigit(ch));
    res = ch ^ 48;
    while (ch = getchar(), isdigit(ch))
    res = res * 10 + (ch ^ 48); 
}

const int e = 2e6 + 5;
struct point
{
    int x, y;
}a[e];
vector<int>g[e];
int stk[e], top, n, m, q, f[e], sze[e];
bool ans[e], pd;

inline int find(int x)
{
    return !f[x] ? x : find(f[x]);
}

inline void merge(int x, int y)
{
    stk[++top] = 0;
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (sze[x] > sze[y]) swap(x, y);
    sze[y] += sze[x];
    f[x] = y;
    stk[top] = x;
}

inline void del()
{
    int x = stk[top--];
    if (!x) return;
    sze[f[x]] -= sze[x];
    f[x] = 0; 
}

inline void modify(int l, int r, int s, int t, int v, int p)
{
    if (l == s && r == t)
    {
        g[p].push_back(v);
        return;
    }
    int mid = l + r >> 1;
    if (t <= mid) modify(l, mid, s, t, v, p2);
    else if (s > mid) modify(mid + 1, r, s, t, v, p3);
    else
    {
        modify(l, mid, s, mid, v, p2);
        modify(mid + 1, r, mid + 1, t, v, p3);
    }
}

inline void dfs(int l, int r, int p, int opt)
{
    int i, len = g[p].size();
    for (i = 0; i < len; i++)
    {
        int j = g[p][i];
        int x = a[j].x, y = a[j].y;
        if (find(x) == find(y)) opt = 0;
        merge(x, y + n);
        merge(x + n, y);
    }
    if (l == r)
    {
        ans[l] = opt;
        for (i = 0; i < len; i++) 
        {
            del();
            del();
        }
        return;
    }
    int mid = l + r >> 1;
    dfs(mid + 1, r, p3, opt);
    dfs(l, mid, p2, opt);
    for (i = 0; i < len; i++) 
    {
        del();
        del();
    }
}

int main()
{
    read(n); read(m); read(q);
    int i, l, r;
    for (i = 1; i <= 2 * n; i++) sze[i] = 1;
    for (i = 1; i <= m; i++)
    {
        read(a[i].x);
        read(a[i].y);
        read(l);
        read(r);
        r--;
        if (l <= r) modify(0, q, l, r, i, 1);
    }
    dfs(0, q, 1, 1);
    for (i = 0; i < q; i++) puts(ans[i] ? "Yes" : "No");
    return 0;
}

标签:opt,ch,int,线段,查集,mid,dfs,sze,BZOJ4025
来源: https://www.cnblogs.com/cyf32768/p/12196291.html