您的位置:首页 > 其它

poj2528--Mayor's posters--离散化&查询区间的不同元素个数

2013-09-11 22:35 405 查看
Mayor's posters

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 35431 Accepted: 10286
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

Source

Alberta Collegiate Programming Contest 2003.10.18

额,

题意就是在一条长为L上的轴上贴选举纸,每张纸有一定长度,

后放的纸会覆盖前放的纸,

问最终能看见多少张不同的纸,

我是用离线+离散化做的。。。。

以下是代码(居然wrong了一炮,原因是忘记Memset了,不应该啊不应该啊)

#include <iostream>
#include <cstdio>
#include <algorithm>
#define mid ((l+r)>>1)
#define lson (root<<1)
#define rson (root<<1|1)
using namespace std;
const int maxnum=10100;

struct query
{
int ql,qr;
}q[maxnum];

struct Node
{
int l,r;
int color;
}tree[maxnum*6];

int color[maxnum];//记录一种颜色是否出现过
int a[maxnum*2]; //记录所有的左右端点
int x[maxnum*2]; //记录去重后的所有左右端点

void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].color=0;
if(l!=r)
{
build(l,mid,lson);
build(mid+1,r,rson);
}
}

void update(int color,int ql,int qr,int root)
{
int l=tree[root].l,r=tree[root].r;
if(ql==l&&qr==r)
{
tree[root].color=color;
}
else
{
if(tree[root].color>0)
{
tree[lson].color=tree[root].color;
tree[rson].color=tree[root].color;
tree[root].color=0;
}
if(ql>mid) update(color,ql,qr,rson);
else if(qr<=mid) update(color,ql,qr,lson);
else
{
update(color,ql,mid,lson);
update(color,mid+1,qr,rson);
}
}
}

int ans;
void cal(int root)
{
int l=tree[root].l,r=tree[root].r;
if(tree[root].color>0) //只有当该区间有颜色且该颜色以前没出现过才++ans
{
if(color[tree[root].color]==0)
{
color[tree[root].color]=1;
++ans;
}
}
else
{
if(l==r) return;
else
{
cal(lson);
cal(rson);
}
}
}

int main()
{
//freopen("input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
memset(color,0,sizeof(color)); //日!以后一定记得测多组样例,居然因为这个wrong了一炮!
int n;
scanf("%d",&n);
int num=1;
for(int i=1;i<=n;++i)
{
scanf("%d%d",&q[i].ql,&q[i].qr);
a[num++]=q[i].ql;
a[num++]=q[i].qr;
}
num=1;
sort(a+1,a+1+2*n);//排序去重
x[1]=a[1];
for(int i=2;i<=2*n;++i) if(a[i]!=a[i-1]) x[++num]=a[i];
build(1,num,1);
for(int i=1;i<=n;++i)
{
update(i,lower_bound(x+1,x+1+num,q[i].ql)-x,lower_bound(x+1,x+1+num,q[i].qr)-x,1);
//寻找q[i]所对应的值在去重后的数组中的下标位置
//一开始二货的直接用q[i].ql and q[i].qr 去更新!
//调BUG调了好久,不应该啊!
}
ans=0;
cal(1);//计算结果
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm poj 线段树