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

估计是把这题做全了,线段树离散化+中间增点,用vjudge多个OJ通过

Posted by 15211160230 at 2017-04-14 16:43:18 on Problem 2528
下面的数据
3
3
5 6
4 5
6 8
3
1 10
1 3
6 10
5
1 4
2 6
8 10
3 4
7 10
得出2 3 4的正确结果。
代码:
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <memory.h>
#define maxn 40010
using namespace std;
struct Node{
	int left,right;
	int type;
}data[maxn],de[4*maxn];
int N,ans;
bool vis[maxn];
vector<int>po,Map;
void BuildTree(int i,int left,int right)
{	de[i].left=left;
	de[i].right=right;
	de[i].type=-1;
	if(left==right)		return;
	int mid=(left+right)>>1;
	BuildTree(2*i,left,mid);
	BuildTree(2*i+1,mid+1,right);
}
void PushDown(int i)
{	if(de[i].left!=de[i].right&&de[i].type!=-1)
	{	de[2*i].type=de[2*i+1].type=de[i].type;
		de[i].type=-1;
	}
}
void Insert(int i,int left,int right,int cover)
{	if(left<=de[i].left&&de[i].right<=right)
	{	de[i].type=cover;
		return;
	}
	if(de[i].left==de[i].right)		return;
	PushDown(i);
	int mid=(de[i].left+de[i].right)>>1;
	if(right<=mid)		Insert(2*i,left,right,cover);
	else if(left>mid)	Insert(2*i+1,left,right,cover);
	else
	{	Insert(2*i,left,mid,cover);
		Insert(2*i+1,mid+1,right,cover);
	}	
}
void Query(int i,int left,int right)
{	if(de[i].type!=-1)
	{	vis[de[i].type]=1;
		return;
	}
	if(de[i].left==de[i].right)	return;
	int mid=(de[i].left+de[i].right)>>1;
	if(right<=mid)		Query(2*i,left,right);
	else if(left>mid)	Query(2*i+1,left,right);
	else
	{	Query(2*i,left,mid);
		Query(2*i+1,mid+1,right);
	}
}
void Solve()
{	int i,n,x,y;
	sort(po.begin(),po.end());
	po.erase(unique(po.begin(),po.end()),po.end());
	n=po.size();
	for(i=0;i<n-1;++i)
		if(po[i+1]-po[i]>1)
			po.push_back((po[i+1]+po[i])>>1);
	sort(po.begin(),po.end());		
	for(i=0;i<po.size();++i)
		Map.push_back(po[i]);
	BuildTree(1,0,po.size()-1);
	for(i=0;i<N;++i)
	{	x=lower_bound(Map.begin(),Map.end(),data[i].left)-Map.begin();
		y=lower_bound(Map.begin(),Map.end(),data[i].right)-Map.begin();
		Insert(1,x,y,data[i].type);
	}
	ans=0;
	memset(vis,0,sizeof(vis));
	Query(1,0,po.size()-1);
	for(i=0;i<N;++i)
		if(vis[i])
			ans++;
	printf("%d\n",ans);
}
int main()
{	int T,i;
	scanf("%d",&T);
	while(T--)
	{	scanf("%d",&N);
		po.clear(),Map.clear();
		for(i=0;i<N;++i)
		{	scanf("%d %d",&data[i].left,&data[i].right);
			data[i].type=i;
			po.push_back(data[i].left);
			po.push_back(data[i].right);
		}
		Solve();
	}
    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