#include <bits/stdc++.h>
#define fi first
#define se second
#define ii pair < int , int >
#define pb push_back
#define eb emplace_back
using namespace std;
int f[200010][20] , BIT[200010] , c[200010];
ii a[200010];
void upl(int x) {
for ( ; x > 0 ; x -= (-x & x))
BIT[x]++;
}
int dwl(int x) {
int tmp = 0;
for ( ; x <= 200000 ; x += (-x & x))
tmp += BIT[x];
return tmp;
}
int get(int l,int r) {
if (l > r) return 0;
int k = __lg(r - l + 1);
return max(f[l][k] , f[r - (1 << k) + 1][k]);
}
int main() {
// freopen("JOI14.INP","r",stdin);
// freopen("JOI14.OUT","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n , k;
cin >> n >> k;
vector < int > vc;
vector < ii > cat;
for (int i = 1 ; i <= n ; ++i) cin >> a[i].fi >> a[i].se;
for (int i = 1 ; i <= k ; ++i) {
cin >> c[i];
vc.pb(c[i]);
}
sort(vc.begin(),vc.end());
for (int i = 1 ; i <= k ; ++i) {
c[i] = lower_bound(vc.begin() , vc.end() , c[i]) - vc.begin() + 1;
f[c[i]][0] = i;
}
int l = __lg(k);
for (int j = 1 ; j <= l ; ++j)
for (int i = 1 ; i + (1 << j) - 1 <= k ; ++i)
f[i][j] = max(f[i][j - 1] , f[i + (1 << (j - 1))][j - 1]);
for (int i = 1 ; i <= n ; ++i) {
int l = lower_bound(vc.begin() , vc.end() , min(a[i].fi , a[i].se)) - vc.begin() + 1;
int r = lower_bound(vc.begin() , vc.end() , max(a[i].fi , a[i].se)) - vc.begin();
cat.eb(get(l , r) , i);
}
sort(cat.begin(),cat.end());
int cur = 1;
long long ans = 0;
for (auto v : cat) {
while (cur <= v.fi) upl(c[cur]) , cur++;
int ma = max(a[v.se].fi , a[v.se].se);
int res = k - (lower_bound(vc.begin() , vc.end() , ma) - vc.begin());
ma = lower_bound(vc.begin() , vc.end() , ma) - vc.begin() + 1;
res -= dwl(ma);
if (v.fi) ans += ((a[v.se].fi < a[v.se].se) ^ (res & 1) ? a[v.se].se : a[v.se].fi);
else ans += (res & 1 ? a[v.se].se : a[v.se].fi);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
544 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
544 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
11 ms |
1792 KB |
Output is correct |
15 |
Correct |
24 ms |
3192 KB |
Output is correct |
16 |
Correct |
35 ms |
4480 KB |
Output is correct |
17 |
Correct |
48 ms |
6004 KB |
Output is correct |
18 |
Correct |
50 ms |
5876 KB |
Output is correct |
19 |
Correct |
48 ms |
5876 KB |
Output is correct |
20 |
Correct |
56 ms |
6128 KB |
Output is correct |
21 |
Correct |
44 ms |
5884 KB |
Output is correct |
22 |
Correct |
34 ms |
5500 KB |
Output is correct |
23 |
Correct |
34 ms |
5492 KB |
Output is correct |
24 |
Correct |
35 ms |
5644 KB |
Output is correct |
25 |
Correct |
32 ms |
5524 KB |
Output is correct |
26 |
Correct |
39 ms |
5756 KB |
Output is correct |
27 |
Correct |
44 ms |
5876 KB |
Output is correct |
28 |
Correct |
42 ms |
6004 KB |
Output is correct |
29 |
Correct |
48 ms |
5876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
544 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
11 ms |
1792 KB |
Output is correct |
15 |
Correct |
24 ms |
3192 KB |
Output is correct |
16 |
Correct |
35 ms |
4480 KB |
Output is correct |
17 |
Correct |
48 ms |
6004 KB |
Output is correct |
18 |
Correct |
50 ms |
5876 KB |
Output is correct |
19 |
Correct |
48 ms |
5876 KB |
Output is correct |
20 |
Correct |
56 ms |
6128 KB |
Output is correct |
21 |
Correct |
44 ms |
5884 KB |
Output is correct |
22 |
Correct |
34 ms |
5500 KB |
Output is correct |
23 |
Correct |
34 ms |
5492 KB |
Output is correct |
24 |
Correct |
35 ms |
5644 KB |
Output is correct |
25 |
Correct |
32 ms |
5524 KB |
Output is correct |
26 |
Correct |
39 ms |
5756 KB |
Output is correct |
27 |
Correct |
44 ms |
5876 KB |
Output is correct |
28 |
Correct |
42 ms |
6004 KB |
Output is correct |
29 |
Correct |
48 ms |
5876 KB |
Output is correct |
30 |
Correct |
126 ms |
20732 KB |
Output is correct |
31 |
Correct |
192 ms |
22504 KB |
Output is correct |
32 |
Correct |
215 ms |
23916 KB |
Output is correct |
33 |
Correct |
310 ms |
27464 KB |
Output is correct |
34 |
Correct |
112 ms |
20464 KB |
Output is correct |
35 |
Correct |
314 ms |
27516 KB |
Output is correct |
36 |
Correct |
316 ms |
27364 KB |
Output is correct |
37 |
Correct |
326 ms |
27368 KB |
Output is correct |
38 |
Correct |
306 ms |
27500 KB |
Output is correct |
39 |
Correct |
306 ms |
27372 KB |
Output is correct |
40 |
Correct |
268 ms |
27368 KB |
Output is correct |
41 |
Correct |
307 ms |
27396 KB |
Output is correct |
42 |
Correct |
312 ms |
27444 KB |
Output is correct |
43 |
Correct |
187 ms |
26732 KB |
Output is correct |
44 |
Correct |
188 ms |
26804 KB |
Output is correct |
45 |
Correct |
191 ms |
26604 KB |
Output is correct |
46 |
Correct |
219 ms |
25580 KB |
Output is correct |
47 |
Correct |
213 ms |
25324 KB |
Output is correct |
48 |
Correct |
263 ms |
27224 KB |
Output is correct |
49 |
Correct |
271 ms |
27120 KB |
Output is correct |