#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 200000 + 10
#define lson L, mid, rt<<1
#define rson mid, R, rt<<1|1
int X1[maxn], X2[maxn];
int n;
int lazy[maxn<<2];
int mark[maxn];
int compress(int *x1, int *x2)
{
vector<int> x;
for(int i=0; i<n; i++)
{
x.push_back(x1[i]);
x.push_back(x2[i]);
}
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
for(int i=0; i<n; i++)
{
x1[i] = find(x.begin(), x.end(), x1[i]) - x.begin();
x2[i] = find(x.begin(), x.end(), x2[i]) - x.begin();
}
return x.size() - 1;
}
void pushdown(int L, int R, int rt)
{
lazy[rt<<1] = lazy[rt];
lazy[rt<<1|1] = lazy[rt];
lazy[rt] = 0;
}
void update(int l, int r, int v, int L, int R, int rt)
{
if(l==L && r==R)
{
lazy[rt] = v;
return ;
}
int mid = (L + R) >> 1;
if(lazy[rt]) pushdown(L, R, rt);
if(r <= mid) update(l, r, v, lson);
else if(l >= mid) update(l, r, v, rson);
else
{
update(l, mid, v, lson);
update(mid, r, v, rson);
}
}
void traverse(int L, int R, int rt)
{
if(L+1 == R)
{
mark[L] = lazy[rt];
return ;
}
int mid = (L + R) >> 1;
if(lazy[rt]) pushdown(L, R, rt);
traverse(lson);
traverse(rson);
}
void solve()
{
int w = compress(X1, X2);
int num = 0;
for(int i=0; i<n; i++)
{
update(X1[i], X2[i], ++num, 0, w, 1);
}
traverse(0, w, 1);
bool vis[maxn];
memset(vis, 0, sizeof(vis));
int ans = 0;
for(int i=0; i<=w; i++)
if(mark[i] && !vis[mark[i]])
{
vis[mark[i]] = 1;
ans++;
}
printf("%d\n", ans);
}
int main()
{
int l;
while(~scanf("%d%d", &n, &l))
{
memset(lazy, 0, sizeof(lazy));
memset(mark, 0, sizeof(mark));
for(int i=0; i<n; i++)
scanf("%d%d", &X1[i], &X2[i]);
solve();
}
return 0;
}
离散化 + 线段树
最新推荐文章于 2024-03-07 10:22:37 发布