#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
#define f(i,a,b) for(int i = a; i < b; i++)
#define fa(i,a,b) for(int i = a; i >= b; i--)
#define ff first
#define ss second
const int N = 2e5 + 5;
const ll inf = 1e17 + 100;
int n, k, a[N], b[N], t[N];
int st[3*N][20], root[3*N], id[3*N];
map <int,int> m;
set <int> s;
vector <int> adj[3*N], L, R, value;
int maxi(int l, int r){
int log = 31 - __builtin_clz(r-l+1);
return max(st[l][log], st[r - (1<<log) + 1][log]);
}
int createleaf(int x){
L.push_back(-1), R.push_back(-1);
value.push_back(x);
return L.size() - 1;
}
int createvertex(int u, int v){
L.push_back(u), R.push_back(v);
value.push_back(value[u] + value[v]);
return L.size() - 1;
}
int build(int l, int r){
if(l == r) return createleaf(0);
int m = (l+r)>>1;
return createvertex(build(l, m), build(m+1, r));
}
int upd(int id, int l, int r, int x, int val){
if(l == r) return createleaf(val);
int m = (l+r)>>1;
if(x <= m) return createvertex(upd(L[id], l, m, x, val), R[id]);
return createvertex(L[id], upd(R[id], m+1, r, x, val));
}
int get(int id, int l, int r, int x, int y){
if(r < x or y < l) return 0;
if(x <= l and r <= y) return value[id];
int m = (l+r)>>1;
return get(L[id], l, m, x, y) + get(R[id], m+1, r, x, y);
}
int main(){
cin >> n >> k;
f(i,1,n+1) {
cin >> a[i] >> b[i];
s.insert(a[i]), s.insert(b[i]);
}
f(i,1,k+1) {
cin >> t[i];
s.insert(t[i]);
}
int ID = 0;
for(int x: s) m[x] = ++ID;
f(i,1,k+1) {
t[i] = m[t[i]];
adj[t[i]].push_back(i);
}
f(i,1,k+1) id[t[i]] = i;
f(i,1,ID+1) st[i][0] = id[i];
f(r,1,20){
f(i,1,ID+1){
int x = i + (1<<(r-1));
if(x > ID) break;
st[i][r] = max(st[i][r-1], st[x][r-1]);
}
}
root[ID+1] = build(0, k);
fa(i,ID,0){
root[i] = root[i+1];
for(int x: adj[i]) root[i] = upd(root[i], 0, k, x, 1);
}
ll ans = 0;
f(i,1,n+1){
if(a[i] == b[i]){
ans += (ll) a[i];
continue;
}
int xi = m[a[i]], yi = m[b[i]], x, y;
x = min(xi, yi), y = max(xi, yi);
int iq = maxi(x, y-1), s;
if(iq == k) s = 0;
else s = get(root[y], 0, k, iq+1, k);
if(iq == 0){
if(s&1) ans += (ll) b[i];
else ans += (ll) a[i];
}
else{
if(s&1) ans += (ll) min(a[i], b[i]);
else ans += (ll) max(a[i], b[i]);
}
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14832 KB |
Output is correct |
2 |
Correct |
9 ms |
14972 KB |
Output is correct |
3 |
Correct |
11 ms |
15188 KB |
Output is correct |
4 |
Correct |
12 ms |
15180 KB |
Output is correct |
5 |
Correct |
11 ms |
15180 KB |
Output is correct |
6 |
Correct |
14 ms |
15180 KB |
Output is correct |
7 |
Correct |
12 ms |
15180 KB |
Output is correct |
8 |
Correct |
12 ms |
15188 KB |
Output is correct |
9 |
Correct |
10 ms |
15188 KB |
Output is correct |
10 |
Correct |
10 ms |
14920 KB |
Output is correct |
11 |
Correct |
11 ms |
14988 KB |
Output is correct |
12 |
Correct |
10 ms |
15080 KB |
Output is correct |
13 |
Correct |
11 ms |
15052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14832 KB |
Output is correct |
2 |
Correct |
9 ms |
14972 KB |
Output is correct |
3 |
Correct |
11 ms |
15188 KB |
Output is correct |
4 |
Correct |
12 ms |
15180 KB |
Output is correct |
5 |
Correct |
11 ms |
15180 KB |
Output is correct |
6 |
Correct |
14 ms |
15180 KB |
Output is correct |
7 |
Correct |
12 ms |
15180 KB |
Output is correct |
8 |
Correct |
12 ms |
15188 KB |
Output is correct |
9 |
Correct |
10 ms |
15188 KB |
Output is correct |
10 |
Correct |
10 ms |
14920 KB |
Output is correct |
11 |
Correct |
11 ms |
14988 KB |
Output is correct |
12 |
Correct |
10 ms |
15080 KB |
Output is correct |
13 |
Correct |
11 ms |
15052 KB |
Output is correct |
14 |
Correct |
63 ms |
22840 KB |
Output is correct |
15 |
Correct |
110 ms |
31004 KB |
Output is correct |
16 |
Correct |
162 ms |
41048 KB |
Output is correct |
17 |
Correct |
253 ms |
47660 KB |
Output is correct |
18 |
Correct |
221 ms |
47628 KB |
Output is correct |
19 |
Correct |
218 ms |
47580 KB |
Output is correct |
20 |
Correct |
263 ms |
47760 KB |
Output is correct |
21 |
Correct |
193 ms |
47584 KB |
Output is correct |
22 |
Correct |
142 ms |
39936 KB |
Output is correct |
23 |
Correct |
142 ms |
36208 KB |
Output is correct |
24 |
Correct |
108 ms |
33432 KB |
Output is correct |
25 |
Correct |
145 ms |
41904 KB |
Output is correct |
26 |
Correct |
173 ms |
39140 KB |
Output is correct |
27 |
Correct |
179 ms |
40576 KB |
Output is correct |
28 |
Correct |
171 ms |
40616 KB |
Output is correct |
29 |
Correct |
227 ms |
44088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14832 KB |
Output is correct |
2 |
Correct |
9 ms |
14972 KB |
Output is correct |
3 |
Correct |
11 ms |
15188 KB |
Output is correct |
4 |
Correct |
12 ms |
15180 KB |
Output is correct |
5 |
Correct |
11 ms |
15180 KB |
Output is correct |
6 |
Correct |
14 ms |
15180 KB |
Output is correct |
7 |
Correct |
12 ms |
15180 KB |
Output is correct |
8 |
Correct |
12 ms |
15188 KB |
Output is correct |
9 |
Correct |
10 ms |
15188 KB |
Output is correct |
10 |
Correct |
10 ms |
14920 KB |
Output is correct |
11 |
Correct |
11 ms |
14988 KB |
Output is correct |
12 |
Correct |
10 ms |
15080 KB |
Output is correct |
13 |
Correct |
11 ms |
15052 KB |
Output is correct |
14 |
Correct |
63 ms |
22840 KB |
Output is correct |
15 |
Correct |
110 ms |
31004 KB |
Output is correct |
16 |
Correct |
162 ms |
41048 KB |
Output is correct |
17 |
Correct |
253 ms |
47660 KB |
Output is correct |
18 |
Correct |
221 ms |
47628 KB |
Output is correct |
19 |
Correct |
218 ms |
47580 KB |
Output is correct |
20 |
Correct |
263 ms |
47760 KB |
Output is correct |
21 |
Correct |
193 ms |
47584 KB |
Output is correct |
22 |
Correct |
142 ms |
39936 KB |
Output is correct |
23 |
Correct |
142 ms |
36208 KB |
Output is correct |
24 |
Correct |
108 ms |
33432 KB |
Output is correct |
25 |
Correct |
145 ms |
41904 KB |
Output is correct |
26 |
Correct |
173 ms |
39140 KB |
Output is correct |
27 |
Correct |
179 ms |
40576 KB |
Output is correct |
28 |
Correct |
171 ms |
40616 KB |
Output is correct |
29 |
Correct |
227 ms |
44088 KB |
Output is correct |
30 |
Correct |
730 ms |
112080 KB |
Output is correct |
31 |
Correct |
984 ms |
127604 KB |
Output is correct |
32 |
Correct |
1324 ms |
147000 KB |
Output is correct |
33 |
Correct |
2169 ms |
185612 KB |
Output is correct |
34 |
Correct |
740 ms |
108160 KB |
Output is correct |
35 |
Correct |
2044 ms |
185600 KB |
Output is correct |
36 |
Correct |
1890 ms |
185600 KB |
Output is correct |
37 |
Correct |
1874 ms |
185656 KB |
Output is correct |
38 |
Correct |
1895 ms |
185644 KB |
Output is correct |
39 |
Correct |
1820 ms |
185584 KB |
Output is correct |
40 |
Correct |
1562 ms |
185276 KB |
Output is correct |
41 |
Correct |
1806 ms |
185656 KB |
Output is correct |
42 |
Correct |
1830 ms |
185564 KB |
Output is correct |
43 |
Correct |
1060 ms |
183696 KB |
Output is correct |
44 |
Correct |
1022 ms |
183944 KB |
Output is correct |
45 |
Correct |
986 ms |
184268 KB |
Output is correct |
46 |
Correct |
872 ms |
128080 KB |
Output is correct |
47 |
Correct |
762 ms |
109356 KB |
Output is correct |
48 |
Correct |
1175 ms |
149616 KB |
Output is correct |
49 |
Correct |
1091 ms |
149668 KB |
Output is correct |