#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
void read(T &x)
{
x = 0;
register int c;
while ((c = getchar()) && (c > '9' || c < '0'))
;
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
}
constexpr bool typetest = 0;
constexpr int N = 5e5 + 5;
constexpr int Inf = 1e9 + 7;
int n, m, k;
struct SegmentTree
{
int st[N * 4], a[N], n;
SegmentTree()
{
fill_n(a, N, Inf);
}
void Build(int id, int l, int r)
{
if (l > r)
return;
if (l == r)
{
st[id] = l;
return;
}
Build(id << 1, l, (l + r) / 2);
Build(id << 1 | 1, (l + r) / 2 + 1, r);
st[id] = a[st[id << 1]] < a[st[id << 1 | 1]] ? st[id << 1] : st[id << 1 | 1];
}
void Build(int n)
{
this->n = n;
Build(1, 1, n);
}
void Update(int id, int l, int r, const int &x, const int &v)
{
if (l > x || r < x)
return;
if (l == x && r == x)
{
a[l] = v;
return;
}
Update(id << 1, l, (l + r) / 2, x, v);
Update(id << 1 | 1, (l + r) / 2 + 1, r, x, v);
st[id] = a[st[id << 1]] < a[st[id << 1 | 1]] ? st[id << 1] : st[id << 1 | 1];
}
void Update(int x, int v)
{
Update(1, 1, n, x, v);
}
int Get(int id, int l, int r, const int &u, const int &v)
{
if (l > v || r < u)
return 0;
if (l >= u && r <= v)
return st[id];
int x = Get(id << 1, l, (l + r) / 2, u, v),
y = Get(id << 1 | 1, (l + r) / 2 + 1, r, u, v);
return a[x] < a[y] ? x : y;
}
int Get(int l, int r)
{
return Get(1, 1, n, l, r);
}
} f;
struct Request
{
int v, d;
} a[N], b[N];
void Read()
{
cin >> n >> m >> k;
f.Build(n);
for (int i = 1; i <= n; ++i)
cin >> a[i].v >> a[i].d;
for (int i = 1; i <= m; ++i)
cin >> b[i].v >> b[i].d;
}
void Solve()
{
sort(a + 1, a + n + 1, [&](const Request &x, const Request &y)
{ return x.d < y.d || (x.d == y.d && x.v < y.v); });
sort(b + 1, b + m + 1, [&](const Request &x, const Request &y)
{ return x.v > y.v || (x.v == y.v && x.d < y.d); });
for (int i = 1; i <= n; ++i)
f.Update(i, a[i].v);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
ll ans(0);
for (int i = 1; i <= m; ++i)
{
int l = 1, mid, h = n;
while (l <= h)
{
mid = (l + h) / 2;
if (a[mid].d < b[i].d)
l = mid + 1;
else
h = mid - 1;
}
if (l > n)
continue;
int x = f.Get(l, n);
if (b[i].v - f.a[x] > 0 && ((int)q.size() < k || b[i].v - f.a[x] > q.top().first))
{
if ((int)q.size() == k)
{
f.Update(q.top().second, a[q.top().second].v);
ans -= q.top().first;
q.pop();
}
ans += b[i].v - f.a[x];
q.emplace(b[i].v - f.a[x], x);
f.Update(x, Inf);
}
}
cout << ans;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
//cout << "Case #" << _ << "\n";
Read();
Solve();
}
//cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
Compilation message
hot.cpp: In function 'void read(T&)':
hot.cpp:12:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
12 | register int c;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2280 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
5340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
8256 KB |
Output is correct |
2 |
Correct |
66 ms |
6248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
289 ms |
19384 KB |
Output is correct |
2 |
Correct |
170 ms |
9176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
542 ms |
29180 KB |
Output is correct |
2 |
Correct |
554 ms |
28032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
583 ms |
30552 KB |
Output is correct |
2 |
Correct |
808 ms |
41736 KB |
Output is correct |
3 |
Correct |
944 ms |
38864 KB |
Output is correct |