标签:
7 10
//用C++提交WA,G++就能AC
//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=20011;
const int inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,n) for(int i=0;i<(n);i++)
template<class T>inline T read(T&x)
{
char c;
while((c=getchar())<=32);
bool ok=false;
if(c=='-')ok=true,c=getchar();
for(x=0; c>32; c=getchar())
x=x*10+c-'0';
if(ok)x=-x;
return x;
}
template<class T> inline void read_(T&x,T&y)
{
read(x);
read(y);
}
template<class T> inline void write(T x)
{
if(x<0)putchar('-'),x=-x;
if(x<10)putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
write(x);
putchar('\n');
}
//-------IO template------
typedef long long LL;
struct poster
{
int left,right;
}pos[maxn];
int x[maxn<<1];
int hash[10000011];
struct node
{
bool bcovered;
}p[maxn<<3];
///初始都可见,即false
void build(int rt,int L,int R)
{
p[rt].bcovered=false;
if(L==R)return ;
build(lson);
build(rson);
}
///插入一张海报,true是此张海报部分或者全部可见,false是不可见
bool update(int rt,int L,int R,int x,int y)
{
//printf("%d %d %d %d %d\n",rt,L,R,x,y);
if(p[rt].bcovered)return false;
if(L==x&&y==R)
{
return p[rt].bcovered=true;
}
bool ans;
if(y<=M)
ans=update(lson,x,y);
else if(x>M)
ans=update(rson,x,y);
else
{
ans=update(lson,x,M)|update(rson,M+1,y);//按位或,保证|的两边都会计算,不能用||,因为||是短路运算,如果左边是true,右边的就不会计算了
}
p[rt].bcovered=p[rt<<1].bcovered&&p[rt<<1|1].bcovered;//回溯回来更新父亲的状态
return ans;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
// #endif // ONLINE_JUDGE
int T;
int n,m,i,j,k,t;
read(T);
while(T--)
{
read(n);
int cnt=0;
For(i,n)
{
read_(pos[i].left,pos[i].right);
x[cnt++]=pos[i].left;
x[cnt++]=pos[i].right;
}
sort(x,x+cnt);
cnt=unique(x,x+cnt)-x;
int num=0;
For(i,cnt)///离散化
{
hash[x[i]]=num;
if(i<cnt-1)
{
if(x[i+1]-x[i]==1)//处理不相邻的离散化后依旧不相邻,相邻的依然相邻
num++;
else
num+=2;
}
}
build(1,0,num);
int ans=0;///从外往里贴海报(和真实的情况正好相反),保证后来的贴的对以前的没影响
for(i=n-1;i>=0;i--)if(update(1,0,num,hash[pos[i].left],hash[pos[i].right]))
ans++;
writeln(ans);
}
return 0;
}
/*
离散化要注意一点
1 10
1 3
6 10
应该变为
0 6
0 2
4 6
注意离散化前不相邻的,离散化后也是不相邻的
*/
D - Mayor's posters (线段树+离散化处理) POJ 2528
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/44873857