/*
Mayor's posters
线段树区间覆盖
离散化
特殊数据:
1
3
1 10
1 4
6 10
输出:
3
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson (pos<<1)
#define rson (pos<<1|1)
const int maxn = 55555;
int lx[maxn],rx[maxn],vis[maxn];
int n,Hash[maxn * 2],cnt,ans;
int cover[maxn << 2];
void Hash_Init(){
sort(Hash,Hash + cnt);
cnt = unique(Hash,Hash + cnt) - Hash;
int m = cnt;
for(int i = 1; i < cnt; i++)
if(Hash[i] != Hash[i - 1] + 1){
Hash[m++] = Hash[i - 1] + 1;
}
cnt = m;
sort(Hash,Hash + cnt);
cnt = unique(Hash,Hash + cnt) - Hash;
}
int HASH(int t){
return lower_bound(Hash,Hash + cnt,t) - Hash;
}
void build(){
memset(cover,-1,sizeof(cover));
memset(vis,0,sizeof(vis));
}
void pushdown(int pos){
if(cover[pos] != -1){
cover[lson] = cover[rson] = cover[pos];
cover[pos] = -1;
}
}
void update(int L,int R,int l,int r,int pos,int d){
if(l <= L && R <= r){
cover[pos] = d;
return;
}
pushdown(pos);
int mid = (L + R) >> 1;
if(l <= mid)
update(L,mid,l,r,lson,d);
if(r > mid)
update(mid + 1,R,l,r,rson,d);
}
void query(int l,int r,int pos){
if(cover[pos] != -1){
if(!vis[cover[pos]]){
vis[cover[pos]] = 1;
ans ++;
}
return;
}
if(l == r) return;
pushdown(pos);
int mid = (l + r) >> 1;
query(l,mid,lson);
query(mid + 1,r,rson);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%d%d",&lx[i],&rx[i]);
Hash[cnt++] = lx[i];
Hash[cnt++] = rx[i];
}
Hash_Init();
build();
ans = 0;
int ret = 0;
for(int i = 0; i < n; i++){
int l = HASH(lx[i]),r = HASH(rx[i]);
update(0,cnt,l,r,1,ret++);
}
query(0,cnt,1);
printf("%d\n",ans);
}
return 0;
}
POJ 2528
最新推荐文章于 2022-12-07 12:50:35 发布
本文介绍了一种使用线段树实现的区间覆盖算法,并通过一个具体的市长海报问题实例展示了如何进行离散化处理,包括数据结构的设计、更新操作及查询操作等关键步骤。
678

被折叠的 条评论
为什么被折叠?



