(线段树)Mayor's posters --poj -- 2528

时间:2025-04-24 16:34:31

链接:

http://poj.org/problem?id=2528

覆盖问题, 要从后往前找, 如果已经被覆盖就不能再覆盖了,否则就可以覆盖

递归呀递归什么时候我才能吃透你

代码:

 #include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int N = *1e4+; struct Node
{
int L, R;
}s[N<<]; struct node
{
int L, R;
bool isCover;
int Mid()
{
return (L+R)>>;
}
}a[N<<]; int Hash[N<<]; void UpDate(int r)
{
if(a[r].L!=a[r].R && (a[Lson].isCover && a[Rson].isCover))
a[r].isCover=true;
}
void BuildTree(int r, int L, int R)
{
a[r].L = L, a[r].R = R;
a[r].isCover = false; if(L==R)
return ; BuildTree(Lson, L, a[r].Mid());
BuildTree(Rson, a[r].Mid()+, R);
}
bool Insert(int r, int L, int R)
{
if(a[r].isCover) return false; if(a[r].L==L && a[r].R==R)
{
a[r].isCover=true;
return true;
} bool ans; if(R<=a[r].Mid())
ans = Insert(Lson, L, R);
else if(L>a[r].Mid())
ans = Insert(Rson, L, R);
else
{
bool lson = Insert(Lson, L, a[r].Mid());
bool rson = Insert(Rson, a[r].Mid()+, R); ans = lson|rson;
} UpDate(r); return ans;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, nh=;
scanf("%d", &n);
memset(s, , sizeof(s)); for(int i=; i<=n; i++)
{
scanf("%d%d", &s[i].L, &s[i].R);
Hash[nh++] = s[i].L, Hash[nh++]=s[i].L-;
Hash[nh++] = s[i].R, Hash[nh++]=s[i].R+;
} sort(Hash, Hash+nh);
nh = unique(Hash, Hash+nh) - Hash; BuildTree(, , nh); int ans=;
for(int i=n; i>; i--)
{
int L = lower_bound(Hash, Hash+nh, s[i].L) - Hash; // 返回是s[i].L的下标
int R = lower_bound(Hash, Hash+nh, s[i].R) - Hash; if(Insert(, L, R))
ans += ;
} printf("%d\n", ans);
}
return ;
}