简单翻译
n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) 。求出最后还能看见多少张海报。
Input
第一行: 样例个数T
第二行: 贴海报的人n
接下来n行: 每个人贴海报的范围
Output
对于每一个输入,输出最后可以看到的海报张数。下面这个图是样例解释
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
题解
显然想到线段树。因为区间太大,可以离散化,把每个点排序重新编号再放回原区间。
倒过来添加,判断,如果没有被覆盖,ans++,否则覆盖
常数巨大的丑陋代码
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <algorithm>
using namespace std;
# define IL inline
# define RG register
# define UN unsigned
# define ll long long
# define rep(i, a, b) for(RG int i = a; i <= b; i++)
# define per(i, a, b) for(RG int i = b; i >= a; i--)
# define uev(e, u) for(RG int e = ft[u]; e != -1; e = edge[e].nt)
# define mem(a, b) memset(a, b, sizeof(a))
# define max(a, b) ((a) > (b)) ? (a) : (b)
# define min(a, b) ((a) < (b)) ? (a) : (b)
IL ll Get(){
RG char c = '!'; RG ll num = 0, z = 1;
while(c != '-' && (c > '9' || c < '0')) c = getchar();
if(c == '-') z = -1, c = getchar();
while(c >= '0' && c <= '9') num = num*10+c-'0', c = getchar();
return num*z;
}
const int MAXN = 10001, MAXM = 1000001;
struct Tree{
int l, r, num;
} tree[MAXM];
int n, ans, a[MAXN][2], num, flag;
struct Post{
int v, pos;
IL bool operator <(Post c) const{
return v < c.v;
}
} t[MAXN<<1];
IL void Updata(RG int now){
if(!tree[now<<1].num || !tree[now<<1|1].num) return;
tree[now].num = 1;
}
IL void Build(RG int now, RG int l, RG int r){
tree[now] = (Tree) {l, r, 0};
if(l == r) return;
RG int mid = l+r>>1;
Build(now<<1, l, mid); Build(now<<1|1, mid+1, r);
}
IL void Add(RG int l, RG int r, RG int now){
if(tree[now].num) return;
if(tree[now].l == l && tree[now].r == r){
tree[now].num = 1; flag = 1;
Updata(now);
return;
}
RG int mid = tree[now].l+tree[now].r>>1;
if(mid >= r) Add(l, r, now<<1);
if(l <= mid && mid < r) Add(l, mid, now<<1), Add(mid+1, r, now<<1|1);
if(mid < l) Add(l, r, now<<1|1);
Updata(now);
}
int main(){
RG int T = Get();
while(T--){
n = Get(); ans = num = 0;
rep(i, 1, n){
t[i*2-1].v = Get(); t[i*2].v = Get();
t[i*2-1].pos = i*2-1;
t[i*2].pos = i*2;
}
sort(t+1, t+2*n+1);
rep(i, 1, 2*n){
RG int c = t[i].pos%2, pos = (t[i].pos+1)/2;
if(t[i].v != t[i-1].v) num++;
a[pos][!c] = num;
}
Build(1, 1, num);
per(i, 1, n){
flag = 0;
Add(a[i][0], a[i][1], 1);
ans += flag;
}
printf("%d\n", ans);
}
return 0;
}