关闭

SGU 128 Snake

688人阅读 评论(0) 收藏 举报

好吧。。我2了。。。我2了。。。我2了。。。。还要用并查集判断是否联通

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define SZ(a)           ((int)a.size())
#define PP(n,m,a)       puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-10;
const double pi = acos(-1.0);

const int maxn = 22222;
const int add = 11111;

struct px
{
    int x;
    int y;
    int id;
    bool operator < (const px & cmp ) const
    {
        if(x!=cmp.x)
        {
            return x<cmp.x;
        }
        else
        {
            return y<cmp.y;
        }
    }
}zx;

struct py
{
    int x;
    int y;
    int id;
    bool operator < (const py & cmp ) const
    {
        if(y!=cmp.y)
        {
            return y<cmp.y;
        }
        else
        {
            return x<cmp.x;
        }
    }
}zy;


int zz[maxn];
vector<px>vx;
vector<py>vy;

int linx[maxn];
int liny[maxn];
bool vis[maxn];
int f[maxn];

int n;
i64 len;

int find(int x)
{
    if(x!=f[x])
    {
        return f[x]=find(f[x]);
    }
    return x;
}

int lowbit(int x)
{
    return x&(-x);
}


int sum(int x)
{
    int re=0;
    while(x)
    {
        re+=zz[x];
        x -= lowbit(x);
    }
    return re;
}

void insert(int x,int p)
{
    while(x<maxn)
    {
        zz[x] += p;
        x += lowbit(x);
    }
    return ;
}


int main()
{
    len = 0;
    vx.clear();
    vy.clear();
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        f[i]=i;
    }
    int tx,ty;
    for(int i=1;i<=n;i++)
    {
        cin>>tx>>ty;
        ty += add;
        zx.x = zy.x = tx;
        zx.y = zy.y = ty;
        zx.id = zy.id = i;
        vx.pb(zx);
        vy.pb(zy);
    }

    sort(vx.begin(),vx.end());
    sort(vy.begin(),vy.end());

    int now,to;
    for(int i=0;i<vx.size();i+=2)
    {
        if(vx[i].x == vx[i+1].x)
        {
            now = vx[i].id;
            to = vx[i+1].id;
            len += vx[i+1].y - vx[i].y;
            linx[now] = to;
            linx[to] = now;
            f[find(now)]=find(to);
        }
        else
        {
            cout<<"0"<<endl;
            return 0;
        }
    }
    for(int i=0;i<vy.size();i+=2)
    {
        if(vy[i].y == vy[i+1].y)
        {
            now = vy[i].id;
            to = vy[i+1].id;
            len += vy[i+1].x - vy[i].x;
            liny[now]= to;
            liny[to] =now;
            f[find(now)]=find(to);
        }
        else
        {
            cout<<"0"<<endl;
            return 0;
        }
    }

    int kk=0;
    for(int i=1;i<=n;i++)
    {
        if(f[i] == i)
        {
            kk++;
        }
    }
    if(kk!=1)
    {
        cout<<"0"<<endl;
        return 0;
    }

    MM(vis,false);
    int tl,tr;
    int n2,t2;
    for(int i=0;i<vx.size();i+=2)
    {
        now = vx[i].id;
        n2 = vx[i+1].id;
        to = liny[now];
        t2 = liny[n2];
        tl = vx[i].y;
        tr = vx[i+1].y;

        vis[n2] = vis[now] =true;
        if(vis[to])
        {
            insert(vx[i].y,-1);
        }
        if(vis[t2])
        {
            insert(vx[i+1].y,-1);
        }

        int temp = sum(tr-1) - sum(tl);

        if(temp>0)
        {
            cout<<"0"<<endl;
            return 0;
        }
        if(!vis[to])
        {
            insert(vx[i].y,1);
        }
        if(!vis[t2])
        {
            insert(vx[i+1].y,1);
        }
    }
    cout<<len<<endl;
    return 0;
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:290340次
    • 积分:6420
    • 等级:
    • 排名:第3949名
    • 原创:300篇
    • 转载:19篇
    • 译文:0篇
    • 评论:126条
    最新评论