poj 2528 离散化+线段树

这个破题  我WA 了   我实在找不到我那里错了

题意:有一个墙,往墙上贴报纸,最后问能看到几张报纸

其实就是很容易的线段树,不容易的地方在于离散化

离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误(包括我以前的代码,这题数据奇弱) 出下面两个简单的例子应该能体现普通离散化的缺陷:

1-10 1-4 5-10

1-10 1-4 6-10  为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10] 如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

int x[21111<<3];
int tree[21111<<4];
int all;
int mark[21111<<4];
int fun(int key,int l,int r)
{
	int mid=(l+r)/2;
	while(l<=r)
	{
		mid=(l+r)/2 ;
		if(key==x[mid]) return mid;
		if(key<x[mid]) r=mid-1;
		else
		l=mid+1;
	}
	return -1;
}
int insert(int L,int R,int color,int l,int r,int index)
{
	if(l<=L&&r>=R)
	{
		tree[index]=color;
		return 0;
	}
	if(tree[index]>0)
	{
		tree[index*2]=tree[index];
		tree[index*2+1]=tree[index];
		tree[index]=0;
	}
	int mid=(R+L)/2;
	if(l<=mid)
	{
		insert(L,mid,color,l,r,index*2);
	}
	if(r>mid)
	{
		insert(mid+1,R,color,l,r,index*2+1);
	}
}
int query(int l,int r,int index)
{
	if(l==r)
	{
		if(mark[tree[index]]==0)
		{
			mark[tree[index]]=1;
			all++;
		}
		return 0;
	}
	if(tree[index]>0)
	{
		tree[index*2]=tree[index*2+1]=tree[index];
		tree[index]=0;
	}
	int mid=(l+r)/2;
	query(l,mid,index*2);
	query(mid+1,r,index*2+1);
}
int main()
{
	int n,t;
	int nn;
	int i;
	scanf("%d",&t);
	int a[21111*4],b[21111*4];
	while(t--)
	{memset(mark,0,sizeof(mark));
	memset(tree,0,sizeof(tree));
		scanf("%d",&n);
		nn=1;
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i],&b[i]);
			x[nn++]=a[i];
			x[nn++]=b[i];
		}
		sort(x+1,x+nn);
		int m=2;
		for(i=2;i<nn;i++)
		{
			if(x[i]!=x[i-1])
			x[m++]=x[i];
		}
		for(i=m;i>0;i--)
		{
			if(x[i]-x[i-1]>1) x[m++]=x[i]-1;
		}

		sort(x+1,x+m);
		m--;
		for(i=1;i<=n;i++)
		{
			int temp1=fun(a[i],1,m);
			int temp2=fun(b[i],1,m);
			insert(1,m,i,temp1,temp2,1);
		}
		all=0;
	//	printf("\nn");

		query(1,m,1);
		printf("%d\n",all);
	}
	return 0;
}
时间: 2024-07-31 21:32:22

poj 2528 离散化+线段树的相关文章

POJ 2299 离散化线段树

点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 40827   Accepted: 14752 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by

poj/OpenJ_Bailian - 2528 离散化+线段树

传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US //http://poj.org/problem?id=2528 题意: 给你n长海报,每张海报在墙上贴的区间范围是l,r 问你这n张海报贴完后,可以看见的海报数量是多少 题解: 离散化+线段树 因为l,r的数据范围是1e7,而题目只给了64MB的空间,直接存的话空间会炸掉,所以需用用到离散化的技巧 然后按照端点单点更新即可 现在重新写这题发现这个题坑点在于每一个点他都代表一个长度为

hdu 2528(离散化线段树)

题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43201   Accepted: 12591 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing

POJ 2528 QAQ线段树+离散化

Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitStatus 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 pl

poj 2528 动态线段树

动态建立结点就不用离散化了,细节见代码,相信还是比较好理解的. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <set> 5 using namespace std; 6 7 const int N = 500000; 8 set<int> s; 9 int cnt; 10 11 struct Node 12 { 13 int lc, rc, l

poj 2528 Mayor&#39;s posters【离散化+线段树】

题目:poj 2528 Mayor's posters 题意:给一个长度非常长的墙上贴长度为ai的海报,由于有的会覆盖掉,求最后能看见的海报个数. 分析:题目和POJ2777 一模一样,方法也一样,只不过这个要离散化,其次要数组开大一点.至少2倍. 离散化的时候用了C++的 pair 类,还是比较好用的. 代码: #include <iostream> #include <algorithm> #include <utility> #include <cstrin

POJ 2528 Mayor&#39;s posters(离散化线段树)

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

POJ 2528 Mayor&amp;#39;s posters 离散化+线段树

题目大意:给出一些海报和贴在墙上的区间.问这些海报依照顺序贴完之后,最后能后看到多少种海报. 思路:区间的范围太大,然而最多仅仅会有10000张海报,所以要离散化. 之后用线段树随便搞搞就能过. 关键是离散化的方法,这个题我时隔半年才A掉,之前一直就TTT,我还以为是线段树写挂了. 当我觉得我自己的水平这样的水线段树已经基本写不挂的时候又写了这个题,竟然还是T. 后来我对照别人的代码,才发现是我的离散化写渣了. 以下附AC代码(79ms),这个离散化写的比較优雅.时间也非常快,以后就这么写了.

POJ 2528 Mayor&#39;s posters (离散化 + 线段树)

强烈不推荐在POJ做这道题!!! 强烈不推荐在POJ做这道题!!! 强烈不推荐在POJ做这道题!!! 推荐去UVA 10587 或 SCU 2249 POJ的数据比较水且可能有错,一些本来错误的数据但可以水过,以及在UVA与SCU同样题目都能AC的程序在POJ莫名WA了. 建议写完程序后跑下这组数据: 1 3 1 10 1 3 6 10 好多题解的答案是2,但答案明显是3,这是因为每个数字其实表示的是一个单位长度,并非一个点 , 这就会导致像这样的区间: 1-10 1-4 5-10 1-10 1