#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 25000;
struct node
{
int l;
int r;
bool full;
}nodes[MAXN * 5];
struct postPnt
{
int pos;
int num;
}post[MAXN];
// cmp functions
bool cmp1(postPnt &a, postPnt &b)
{
if(a.pos < b.pos)
return true;
return false;
};
bool cmp2(postPnt &a, postPnt &b)
{
if(a.num > b.num)
return true;
if(a.num == b.num && a.pos < b.pos)
return true;
return false;
};
// segment tree functions
void build(int l, int r, int pos)
{
nodes[pos].l = l;
nodes[pos].r = r;
nodes[pos].full = false;
int mid;
if(r != l)
{
mid = (l + r) / 2;
build(l, mid, pos * 2);
build(mid + 1, r, pos *2 + 1);
}
};
bool flag = false;
void insert(int l, int r, int pos)
{
if(nodes[pos].full)
return;
int l0 = nodes[pos].l;
int r0 = nodes[pos].r;
if(l0 == l && r0 == r)
{
nodes[pos].full = true;
flag = true;
return;
}
if(r <= nodes[pos * 2].r)
insert(l, r, pos * 2);
else if(l >= nodes[pos * 2 + 1].l)
insert(l, r, pos * 2 + 1);
else
{
insert(l, nodes[pos * 2].r, pos * 2);
insert(nodes[pos * 2 + 1].l, r, pos * 2 + 1);
}
nodes[pos].full = nodes[pos * 2].full & nodes[pos * 2 + 1].full;
};
int main()
{
int caseCnt;
scanf("%d", &caseCnt);
int posterCnt;
while(caseCnt--)
{
cin >> posterCnt;
for(int i = 0; i < posterCnt; i++)
{
scanf("%d%d", &post[i * 2].pos, &post[i * 2 + 1].pos);
post[i * 2].num = post[i * 2 + 1].num = i;
}
sort(post, post + posterCnt * 2, cmp1);
int pre = 0;
int k = 0;
for(int i = 0; i < posterCnt * 2; i++)
{
if(post[i].pos != pre)
{
pre = post[i].pos;
post[i].pos = ++k;
}
else
post[i].pos = k;
}
sort(post, post + posterCnt * 2, cmp2);
build(1, k, 1);
int totalCount = 0;
for(int i = 0; i < posterCnt; i++)
{
flag = false;
insert(post[i * 2].pos, post[i * 2 + 1].pos, 1);
if(flag)
totalCount++;
}
printf("%d\n", totalCount);
}
return 0;
}
POJ 2528
最新推荐文章于 2020-01-12 17:03:04 发布