题目大意
你有个区间, 你要恰好删掉一个区间,使得剩下的个区间的并的总和最多
的并是
题目分析
首先我们将给定的区间进行离散化
由于区间端点相邻的区间并不算相交,所以离散化时要进行特殊处理
然后本题就变成了这样一个问题:
1.对于所有, 把对应区间的数值全部加上1,并统计此时所有区间并的个数
2.对于每个, 求出该区间内满足的连续段个数,并统计最大值
那么,即为答案
对于步骤,可以用差分求出;对于步骤, 可以用前缀和求出(注意特判开头和结尾相等的情况)
代码
#include <bits/stdc++.h>
#define R register
#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)
const int MaxN = 1e6 + 10;
std::map<int, int> m1, m2;
int n, cnt, l[MaxN], r[MaxN], a[MaxN], s[MaxN];
inline void prework()
{
m1.clear(), m2.clear();
for (int i = 1; i <= n; i++)
m1[l[i]] = m1[r[i]] = 1;
cnt = 0;
for (std::map<int, int>::iterator it = m1.begin(); it != m1.end(); it++)
m2[it->first] = ++cnt;
for (int i = 1; i <= n; i++)
l[i] = m2[l[i]] * 2 - 1, r[i] = m2[r[i]] * 2 - 1;
}
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0')
{
if (ch == '-')
f = 0;
ch = getchar();
}
while (ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? x : (-x);
}
int main()
{
int T = read();
while (T--)
{
n = read(), cnt = 0;
for (int i = 1; i <= n; i++)
l[i] = read(), r[i] = read();
prework(), cnt = cnt * 2 - 1;
for (int i = 1; i <= n; i++)
a[l[i]]++, a[r[i] + 1]--;
for (int i = 1; i <= cnt; i++)
a[i] += a[i - 1];
int num = 0, ans = -1000000;
for (int i = 1; i <= cnt; i++)
{
s[i] = s[i - 1];
num += (a[i] && !a[i - 1]);
if (a[i] > 1 && a[i - 1] <= 1)
++s[i];
}
for (int i = 1; i <= n; i++)
{
int cur = s[r[i]] - s[l[i] - 1] + ((a[l[i]] > 1) && (a[l[i] - 1] > 1)) - 1;
ans = std::max(ans, cur);
}
printf("%d\n", num + ans);
for (int i = 0; i <= cnt * 2; i++)
a[i] = s[i] = 0;
}
return 0;
}