思路:
差分对区间进行 O ( n ) O(n) O(n)处理标记区间,分块取出每一块的数量和边界 r r r,对每一块的数量进行排序,二分出符合x询问的边界值,建立线段树对该区间进行最大值询问。
注意:写线段树时,注意 n = 0 n=0 n=0 的情况
参考代码:
/*
* @Author: vain
* @Date: 2020-08-26 11:58:46
* @LastEditTime: 2020-09-06 10:33:18
* @LastEditors: sueRimn
* @Description: In User Settings Edit
* @FilePath: \main\demo.cpp
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
//#define ll long long
typedef unsigned long long ull;
const ll N = 2e3 + 20;
const ll maxn = 1e5 + 20;
const ll mod = 1e9 + 7;
ll head[maxn], stac[maxn];
int a[maxn], b[maxn], c[maxn];
//typedef pair p;
//priority_queue, greater
> m;
//ll sum[maxn];
int max(int a, int b) {
return a > b ? a : b; }
ll min(ll a, ll b) {
return a < b ? a : b; }
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
return a * b / gcd(a, b); }
void swap(ll &x, ll &y) {
x ^= y, y ^= x, x ^= y; }
map<string, ll> pll, che, mp;
ll f[N][3], q[N];
int lowbit(int x) {
return (x) & (-x); }
vector<int> fc;
//ull h[maxn], p[maxn];
const ull base = 13331;
//char s1[maxn];
ll ksm(ll a, ll b)
{
ll res = 1;
while (b)
{
if (b & 1)
res = a * res;
b >>= 1;
a = a * a;
}
return res;
}
struct node
{
int r, sizx;
} p[maxn];
bool cmp(node x, node y)
{
return x.sizx < y.sizx;
}
struct vain
{
int l, r, ma;
} tr[maxn << 2];
int pushup(int k)
{
tr[k].ma = max(tr[k << 1].ma, tr[k << 1 | 1].ma);
}
void build(int k, int l, int r)
{
tr[k].l = l, tr[k].r = r;
if (l == r)
{
tr[k].ma = p[l].r;
return;
}
int mid = l + r >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
pushup(k);
}
int query(int k, int ql, int qr)
{
if (tr[k].l >= ql && tr[k].r <= qr)
{
return tr[k].ma;
}
int mid = tr[k].l + tr[k].r >> 1;
int ans = 0;
if (ql <= mid)
{
ans = max(ans, query(k << 1, ql, qr));
}
if (qr > mid)
{
ans = max(ans, query(k << 1 | 1, ql, qr));
}
return ans;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0), cout.tie(0);
// srand(time(NULL));
int n, m, t, s, q;
scanf("%d", &t);
while (t--)
{
fc.clear();
scanf("%d %d %d", &n, &m, &q);
for (int i = 0; i <= m; i++)
a[i] = 0;
while (n--)
{
int l, r;
scanf("%d %d", &l, &r);
a[l]--;
a[r + 1]++;
}
for (int i = 1; i <= m; i++)
a[i] = a[i] + a[i - 1];
int cnt = 0, ans = 0;
for (int i = 1; i <= m; i++)
{
if (!a[i])
ans++;
else if (ans)
p[++cnt].sizx = ans, p[cnt].r = i - 1, ans = 0;
}
if (ans)
p[++cnt].sizx = ans, p[cnt].r = m;
sort(p + 1, p + 1 + cnt, cmp);
for (int i = 1; i <= cnt; i++)
fc.push_back(p[i].sizx);
if (cnt > 0)
build(1, 1, cnt);
while (q--)
{
int x;
scanf("%d", &x);
if (x > p[cnt].sizx)
printf("-1 -1\n");
else
{
int y = lower_bound(fc.begin(), fc.end(), x) - fc.begin() + 1;
int qr = query(1, y, cnt);
printf("%d %d\n", qr - x + 1, qr);
}
}
}
}