poj 2528 Mayor's posters(线段树 离散化)
2015-10-08 22:57
309 查看
Language: Default Mayor's posters
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 <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
struct node
{
int l,r;
int col;
};
node no[MAXN<<4];
int ll[MAXN],rr[MAXN];
int a[MAXN*4];//离散化用
int vis[MAXN];
void build(int l,int r,int k)
{
no[k].l=l;
no[k].r=r;
no[k].col=-1;
if(l==r)
return;
int mid=(l+r)/2;
build(l,mid,k*2);
build(mid+1,r,k*2+1);
}
void pushdown(int k)
{
if(no[k].col!=-1)
{
no[k*2].col=no[k*2+1].col=no[k].col;
no[k].col=-1;
return;
}
}
void update(int l,int r,int k,int col)
{
if(no[k].l>=l&&no[k].r<=r)
{
no[k].col=col;
return;
}
pushdown(k);
int mid=(no[k].l+no[k].r)/2;
if(r<=mid) update(l,r,k*2,col);
else if(l>mid) update(l,r,k*2+1,col);
else
{
update(l,mid,k*2,col);
update(mid+1,r,k*2+1,col);
}
}
int ans;
void query(int k)
{
if(no[k].col!=-1)
{
if(!vis[no[k].col]) ans++;
vis[no[k].col]=1;
return;
}
if(no[k].l==no[k].r)
return;
query(k*2);
query(k*2+1);
}
int bin(int key,int n)
{
int s=0,t=n-1;
while(s<=t)
{
int mid=(s+t)/2;
if(a[mid]==key)
return mid;
else if(a[mid]<key)
s=mid+1;
else t=mid-1;
}
return -1;
}
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
int n;
scanf("%d",&n);
int num=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&ll[i],&rr[i]);
a[num++]=ll[i];
a[num++]=rr[i];
}
sort(a,a+num);
int m=1;
for(int i=1;i<num;i++)
{
if(a[i]!=a[i-1])
a[m++]=a[i];
}
for(int i=m-1;i>0;i--)
{
if(a[i]!=a[i-1]+1)
a[m++]=a[i-1]+1;
}
sort(a,a+m);
build(0,m,1);
for(int i=0;i<n;i++)
{
int l=bin(ll[i],m);
int r=bin(rr[i],m);
update(l,r,1,i);
}
ans=0;
MEM(vis,0);
query(1);
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 自定义BaseActivity
- Mybatis框架基础学习(一)
- “钱三篇”后续之复利现值
- LTP算子
- superoj495 不要62(未完成)
- 字母统计
- 01.ActiveMQ安装部署
- 刘宇凡:罗永浩的锤子情怀只能拿去喂狗
- C++两个栈实现队列
- E5_1.c:(.text+0x67):对‘sqrt’未定义的引用 collect2: error: ld returned 1 exit status
- 正则表达式学习笔记
- MySQL读写分离-amoeba
- ActiveMQ 即时通讯服务 浅析
- POJ 题目2114 Boatherds(树的点分治)
- VC++压缩解压zip文件(支持密码)
- linux自动备份
- Android-ListView-Adapter view无复用
- ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)
- objective_c
- java内部类 和外嵌类