一开始的思路就是对的,但是排序搞错了,白wa了5发,大致思路就是按开始时间排序,然后遍历区间,将不相交的区间直接放入set里,相交的话,找结束时间最晚的不相交区间,令其结束时间为当前结束时间,然后重复模拟就好啦
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long ll;
#define inf 1000000000
#define mod 1000000007
#define maxn 106000
#define PI 3.1415926
#define lowbit(x) (x&-x)
#define eps 1e-9
struct node
{
ll l, r;
bool operator < (node b)const
{
return r < b.r;
}
bool operator == (node b)const
{
return r == b.r;
}
bool operator > (node b)const
{
return r > b.r;
}
}a[maxn], b[maxn];
int cnt;
bool comp(node a, node b)
{
if (a.l == b.l)
return a.r < b.r;
return a.l < b.l;
}
int main(void)
{
int T, n, i, j;
scanf("%d", &T);
while (T--)
{
cnt = 0;
multiset<node>s;
multiset<node>::iterator it;
scanf("%d", &n);
for (i = 1;i <= n;i++)
scanf("%lld%lld", &a[i].l, &a[i].r);
sort(a + 1, a + n + 1, comp);
for (i = 1;i <= n;i++)
{
if (i == 1)
{
b[++cnt] = a[i];
s.insert(b[cnt]);
}
else
{
node tmp;
tmp.l = a[i].r;
tmp.r = a[i].l;
it = s.upper_bound(tmp);
if (it == s.begin())
{
b[++cnt] = a[i];
s.insert(b[cnt]);
}
else
{
tmp = *(--it);
s.erase(it);
tmp.r = a[i].r;
s.insert(tmp);
}
}
}
ll ans = 0;
for (it = s.begin();it != s.end();it++)
ans += (*it).r - (*it).l;
printf("%d %lld\n", cnt, ans);
}
return 0;
}