#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class P , class Q > ostream& operator<<(ostream& stream, pair < P , Q > v){ stream << "(" << v.fi << ',' << v.se << ")"; return stream;}
template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;}
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
const int N = 1e6 + 5;
int a[N],b[N];
int e[20][N];
int t[N];
int SZ;
vector < pii > who[N];
void upd(int i) {
for (;i;i -= i&(-i))
t[i] += 1;
}
int que(int i) {
int ans = 0;
for (;i <= SZ;i += i&(-i))
ans += t[i];
return ans;
}
int how[N];
int main(void) {
vi w;
int n,m;
cin>>n>>m;
for (int i = 1;i <= n;++i)
cin>>a[i]>>b[i],w.pb(a[i]),w.pb(b[i]);
vi h(m);
for (int i = 1;i <= m;++i) {
cin>>h[i - 1];
w.pb(h[i - 1]);
}
sort(all(w));
w.resize(unique(all(w)) - w.begin());
SZ = w.size();
int shit = 0;
for (auto & it : h) {
it = lower_bound(all(w),it) - w.begin() + 1;
e[0][it] = ++shit;
}
for (int k = 1;(SZ >> k);++k)
for (int i = 1;i + (1 << k) <= SZ + 1;++i)
e[k][i] = max(e[k - 1][i],e[k - 1][i + (1 << (k - 1))]);
for (int i = 1;i <= n;++i) {
int l = min(a[i],b[i]);
int r = max(a[i],b[i]);
int wh = 0;
l = lower_bound(all(w),l) - w.begin() + 1;
r = lower_bound(all(w),r) - w.begin() + 1;
int ind = l;
for (int t = 20;t >= 0;--t)
if (ind + (1 << t) <= r)
smax(wh,e[t][ind]),ind += 1 << t;
if (wh != 0) {
how[i] = 1;
if (a[i] > b[i])
swap(a[i],b[i]);
}
who[wh].pb(mp(i,r));
}
for (int i = m;i >= 0;--i) {
for (auto it : who[i]) {
how[it.fi] ^= que(it.se) & 1;
}
if (i > 0)
upd(h[i - 1]);
}
ll answer = 0;
for (int i = 1;i <= n;++i)
answer += (!how[i] ? a[i] : b[i]);
cout << answer << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
24056 KB |
Output is correct |
2 |
Correct |
27 ms |
24212 KB |
Output is correct |
3 |
Correct |
32 ms |
24380 KB |
Output is correct |
4 |
Correct |
30 ms |
24468 KB |
Output is correct |
5 |
Correct |
30 ms |
24496 KB |
Output is correct |
6 |
Correct |
30 ms |
24532 KB |
Output is correct |
7 |
Correct |
32 ms |
24564 KB |
Output is correct |
8 |
Correct |
34 ms |
24596 KB |
Output is correct |
9 |
Correct |
32 ms |
24628 KB |
Output is correct |
10 |
Correct |
29 ms |
24712 KB |
Output is correct |
11 |
Correct |
31 ms |
24720 KB |
Output is correct |
12 |
Correct |
31 ms |
24752 KB |
Output is correct |
13 |
Correct |
32 ms |
24852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
24056 KB |
Output is correct |
2 |
Correct |
27 ms |
24212 KB |
Output is correct |
3 |
Correct |
32 ms |
24380 KB |
Output is correct |
4 |
Correct |
30 ms |
24468 KB |
Output is correct |
5 |
Correct |
30 ms |
24496 KB |
Output is correct |
6 |
Correct |
30 ms |
24532 KB |
Output is correct |
7 |
Correct |
32 ms |
24564 KB |
Output is correct |
8 |
Correct |
34 ms |
24596 KB |
Output is correct |
9 |
Correct |
32 ms |
24628 KB |
Output is correct |
10 |
Correct |
29 ms |
24712 KB |
Output is correct |
11 |
Correct |
31 ms |
24720 KB |
Output is correct |
12 |
Correct |
31 ms |
24752 KB |
Output is correct |
13 |
Correct |
32 ms |
24852 KB |
Output is correct |
14 |
Correct |
54 ms |
27120 KB |
Output is correct |
15 |
Correct |
102 ms |
30140 KB |
Output is correct |
16 |
Correct |
151 ms |
33488 KB |
Output is correct |
17 |
Correct |
164 ms |
37172 KB |
Output is correct |
18 |
Correct |
143 ms |
38332 KB |
Output is correct |
19 |
Correct |
162 ms |
39424 KB |
Output is correct |
20 |
Correct |
166 ms |
40788 KB |
Output is correct |
21 |
Correct |
165 ms |
42080 KB |
Output is correct |
22 |
Correct |
109 ms |
42080 KB |
Output is correct |
23 |
Correct |
110 ms |
42080 KB |
Output is correct |
24 |
Correct |
107 ms |
42080 KB |
Output is correct |
25 |
Correct |
111 ms |
42604 KB |
Output is correct |
26 |
Correct |
118 ms |
42604 KB |
Output is correct |
27 |
Correct |
130 ms |
44048 KB |
Output is correct |
28 |
Correct |
167 ms |
45264 KB |
Output is correct |
29 |
Correct |
137 ms |
47848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
24056 KB |
Output is correct |
2 |
Correct |
27 ms |
24212 KB |
Output is correct |
3 |
Correct |
32 ms |
24380 KB |
Output is correct |
4 |
Correct |
30 ms |
24468 KB |
Output is correct |
5 |
Correct |
30 ms |
24496 KB |
Output is correct |
6 |
Correct |
30 ms |
24532 KB |
Output is correct |
7 |
Correct |
32 ms |
24564 KB |
Output is correct |
8 |
Correct |
34 ms |
24596 KB |
Output is correct |
9 |
Correct |
32 ms |
24628 KB |
Output is correct |
10 |
Correct |
29 ms |
24712 KB |
Output is correct |
11 |
Correct |
31 ms |
24720 KB |
Output is correct |
12 |
Correct |
31 ms |
24752 KB |
Output is correct |
13 |
Correct |
32 ms |
24852 KB |
Output is correct |
14 |
Correct |
54 ms |
27120 KB |
Output is correct |
15 |
Correct |
102 ms |
30140 KB |
Output is correct |
16 |
Correct |
151 ms |
33488 KB |
Output is correct |
17 |
Correct |
164 ms |
37172 KB |
Output is correct |
18 |
Correct |
143 ms |
38332 KB |
Output is correct |
19 |
Correct |
162 ms |
39424 KB |
Output is correct |
20 |
Correct |
166 ms |
40788 KB |
Output is correct |
21 |
Correct |
165 ms |
42080 KB |
Output is correct |
22 |
Correct |
109 ms |
42080 KB |
Output is correct |
23 |
Correct |
110 ms |
42080 KB |
Output is correct |
24 |
Correct |
107 ms |
42080 KB |
Output is correct |
25 |
Correct |
111 ms |
42604 KB |
Output is correct |
26 |
Correct |
118 ms |
42604 KB |
Output is correct |
27 |
Correct |
130 ms |
44048 KB |
Output is correct |
28 |
Correct |
167 ms |
45264 KB |
Output is correct |
29 |
Correct |
137 ms |
47848 KB |
Output is correct |
30 |
Correct |
247 ms |
59336 KB |
Output is correct |
31 |
Correct |
321 ms |
69340 KB |
Output is correct |
32 |
Correct |
471 ms |
82708 KB |
Output is correct |
33 |
Correct |
752 ms |
107812 KB |
Output is correct |
34 |
Correct |
233 ms |
107812 KB |
Output is correct |
35 |
Correct |
850 ms |
115572 KB |
Output is correct |
36 |
Correct |
739 ms |
120984 KB |
Output is correct |
37 |
Correct |
741 ms |
126796 KB |
Output is correct |
38 |
Correct |
687 ms |
132732 KB |
Output is correct |
39 |
Correct |
777 ms |
138456 KB |
Output is correct |
40 |
Correct |
681 ms |
145480 KB |
Output is correct |
41 |
Correct |
691 ms |
149864 KB |
Output is correct |
42 |
Correct |
747 ms |
155456 KB |
Output is correct |
43 |
Correct |
540 ms |
160244 KB |
Output is correct |
44 |
Correct |
509 ms |
165772 KB |
Output is correct |
45 |
Correct |
558 ms |
171704 KB |
Output is correct |
46 |
Correct |
598 ms |
171704 KB |
Output is correct |
47 |
Correct |
452 ms |
171704 KB |
Output is correct |
48 |
Correct |
663 ms |
171704 KB |
Output is correct |
49 |
Correct |
654 ms |
174948 KB |
Output is correct |