/* IN THE NAME OF GOD */
/* |\/| /-\ |\| | |\/| /-\ */
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int)x.size()
#define endl '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define mr make_pair
//#define int long long
#define pii pair<int, int>
typedef long double ld;
typedef long long ll;
mt19937 rng (chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 1e6 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9 + 1;
int a[N], b[N], x[N], seg[N * 4], wh[N * 4], fen[N], lst[N];
vector<int> vec, V[N];
vector<pii> t[N];
void bulid(int id, int s, int e){
if(s == e){
if(t[s].empty())
seg[id] = -1;
else{
seg[id] = t[s].back().F;
wh[id] = t[s].back().S;
t[s].pop_back();
}
return;
}
int mid = (s + e) / 2;
bulid(id * 2, s, mid);
bulid(id * 2 + 1, mid + 1, e);
if(seg[id * 2] > seg[id * 2 + 1]){
seg[id] = seg[id * 2];
wh[id] = wh[id * 2];
}
else{
seg[id] = seg[id * 2 + 1];
wh[id] = wh[id * 2 + 1];
}
}
void upd(int id, int s, int e, int p){
if(s > p || p > e)
return;
if(s == e){
if(t[s].empty())
seg[id] = -1;
else{
seg[id] = t[s].back().F;
wh[id] = t[s].back().S;
t[s].pop_back();
}
return;
}
int mid = (s + e) / 2;
upd(id * 2, s, mid, p);
upd(id * 2 + 1, mid + 1, e, p);
if(seg[id * 2] > seg[id * 2 + 1]){
seg[id] = seg[id * 2];
wh[id] = wh[id * 2];
}
else{
seg[id] = seg[id * 2 + 1];
wh[id] = wh[id * 2 + 1];
}
}
pii get(int id, int s, int e, int l, int r){
if(s > r || e < l)
return mr(-1, -1);
if(l <= s && e <= r)
return mr(seg[id], wh[id]);
int mid = (s + e) / 2;
pii p = get(id * 2, s, mid, l, r), q = get(id * 2 + 1, mid + 1, e, l, r);
return max(p, q);
}
void add(int p, int x){
for(; p < N; p += p & -p)
fen[p] += x;
}
int fget(int p){
int ret = 0;
for(; p > 0; p -= p & -p)
ret += fen[p];
return ret;
}
int f(int x){
return lower_bound(all(vec), x) - vec.begin() + 1;
}
int32_t main(){
ios_base:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i] >> b[i];
vec.pb(a[i]);
vec.pb(b[i]);
}
for(int i = 1; i <= k; i++){
cin >> x[i];
vec.pb(x[i]);
}
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
for(int i = 1; i <= n; i++)
t[f(min(a[i], b[i]))].pb(mr(max(a[i], b[i]), i));
for(int i = 1; i < N; i++)
sort(all(t[i]));
bulid(1, 1, N - 1);
for(int i = k; i >= 1; i--){
while(1){
pii p = get(1, 1, N - 1, 1, f(x[i]));
if(p.F <= x[i])
break;
lst[p.S] = i;
upd(1, 1, N - 1, f(min(a[p.S], b[p.S])));
}
}
for(int i = 1; i <= n; i++)
V[lst[i]].pb(i);
ll ans = 0;
for(int i = k; i >= 0; i--){
if(i != 0){
add(1, 1);
add(f(x[i]) + 1, -1);
}
for(int v : V[i]){
int r = fget(f(max(b[v], a[v])));
if(i == 0){
if(r % 2)
ans += b[v];
else
ans += a[v];
}
else if(a[v] < b[v]){
if(r % 2)
ans += a[v];
else
ans += b[v];
}
else{
if(r % 2)
ans += b[v];
else
ans += a[v];
}
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
59732 KB |
Output is correct |
2 |
Correct |
37 ms |
59720 KB |
Output is correct |
3 |
Correct |
39 ms |
59700 KB |
Output is correct |
4 |
Correct |
40 ms |
59812 KB |
Output is correct |
5 |
Correct |
41 ms |
59764 KB |
Output is correct |
6 |
Correct |
42 ms |
59816 KB |
Output is correct |
7 |
Correct |
43 ms |
59724 KB |
Output is correct |
8 |
Correct |
38 ms |
59756 KB |
Output is correct |
9 |
Correct |
39 ms |
59724 KB |
Output is correct |
10 |
Correct |
39 ms |
59728 KB |
Output is correct |
11 |
Correct |
39 ms |
59756 KB |
Output is correct |
12 |
Correct |
39 ms |
59668 KB |
Output is correct |
13 |
Correct |
39 ms |
59756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
59732 KB |
Output is correct |
2 |
Correct |
37 ms |
59720 KB |
Output is correct |
3 |
Correct |
39 ms |
59700 KB |
Output is correct |
4 |
Correct |
40 ms |
59812 KB |
Output is correct |
5 |
Correct |
41 ms |
59764 KB |
Output is correct |
6 |
Correct |
42 ms |
59816 KB |
Output is correct |
7 |
Correct |
43 ms |
59724 KB |
Output is correct |
8 |
Correct |
38 ms |
59756 KB |
Output is correct |
9 |
Correct |
39 ms |
59724 KB |
Output is correct |
10 |
Correct |
39 ms |
59728 KB |
Output is correct |
11 |
Correct |
39 ms |
59756 KB |
Output is correct |
12 |
Correct |
39 ms |
59668 KB |
Output is correct |
13 |
Correct |
39 ms |
59756 KB |
Output is correct |
14 |
Correct |
54 ms |
60832 KB |
Output is correct |
15 |
Correct |
83 ms |
61984 KB |
Output is correct |
16 |
Correct |
97 ms |
63192 KB |
Output is correct |
17 |
Correct |
113 ms |
64296 KB |
Output is correct |
18 |
Correct |
114 ms |
64288 KB |
Output is correct |
19 |
Correct |
115 ms |
64236 KB |
Output is correct |
20 |
Correct |
114 ms |
64300 KB |
Output is correct |
21 |
Correct |
109 ms |
64200 KB |
Output is correct |
22 |
Correct |
89 ms |
63448 KB |
Output is correct |
23 |
Correct |
90 ms |
63324 KB |
Output is correct |
24 |
Correct |
90 ms |
63196 KB |
Output is correct |
25 |
Correct |
95 ms |
63656 KB |
Output is correct |
26 |
Correct |
89 ms |
62412 KB |
Output is correct |
27 |
Correct |
96 ms |
63252 KB |
Output is correct |
28 |
Correct |
95 ms |
63008 KB |
Output is correct |
29 |
Correct |
99 ms |
63868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
59732 KB |
Output is correct |
2 |
Correct |
37 ms |
59720 KB |
Output is correct |
3 |
Correct |
39 ms |
59700 KB |
Output is correct |
4 |
Correct |
40 ms |
59812 KB |
Output is correct |
5 |
Correct |
41 ms |
59764 KB |
Output is correct |
6 |
Correct |
42 ms |
59816 KB |
Output is correct |
7 |
Correct |
43 ms |
59724 KB |
Output is correct |
8 |
Correct |
38 ms |
59756 KB |
Output is correct |
9 |
Correct |
39 ms |
59724 KB |
Output is correct |
10 |
Correct |
39 ms |
59728 KB |
Output is correct |
11 |
Correct |
39 ms |
59756 KB |
Output is correct |
12 |
Correct |
39 ms |
59668 KB |
Output is correct |
13 |
Correct |
39 ms |
59756 KB |
Output is correct |
14 |
Correct |
54 ms |
60832 KB |
Output is correct |
15 |
Correct |
83 ms |
61984 KB |
Output is correct |
16 |
Correct |
97 ms |
63192 KB |
Output is correct |
17 |
Correct |
113 ms |
64296 KB |
Output is correct |
18 |
Correct |
114 ms |
64288 KB |
Output is correct |
19 |
Correct |
115 ms |
64236 KB |
Output is correct |
20 |
Correct |
114 ms |
64300 KB |
Output is correct |
21 |
Correct |
109 ms |
64200 KB |
Output is correct |
22 |
Correct |
89 ms |
63448 KB |
Output is correct |
23 |
Correct |
90 ms |
63324 KB |
Output is correct |
24 |
Correct |
90 ms |
63196 KB |
Output is correct |
25 |
Correct |
95 ms |
63656 KB |
Output is correct |
26 |
Correct |
89 ms |
62412 KB |
Output is correct |
27 |
Correct |
96 ms |
63252 KB |
Output is correct |
28 |
Correct |
95 ms |
63008 KB |
Output is correct |
29 |
Correct |
99 ms |
63868 KB |
Output is correct |
30 |
Correct |
200 ms |
65648 KB |
Output is correct |
31 |
Correct |
295 ms |
69440 KB |
Output is correct |
32 |
Correct |
337 ms |
73860 KB |
Output is correct |
33 |
Correct |
545 ms |
83208 KB |
Output is correct |
34 |
Correct |
184 ms |
64072 KB |
Output is correct |
35 |
Correct |
500 ms |
83176 KB |
Output is correct |
36 |
Correct |
531 ms |
83160 KB |
Output is correct |
37 |
Correct |
561 ms |
83216 KB |
Output is correct |
38 |
Correct |
530 ms |
82476 KB |
Output is correct |
39 |
Correct |
559 ms |
83112 KB |
Output is correct |
40 |
Correct |
516 ms |
81968 KB |
Output is correct |
41 |
Correct |
443 ms |
82936 KB |
Output is correct |
42 |
Correct |
503 ms |
82948 KB |
Output is correct |
43 |
Correct |
344 ms |
80564 KB |
Output is correct |
44 |
Correct |
331 ms |
80948 KB |
Output is correct |
45 |
Correct |
355 ms |
81140 KB |
Output is correct |
46 |
Correct |
330 ms |
78516 KB |
Output is correct |
47 |
Correct |
339 ms |
77804 KB |
Output is correct |
48 |
Correct |
349 ms |
75628 KB |
Output is correct |
49 |
Correct |
344 ms |
75884 KB |
Output is correct |