Welcome Party

题目链接

题意

有$n$个人,每个人都有两个属性,现在要选出若干个人使用第一属性,其余人使用第二属性。使第一属性最大值和第二属性的最大值差的绝对值最小。

思路

很容易想到枚举每一个人作为第一属性的能力值最高的人,那么那些第一属性比他高的人都去发挥第二属性,我们维护这批人的第二属性最大值(因为这批人只会往里面添加,不会减少)。我们还可以在其余人里选出一些发挥第二属性,很容易发现选第二属性接近我们当前枚举的人的第一属性的人。
那么实现上就只需要一个能维护插入元素的前驱和后继并快速删除的数据结构。
如,treap,pbds,splay等。
牢牢记住这些东西删除重复元素是全部删除的!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#ifndef ONLINE_JUDGE
#define dbg(x...) do{cout << "\033[33;1m" << #x << "->" ; err(x);} while (0)
void err(){cout << "\033[39;0m" << endl;}
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x){for (auto v: a) cout << v << ' '; err(x...);}
template<typename T, typename... A>
void err(T a, A... x){cout << a << ' '; err(x...);}
#else
#define dbg(...)
#endif
const int N = 1e5 + 5;
namespace Treap
{
#define ls(x) treap[x].child[0]
#define rs(x) treap[x].child[1]
struct node
{
int child[2], size;
ll val;
unsigned key;
}treap[N];
/*
ll seed = 17124057;
ll Rand()
{
return seed = seed * 48271 % 2147483647;
}
*/
mt19937 rd(time(0));
unsigned Rand[20];

void rand_init()
{
for (int i = 0; i < 20; i++)
Rand[i] = rd();
}

int cur;
inline void init(int &rt)
{
rt = cur = 0;
rand_init();
ls(0) = rs(0) = 0;
treap[0].size = 0;
treap[0].val = 0;
}
void dfs(int root)
{
dbg(root, ls(root), rs(root));
if (ls(root))
dfs(ls(root));
if (rs(root))
dfs(rs(root));
}

inline void pushup(int root)
{
treap[root].size = treap[ls(root)].size + treap[rs(root)].size + 1;
}
void split(int root, int &x, int &y, ll v)
{
if (!root)
{
x = 0;
y = 0;
return;
}
if (treap[root].val <= v)
{
x = root;
split(rs(root), rs(x), y, v);
}
else
{
y = root;
split(ls(root), x, ls(y), v);
}
pushup(root);
}
void merge(int &root, int x, int y)
{
if (!x || !y)
{
root = x + y;
return;
}
if (treap[x].key <= treap[y].key)
{
root = x;
merge(rs(root), rs(x), y);
}
else
{
root = y;
merge(ls(root), x, ls(y));
}
pushup(root);
}
void split_by_rank(int root, int &x, int &y, int k)
{
//dbg(root, k);
if (!root)
{
x = y = 0;
return;
}
if (treap[ls(root)].size + 1 <= k)
{
x = root;
split_by_rank(rs(root), rs(x), y, k - treap[ls(root)].size - 1);
}
else
{
y = root;
split_by_rank(ls(root), x, ls(y), k);
}
pushup(root);
}

inline unsigned get_rd(int id)
{
unsigned now = 0;
for (int i = 0; i < 20; i++)
if (id & (1 << i))
now ^= Rand[i];
return now;
}

inline void insert(int &root, ll v)
{
int x = 0, y = 0, z = ++cur;
treap[z].val = v;
ls(z) = rs(z) = 0;
treap[z].size = 1;
treap[z].key = get_rd(cur);
split(root, x, y, v);
merge(x, x, z);
merge(root, x, y);
}
inline void del(int &root, ll v)
{
//dbg("del which val is ", v);
int x = 0, y = 0, z = 0;
split(root, x, y, v);
int rk = treap[x].size;
split_by_rank(x, x, z, rk - 1);
//dbg(z, treap[z].size, treap[z].val);
assert(treap[z].size == 1);
//dfs(x);
//dfs(y);
//dfs(z);
merge(root, x, y);
}
inline pair<int, int> lower_bound(int &root, ll v)
{
//dbg("ask near to ", v);
int x = 0, y = 0;
split(root, x, y, v - 1);
int now = y;
while (ls(now))
now = ls(now);
int nnow = x;
while (rs(nnow))
nnow = rs(nnow);
merge(root, x, y);
// dbg(nnow, now, treap[nnow].val, treap[now].val);
return make_pair(nnow, now);
}
int rt;
} //namespace Treap

struct Node
{
ll x, y;
}arr[N];
bool cmp(Node x, Node y)
{
if (x.x == y.x)
return x.y < y.y;
return x.x > y.x;
}
/*
int main()
{
Treap::init(Treap::rt);
Treap::insert(Treap::rt, 14);
Treap::insert(Treap::rt, 87);
Treap::insert(Treap::rt, 9);
Treap::insert(Treap::rt, 47);
Treap::insert(Treap::rt, 36);
Treap::dfs(Treap::rt);
Treap::del(Treap::rt, 14);
Treap::del(Treap::rt, 87);
return 0;
}
*/
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld%lld", &arr[i].x, &arr[i].y);
arr[i].x += 1;
arr[i].y += 1;
}
sort(arr + 1, arr + n + 1, cmp);
Treap::init(Treap::rt);
ll maxi = -5e18;
for (int i = 1; i <= n; i++)
{
Treap::insert(Treap::rt, arr[i].y);
//dbg(i, arr[i].x, arr[i].y);
}
ll ans = 5e18;
for (int i = 1; i <= n; i++)
{
Treap::del(Treap::rt, arr[i].y);
if (maxi >= arr[i].x)
{
ans = min(ans, maxi - arr[i].x);
}
else{
pair<int, int> p = Treap::lower_bound(Treap::rt, arr[i].x);
int pre = p.first, suf = p.second;
//dbg(pre, suf, Treap::treap[pre].val, Treap::treap[suf].val, maxi);
if (suf)
ans = min(ans, abs(max(maxi, Treap::treap[suf].val) - arr[i].x));
if (pre)
{
ans = min(ans, abs(max(maxi, Treap::treap[pre].val) - arr[i].x));
}
//dbg(ans);
ans=min(ans,abs(maxi-arr[i].x));
}
maxi=max(maxi,arr[i].y);
}
printf("%lld\n",ans);
}
return 0;
}

0%