【SGU 128】snake 线段树




128. Snake

time limit per test: 0.25 sec.
memory limit per test: 4096 KB

There are N points given by their coordinates on a plane. All coordinates (xi,yi) are integers in a range from -10000 up to 10000 inclusive . It is necessary to construct a broken line satisfying the following conditions:
1. The broken line should be closed.
2. End points of each segment (verteces) of the broken line can only be the given points, and all given points should be used.
3. Each two consecutive segments of the broken line should form a corner of 90 degrees in each vertex point.
4. The sides of the broken line should be parallel to coordinate axes.
5. The broken line should have no self-crossing and self-contact.
6. The broken line should have the minimal length.
You have to either find the length L of the constructed broken line, or determine that it is impossible to construct such a broken line.

Input

First line contains the number N (4 <= N <= 10000) - amount of points. Each of the following N lines contains coordinates of points separated by space xi and yi (1 <= i <= N). Points are given in random order.

Output

First line should contain the length of the broken line L or 0 if there is no solution.

Sample Input

Sample Output

4
0 0
0 3
3 3
3 0

Sample Output

12

题意:给出n个点。给这n点连线,使得:

1.这n个点连线后形成的折线是闭合。

2.折线必包含所有的n个点,且只能包含这n个点。

3.折线中相邻线段要形成90度的角。

4.每条线段都必须是平行于坐标轴,只能有x方向和y方向的线段。

5.形成的线段不自交。

6.折线必有最小长度。


1.只能有一个连通分支;------------------------

2.不能出现两边相交(除给定的点外)------------线段树

3.还有,要判断点的联通 (并茶几)






1.偶数判合法;

2.find&union;

3.sorting   ,后O(n)找,线段树判2;

4.sorting 算周长

5.细节“ 区间-1要特判 if(l>r)return;

 ----------------------------------------我是分界线---------------------------------------------

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
int n;
int numr[20550],numl[20550];
long long ans=0,an=0;
struct node{
int x,y,id;
node(int xx,int yy,int idd)
{
x=xx,y=yy;id=idd;
}node(){}
};
vector<node> d;
struct nod{
int val;
int lazy;
}tree[85000];
void push_up(int id){
  tree[id].val=tree[lson].val+tree[rson].val;
}
void push_down(int id,int l,int mid,int r)
{
tree[lson].lazy+=tree[id].lazy;
tree[rson].lazy+=tree[id].lazy;
tree[lson].val+=(mid-l+1)*tree[id].lazy;
tree[rson].val+=(r-mid)*tree[id].lazy;
tree[id].lazy=0;
}
void build_tree(int id,int l,int r)
{
if(l>=r)
{
tree[id].lazy=tree[id].val=0;
return ;
}
int mid=(l+r)>>1;
build_tree(lson,l,mid);
build_tree(rson,mid+1,r);
push_up(id);
}
void query(int id,int l,int r,int L,int R)
{
if (R<L) return;
if(r<L||l>R) return ;
if (R>=r && L<=l)
{
an+=tree[id].val;
return;
}
int mid=(l+r)/2;
push_down(id,l,mid,r);
  query(lson,l,mid,L,R);
query(rson,mid+1,r,L,R);
return;
}


void add(int id,int l,int r,int L,int R,int V)
{
if(r<L||l>R) return ;
if(r<=R&&l>=L)
{
tree[id].val+=V*(r-l+1);
tree[id].val%=2;
tree[id].lazy+=V;
return ;
}
int mid=(l+r)/2;
push_down(id,l,mid,r);
add(lson,l,mid,L,R,V);
add(rson,mid+1,r,L,R,V);
push_up(id);  
return;
}




int cc(node x,node y)
{
if(x.x!=y.x) return x.x<y.x;
else return x.y<y.y;
}
int cmp(node x,node y)
{
if(x.y!=y.y)
return x.y<y.y;
else
return x.x<y.x;
}
void solve()
{
long long ans=0;
int ii=0;
while(ii<n-1)
{
int tx=d[ii].x;
int txx=d[ii+1].x;
an=0;
add(1,1,20050,tx,tx,1);
add(1,1,20050,txx,txx,1);
query(1,1,20050,tx+1,txx-1);
if(an>0 && tx+1<txx)
{
cout<<0<<endl;
return ;
}
ii+=2; 
}
for(int i=0;i<n-1;i=i+2) ans+=d[i+1].x-d[i].x;
sort(d.begin(),d.end(),cc);
for(int i=0;i<n-1;i=i+2) ans+=d[i+1].y-d[i].y;
cout<<ans<<endl;
}
int fa[20055];
void init()
{
for (int i=1;i<=10000;i++)
fa[i]=i;
}
int find(int i)
{
if (fa[i]==i) return i;
return fa[i]=find(fa[i]);
}
int unio(int i,int j)
{
fa[find(i)]=find(j);
}


int main()
{
memset(numl,0,sizeof(numl));
memset(numr,0,sizeof(numr));
d.clear();
scanf("%d",&n);
if(n%2)
{
cout<<0<<endl;
return 0;
}
build_tree(1,1,20050);
for(int i=1;i<=n;i++)
{
int aa,bb;
scanf("%d%d",&aa,&bb);
swap(aa,bb);
aa+=10001;
bb+=10001;
d.push_back(node(aa,bb,i));
numl[aa]++; numr[bb]++;
}
for(int i=1;i<=20050;i++)
{
if(numr[i]%2==1||numl[i]%2==1)
{
cout<<0<<endl;
return 0;
}
}

init();
int ff=0;
sort(d.begin(),d.end(),cmp);
for(int i=0;i<n-1;i=i+2)
{
if (find(i+1)==find(i)) ff++;
unio(d[i+1].id,d[i].id);
}
sort(d.begin(),d.end(),cc);
for(int i=0;i<n-1;i=i+2)
{
if (find(i+1)==find(i)) ff++;
unio(d[i+1].id,d[i].id);
}
for (int i=1;i<n;i++)
if (find(i)!=find(i+1))
{
cout<<0<<endl;
return 0;
}

sort(d.begin(),d.end(),cmp);
solve();


//while(1);
return 0;
}
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值