https://www.luogu.com.cn/problem/P3963
#define mid ((l+r)>>1)
const int N = 200010;
struct node
{
int cj, jj;
}a[N];
int b[N];
int cmp(node a, node b)
{
return a.cj < b.cj;
}
int ls[N * 40], rs[N * 40], sum[N * 40], ans[N * 40], root[N], idx;
void build(int& u, int l, int r)
{
u = ++idx;
if (l == r) return;
build(ls[u], l, mid);
build(rs[u], mid + 1, r);
}
void push_up(int u)
{
sum[u] = sum[ls[u]] + sum[rs[u]];
ans[u] = ans[ls[u]] + ans[rs[u]];
}
void add(int& u, int v, int l, int r, int p, int k)
{
u = ++idx;
ls[u] = ls[v];
rs[u] = rs[v];
sum[u] = sum[v];
ans[u] = ans[v];
if (l == r)
{
sum[u]++;
ans[u] += k;
return;
}
if (p <= mid) add(ls[u], ls[v], l, mid, p, k);
else add(rs[u], rs[v], mid + 1, r, p, k);
push_up(u);
}
ll find(int u, int v, int l, int r, int k)
{
ll res = 0;
if (l == r) return k * b[l]; //叶子节点l存的是这个数离散化后的值,但是到了叶子节点的时候k值不一定为1,因为l的sum(数字个数)值不一定为1
int s = sum[ls[u]] - sum[ls[v]]; //所以要乘k,b[l]是原数组的值
if (k <= s) res += find(ls[u], ls[v], l, mid, k);
else
{
res += ans[ls[u]] - ans[ls[v]];
res += find(rs[u], rs[v], mid + 1, r, k - s);
}
return res;
}
void solve()
{
int n, c, f;
cin >> n >> c >> f;
for (int i = 1; i <= c; i++) cin >> a[i].cj >> a[i].jj, b[i] = a[i].jj;
sort(a + 1, a + 1 + c, cmp);
sort(b + 1, b + 1 + c);
int len = unique(b + 1, b + 1 + c) - b - 1;
build(root[0], 1, len);
for (int i = 1; i <= c; i++)
{
int x = lower_bound(b + 1, b + 1 + len, a[i].jj) - b;
add(root[i], root[i-1], 1, len, x, a[i].jj);
}
int k = n / 2;
int ok = 0;
for (int i = c - k; i >= 1 + k; i--)
{
ll cnty = find(root[c], root[i], 1, len, k);
ll cntz = find(root[i - 1], root[0], 1, len, k);
if (cntz + cnty + a[i].jj <= f)
{
cout << a[i].cj << '\n';
ok = 1;
break;
}
}
if (ok == 0) cout << "-1" << '\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}