#include<bits/stdc++.h>
using namespace std;
const int N = 7e5 + 3;
int n, k;
struct ok2
{
long long A;
long long B;
int cs;
};
ok2 a[N];
int pos[N][3];
int b[N];
int pos2[N];
vector < pair < int, int > > num;
pair < int, int > roirac[N];
struct ok
{
int lo;
int hi;
int pos;
int cnt;
};
ok Node[4 * N], Sum[4 * N];
void build(int id, int l, int r, ok Node[])
{
Node[id].lo = l; Node[id].hi = r;
if (l == r) return;
int mid = (l + r) / 2;
build(id * 2,l, mid, Node);
build(id * 2 + 1, mid + 1, r, Node);
}
void update(int id, int val, int pos, ok Node[])
{
if (Node[id].lo > pos || Node[id].hi < pos) return;
if (Node[id].lo == Node[id].hi)
{
Node[id].pos = val;
Node[id].cnt = 1;
return;
}
update(id * 2, val, pos, Node);
update(id * 2 + 1, val, pos, Node);
Node[id].pos = max(Node[id * 2].pos, Node[id * 2 + 1].pos);
Node[id].cnt = Node[id * 2].cnt + Node[id * 2 + 1].cnt;
}
int getpos(int id, int l, int r)
{
if (l > r) return 0;
if (Node[id].lo > r || Node[id].hi < l) return 0;
if (l <= Node[id].lo && Node[id].hi <= r)
return Node[id].pos;
return max(getpos(id * 2,l,r), getpos(id * 2 + 1,l,r));
}
int getsum(int id, int pos)
{
if (Sum[id].hi < pos) return 0;
if (Sum[id].lo >= pos)
return Sum[id].cnt;
return getsum(id * 2, pos) + getsum(id * 2 + 1, pos);
}
bool cmp(ok2 a, ok2 b)
{
return max(a.A, a.B) > max(b.A, b.B);
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("t.inp","r",stdin); freopen("t.out","w",stdout);
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i].A >> a[i].B;
num.push_back({a[i].A,i});
num.push_back({a[i].B,i});
a[i].cs = i;
}
for (int i = 1; i <= k; i++)
{
cin >> b[i];
num.push_back({b[i],i + n});
roirac[i].first = b[i];
roirac[i].second = i;
}
sort(num.begin(), num.end());
int m = num.size();
int dem = 1;
for (int i = 0; i < m; i++)
{
int cur = num[i].first;
int vt = num[i].second;
if (i >= 1 && cur != num[i - 1].first) dem++;
if (vt <= n)
{
if (pos[vt][1]) pos[vt][2] = dem;
else
if (pos[vt][2]) pos[vt][1] = dem;
else
if (a[vt].A > a[vt].B) pos[vt][2] = dem;
else pos[vt][1] = dem;
}
else
pos2[vt - n] = dem;
}
build(1,1,k,Sum);
build(1,1,m,Node);
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= k; i++)
update(1,i,pos2[i],Node);
sort(roirac + 1, roirac + k + 1);
int lo = k;
long long res = 0;
// for (int i = 1; i <= n; i++)
// {
// cout << a[i].A << " " << a[i].B << '\n';
// }
for (int i = 1; i <= n; i++)
{
while (lo && roirac[lo].first >= max(a[i].A, a[i].B))
{
update(1,1,roirac[lo].second,Sum);
lo--;
}
int vt = getpos(1, min(pos[a[i].cs][1], pos[a[i].cs][2]), max(pos[a[i].cs][1], pos[a[i].cs][2]) - 1);
int bonus = 0;
if (vt && a[i].A < a[i].B) bonus = 1;
int to = getsum(1, vt + 1);
//cout << getsum(1,10) << '\n';
//cout << min(pos[a[i].cs][1], pos[a[i].cs][2]) << " " << max(pos[a[i].cs][1], pos[a[i].cs][2]) - 1 << ' ';
//cout << bonus << " " << vt << " " << to << ' ';
if ((to + bonus) % 2)
{
res += a[i].B;
//cout << a[i].B << '\n';
}
else
{
res += a[i].A;
//cout << a[i].A << '\n';
}
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
21 ms |
2816 KB |
Output is correct |
15 |
Correct |
45 ms |
5116 KB |
Output is correct |
16 |
Correct |
73 ms |
7928 KB |
Output is correct |
17 |
Correct |
100 ms |
9708 KB |
Output is correct |
18 |
Correct |
99 ms |
9740 KB |
Output is correct |
19 |
Correct |
93 ms |
9712 KB |
Output is correct |
20 |
Correct |
96 ms |
9720 KB |
Output is correct |
21 |
Correct |
80 ms |
9712 KB |
Output is correct |
22 |
Correct |
76 ms |
9720 KB |
Output is correct |
23 |
Correct |
74 ms |
9716 KB |
Output is correct |
24 |
Correct |
82 ms |
9720 KB |
Output is correct |
25 |
Correct |
79 ms |
9708 KB |
Output is correct |
26 |
Correct |
80 ms |
9328 KB |
Output is correct |
27 |
Correct |
87 ms |
9588 KB |
Output is correct |
28 |
Correct |
93 ms |
9712 KB |
Output is correct |
29 |
Correct |
84 ms |
9720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
21 ms |
2816 KB |
Output is correct |
15 |
Correct |
45 ms |
5116 KB |
Output is correct |
16 |
Correct |
73 ms |
7928 KB |
Output is correct |
17 |
Correct |
100 ms |
9708 KB |
Output is correct |
18 |
Correct |
99 ms |
9740 KB |
Output is correct |
19 |
Correct |
93 ms |
9712 KB |
Output is correct |
20 |
Correct |
96 ms |
9720 KB |
Output is correct |
21 |
Correct |
80 ms |
9712 KB |
Output is correct |
22 |
Correct |
76 ms |
9720 KB |
Output is correct |
23 |
Correct |
74 ms |
9716 KB |
Output is correct |
24 |
Correct |
82 ms |
9720 KB |
Output is correct |
25 |
Correct |
79 ms |
9708 KB |
Output is correct |
26 |
Correct |
80 ms |
9328 KB |
Output is correct |
27 |
Correct |
87 ms |
9588 KB |
Output is correct |
28 |
Correct |
93 ms |
9712 KB |
Output is correct |
29 |
Correct |
84 ms |
9720 KB |
Output is correct |
30 |
Correct |
287 ms |
22152 KB |
Output is correct |
31 |
Correct |
370 ms |
32352 KB |
Output is correct |
32 |
Correct |
465 ms |
34912 KB |
Output is correct |
33 |
Correct |
666 ms |
56408 KB |
Output is correct |
34 |
Correct |
192 ms |
21608 KB |
Output is correct |
35 |
Correct |
675 ms |
56404 KB |
Output is correct |
36 |
Correct |
679 ms |
56404 KB |
Output is correct |
37 |
Correct |
675 ms |
56404 KB |
Output is correct |
38 |
Correct |
614 ms |
56404 KB |
Output is correct |
39 |
Correct |
631 ms |
56408 KB |
Output is correct |
40 |
Correct |
503 ms |
56536 KB |
Output is correct |
41 |
Correct |
585 ms |
56384 KB |
Output is correct |
42 |
Correct |
606 ms |
56536 KB |
Output is correct |
43 |
Correct |
378 ms |
56404 KB |
Output is correct |
44 |
Correct |
359 ms |
56408 KB |
Output is correct |
45 |
Correct |
372 ms |
56464 KB |
Output is correct |
46 |
Correct |
459 ms |
56404 KB |
Output is correct |
47 |
Correct |
462 ms |
56532 KB |
Output is correct |
48 |
Correct |
530 ms |
56536 KB |
Output is correct |
49 |
Correct |
506 ms |
56404 KB |
Output is correct |