Mayor's posters

http://acm.hust.edu.cn:8080/judge/contest/viewProblem.action?pid=45729

Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules: 
  • Every candidate can place exactly one poster on the wall. 
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown). 
  • The wall is divided into segments and the width of each segment is one byte. 
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections. 
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall. 

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed. 

The picture below illustrates the case of the sample input. 

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
第一份:
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <set>
#include <numeric>
#include <functional>
#include <ctype.h>
#include <utility>
#include <cassert>
#include <time.h>
using namespace std;

#define  max(a,b)  ((a)>(b)?(a):(b))
#define  min(a,b)  ((a)<(b)?(a):(b))

const int inf = 0x3f3f3f3f;
const double eps = 1e-10;
#define N  20005
bool used[N];
int ans;
struct seg_tree
{
    int l,r;
	int color;
}tree[N*4];
struct node
{
	int x,y;
}a[N/2];
struct Node
{
    int pos,val;
	bool flag;
	bool operator < (const Node & oo)const{
		return val < oo.val;
	}
}data[N];

void build_tree(int p,int l,int r)
{
    tree[p].color = -1;
	tree[p].l = l , tree[p].r = r;
	if( tree[p].l == tree[p].r )  return ;
    int mid = (l + r) >> 1;
	build_tree(2*p,l,mid);
	build_tree(2*p+1,mid+1,r);
}
void update_tree(int p,int l,int r,int color)
{
    if(tree[p].l == l && tree[p].r == r)
	{
		tree[p].color = color;
        return ;
	}   
	if(tree[p].color != -1 )
	{
	     tree[2*p].color = tree[2*p+1].color = tree[p].color;
		 tree[p].color = -1;
  	}
	int mid = (tree[p].l + tree[p].r) >> 1;
	if( r <= mid ) update_tree(2*p,l,r,color);
	else if( l >= mid+1 ) update_tree(2*p+1,l,r,color);
	else
	{
        update_tree(2*p,l,mid,color);
		update_tree(2*p+1,mid+1,r,color);
	}
}
void query(int p)
{
     if( tree[p].color != -1)
	 {
		 if(!used[tree[p].color]) { used[tree[p].color] = 1; ans++; }
		 return ;
	 }
	 if(tree[p].l == tree[p].r ) return ; 
	 query(2*p);
	 query(2*p+1);
}
int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
        int n,i,j,k = 0;
		scanf("%d",&n);
		for(i = 1; i <= n; i++)
		{
			scanf("%d%d",&a[i].x,&a[i].y);
            data[k].val = a[i].x; data[k].flag = 0; data[k++].pos = i;
			data[k].val = a[i].y; data[k].flag = 1; data[k++].pos = i;
		}
		sort(data,data+2*n);
		a[data[0].pos].x = 1;
        int Case = 1;
		for(i = 1; i < 2*n; i++)
		{
            if(data[i].val != data[i-1].val) Case++;
			if(data[i].flag) a[data[i].pos].y = Case;
			else a[data[i].pos].x = Case;
		}
		build_tree(1,1,Case);
		for(i = 1; i <= n ; i++)
		{
            // printf("%d %d\n",a[i].x,a[i].y);
			 update_tree(1,a[i].x,a[i].y,i);
		}
        ans = 0;
		memset(used,0,sizeof(used));
		query(1);
        printf("%d\n",ans);
	}
	return 0;
}
第二份:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mm 20005
using namespace std;

struct node
{
	int left;
	int right;
	int flag;
	int color;
}t[8*mm];
struct 
{
  int ll;
  int rr;	
}ss[2*mm];
int bb[2*mm],sum,v;
bool visited[2*mm];
void build(int l,int r,int root)
{
	t[root].left=l;t[root].right=r;
	t[root].flag=0;t[root].color=0;
	if(l==r)return ;
	build(l,(l+r)/2,2*root);
	build((l+r)/2+1,r,2*root+1);
	return ;
}
int find(int x)
{
	int i;
	for(i=1;i<=v;i++)
	  if(bb[i]==x)return i;
 return 0;
}

int qu(int l,int r,int root,int se)
{
	if(t[root].color)return 0;
	if(l==t[root].left&&r==t[root].right)
	{
		if(t[root].flag==0)
		{
		  t[root].color=se;	
		  t[root].flag=1;
		  if(!visited[se])
		  {
			sum++;
			visited[se]=true;
		  }
		}
		 return 0;
	}
	int mi=(t[root].right+t[root].left)/2;
    if(r<=mi)qu(l,r,2*root,se);
    else if(l>mi)qu(l,r,2*root+1,se);
	else{
    	qu(l,mi,2*root,se);
    	qu(mi+1,r,2*root+1,se);
    }
	if(t[2*root].flag&&t[2*root+1].flag)
	 t[root].flag=1;
}
int main()
{
	int T,n,a[2*mm],i;
	scanf("%d",&T);
	while(T--)
	{
		v=1;
		sum=0;
		memset(visited,false,sizeof(visited));
	 	scanf("%d",&n);
	 	for(i=1;i<=n;i++)
	    {
	 	  scanf("%d %d",&ss[i].ll,&ss[i].rr);
	    }
	    for(i=1;i<=n;i++)
	      a[i]=ss[i].ll;
        for(i=1;i<=n;i++)
          a[n+i]=ss[i].rr;
        sort(a+1,a+1+2*n);
        //for(i=1;i<=2*n;i++)printf("%d ",a[i]);
        bb[1]=a[1]; 
        for(i=2;i<=2*n;i++)
          if(bb[v]!=a[i])bb[++v]=a[i];
        build(1,v,1);
        int u=1;
		for(i=n;i>=1;i--)
		{
			qu(find(ss[i].ll),find(ss[i].rr),1,u++);
		}
        printf("%d\n",sum);
	}
}
第三份:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>

using namespace std;
#define MAXN 21000

struct point 
{
	int x,y;

}an[MAXN*3];

struct node 
{
	int l;
	int r;
	int val;

}t[MAXN*5];

int flag[MAXN*3];
int A[MAXN*3];
int nn;
void make(int st,int ed,int num)
{
	t[num].l=st;
	t[num].r=ed;
	t[num].val=0;

	if(st==ed)return ;

	int mid=(t[num].l+t[num].r)/2;
	make(st,mid,num*2);
	make(mid+1,ed,num*2+1);
}

void insert(int st,int ed,int num,int color)
{
	if(st<=t[num].l&&ed>=t[num].r)
	{
		t[num].val=color;
		return ;
	}

	if(t[num].val>=0&&t[num].r!=t[num].l)
	{
		t[num*2+1].val=t[num].val;
		t[num*2].val=t[num].val;
		t[num].val=-1;
	}

	int mid=(t[num].l+t[num].r)/2;
	if(st<=mid)
		insert(st,ed,num<<1,color);
	if(ed>mid)
		insert(st,ed,num<<1|1,color);

}

int ans;

void query(int l,int r,int num)
{
	if(t[num].val!=-1)
	{
		if(!flag[t[num].val])
		{
			flag[t[num].val]=true;
			ans++;
		}
		return ;
	}

	int mid=(t[num].r+t[num].l)>>1;
	if(r<=mid)
		query(l,r,num*2);
	else if(l>mid)
		query(l,r,num*2+1);
	else
	{
		query(l,mid,num<<1);
		query(mid+1,r,num<<1|1);
	}
}

int search(int x)
{
	int left=0,right=nn;
	while(left<=right)
	{
		int mid=(left+right)>>1;
		if(A[mid]==x)
			return mid;
		if(A[mid]<x)
			left=mid+1;
		if(A[mid]>x)
			right=mid-1;
	}
	return -1;
}

int main()
{
	int t;
	int n;
	int k;
	int i;
	scanf("%d",&t);
	while(t--)
	{

		memset(flag,false,sizeof(flag));
		memset(A,0,sizeof(A));
		k=0;
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%d %d",&an[i].x,&an[i].y);
			A[k++]=an[i].x;
			A[k++]=an[i].y;
		}
		sort(A,A+k);
		nn=0;
		for(i=0;i<k;i++)
		{
			if(i==1||A[i]!=A[i-1])
				A[nn++]=A[i];
		}
		make(0,nn-1,1);
		for(i=0;i<n;i++)
		{
			insert(search(an[i].x),search(an[i].y),1,i);
		}
		ans=0;
		query(0,nn-1,1);
		printf("%d\n",ans);
	}
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值