(线段树+离散化) Mayor's post(P2528)

这个题用了我很多的时候,了解的线段树很肤浅,做一道简单题就这样了。


我的思路是:先离散化把给出的数据重新排序后得到一个排序数做为新的数据(不过这样有缺陷:有一些线段会被忽略掉,不过这个题就是要这样做,OJ上的数据也能过)

          然后倒着给出的点查找该段里是不是已经贴满。再更新此段,第一个点上的数值为1



注意:这是又点,所以空间要开大,建树时也要用2倍多一些。不然会 rumtime error



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstring>

using namespace std;

#define N 800001

struct my
{
	int l,r;
	int c;
}t[N*3];

int n;

struct you
{
	int x,y;
}
p[N];
int a[N*2];
int b[N*2];

void build(int cur,int l,int r)
{
	t[cur].l=l;
	t[cur].r=r;
	t[cur].c=0;
	//cout<<cur<<' '<<l<<' '<<r<<endl;
	if (l!=r)
	{
		int m=(l+r)/2;
		build(cur*2,l,m);
		build(cur*2+1,m+1,r);
	}

}

void update(int cur,int l,int r,int c)
{
	//cout<<cur<<' '<<l<<' '<<r<<' '<<t[cur].l<<' '<<t[cur].r<<endl;
	if (t[cur].l==l&&t[cur].r==r)
	{
		t[cur].c=c;
		return; 
	}
	


	int m=(t[cur].l+t[cur].r)>>1;

	if (r<=m)
	{
		update(cur*2,l,r,c);
	}
	else if (l>m)
	{
		update(cur*2+1,l,r,c);
	}
	else
	{
		update(cur*2,l,m,c);
		update(cur*2+1,m+1,r,c);
	}

}

int find(int cur,int l,int r)
{
	//cout<<cur<<' '<<t[cur].l<<' '<<t[cur].r<<' '<<l<<' '<<r<<' '<<t[cur].c<<endl;
	int sum=0;
	int m=(t[cur].l+t[cur].r)>>1;
	if ((t[cur].c&&t[cur].l==l&&t[cur].r==r)||t[cur].l==t[cur].r)
	{
		sum=t[cur].c*(r-l+1);
		return sum;
	}

		


	if (t[cur].c)   //如果要选到下一层,会把这层的信息,更新到下一层,这叫懒操作。可以节省时间。
	{
		t[cur*2].c=t[cur].c;
		t[cur*2+1].c=t[cur].c;
		t[cur].c=0;
	}

	if (r<=m)
	{
		sum=find(cur<<1,l,r);
	}
	else if (l>m)
	{
		sum+=find(cur*2+1,l,r);
	}
	else 
	{
		sum+=find(cur*2,l,m)+find(cur*2+1,m+1,r);
	}
//	cout<<sum<<' ';
	return sum;
}

int big_to_small(int l,int r,int v)
{
	int m;
	while (l<=r)
	{
		
		m=(l+r)>>1;
		//cout<<v<<' '<<l<<' '<<r<<' '<<m<<' '<<b[m]<<endl;
		if (b[m]==v)
			return m;
		if (b[m]>v)
			r=m-1;
		else
			l=m+1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int i,j,k;

	int cas;
	cin>>cas;
	while (cas--)
	{
		k=0;
		scanf("%d",&n);
		for (i=0;i<n;i++)
		{
			scanf("%d%d",&p[i].x,&p[i].y);
			a[k++]=p[i].x;
			a[k++]=p[i].y;
		}
		sort(a,a+2*n);

		b[1]=a[0];
		k=2;
		for (i=1;i<2*n;i++)
		{
			if (a[i]!=a[i-1])
				b[k++]=a[i];
		}
		

		for (i=0;i<n;i++)
		{
			//cout<<i<<' '<<p[i].x<<' '<<p[i].y<<endl;
			p[i].x=big_to_small(1,k-1,p[i].x);
			p[i].y=big_to_small(1,k-1,p[i].y);
			if (p[i].x>p[i].y)
				swap(p[i].x,p[i].y);
			//cout<<i<<' '<<p[i].x<<' '<<p[i].y<<endl;
		}

		int ans=0;
		
		build(1,0,30000);
		//cout<<"yes";
		for (i=n-1;i>=0;i--)
		{
			int temp=find(1,p[i].x,p[i].y);
			//cout<<temp<<' ';
			if (temp<(p[i].y-p[i].x+1))
				ans++;
			//cout<<i<<endl;
			//cout<<p[i].x<<' '<<p[i].y<<endl;
			update(1,p[i].x,p[i].y,1);
		}
		
		cout<<ans<<endl;

	}

	return 0;
}




Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26554 Accepted: 7659

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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值