Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

发福利!谁能考到我的代码

Posted by OZY123 at 2016-02-16 10:25:44 on Problem 2528
#include<cstdio>
#include<cstring>
int T;
int n;
struct qr
{
	int l,r;
	int s1,s2;
	int c;
};
qr s[200005];
struct qq
{
	int a,b;
};
qq k[200005];
int k1[200005];
int k2[200005];
bool c[200005];
int num;
void qs (int l,int r)
{
	if (l>=r) return ;
	int i=l,j=r;
	int m=k1[l];
	while (i<=j)
	{
		while (k1[i]<m) i++;
		while (k1[j]>m) j--;
		if (i<=j)
		{
			int tt=k1[i];
			k1[i]=k1[j];
			k1[j]=tt;
			tt=k2[i];
			k2[i]=k2[j];
			k2[j]=tt;
			i++;j--;
		}
	}
	qs(l,j);
	qs(i,r);
}
void build (int l,int r)
{
	num++;
	int a=num;
	s[a].l=l;
	s[a].r=r;
	s[a].c=0;
	if (l+1==r)
	{
		s[a].s1=s[a].s2=-1;
		return ;
	}
	int mid=(l+r)/2;
	s[a].s1=num+1;build(l,mid);
	s[a].s2=num+1;build(mid,r);
}
void ch (int x,int y,int z,int k)
{
	if (s[x].l==y&&s[x].r==z)
	{
		s[x].c=k;
		return ;
	}
	int mid=(s[x].l+s[x].r)/2;
	int s1=s[x].s1,s2=s[x].s2;
	if (s[x].c!=-1)
	{
		s[s1].c=s[x].c;
		s[s2].c=s[x].c;
	}
	if (z<=mid)	ch(s1,y,z,k);
	else if (y>=mid) ch(s2,y,z,k);
	else 
	{
		ch(s1,y,mid,k);
		ch(s2,mid,z,k);
	}
	if (s[s1].c==s[s2].c&&s[s1].c!=-1) s[x].c=s[s1].c;
	else s[x].c=-1;
}
void check (int x,int y,int z)
{
	if (s[x].c!=-1)
	{
		c[s[x].c]=true;
		return ;
	}
	int mid=(s[x].l+s[x].r)/2;
	int s1=s[x].s1,s2=s[x].s2;
	if (s[x].c!=-1)
	{
		s[s1].c=s[x].c;
		s[s2].c=s[x].c;
	}
	if (z<=mid)	check(s1,y,z);
	else if (y>=mid) check(s2,y,z);
	else 
	{
		check(s1,y,mid);
		check(s2,mid,z);
	}
}
int main()
{
	scanf("%d",&T);
	while (T--)
	{
		memset(c,false,sizeof(c));
		num=0;
		scanf("%d",&n);
		for (int u=1;u<=n;u++)
		{
			scanf("%d%d",&k[u].a,&k[u].b);
			k[u].b++;//改为按点储存
			k1[u]=k[u].a;
			k1[u+n]=k[u].b;
			k2[u]=u;
			k2[u+n]=u+n;
		}
		int N=2*n;
		qs(1,N);
		int t=-1,t1=0;
		for (int u=1;u<=N;u++)
		{
			if (t!=k1[u])
			{
				t=k1[u];
				t1++;
			}
			if (k2[u]>n) k[k2[u]-n].b=t1;
			else k[k2[u]].a=t1;
		}
		build(1,t1);
		for (int u=1;u<=n;u++)
			ch(1,k[u].a,k[u].b,u);
		//for (int u=1;u<=num;u++) printf("%d %d %d\n",s[u].l,s[u].r,s[u].c);
		check(1,1,t1);
		//for (int u=1;u<=10;u++) printf("%d %d\n",u,c[u]);
		int ans=0;
		for (int u=1;u<=t1;u++)
			if (c[u]==true) ans++;
			printf("%d\n",ans);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator