#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1.1e9;
struct segtree {
int ptwo;
vector<int> seg;
segtree(){}
int& operator[](int i) {
return seg[i+ptwo];
}
segtree(int nn) {
ptwo=1;
while(ptwo<nn)
ptwo*=2;
seg.assign(ptwo*2,oo);
}
void update(int i, int val) {
assert(i>=0 and i<ptwo);
i+=ptwo;
seg[i] = val;
for(i/=2;i>=1;i/=2) {
seg[i] = min(seg[2*i],seg[2*i+1]);
}
}
int findLastSmall(int v) {
int at=1;
while(at<ptwo) {
at = at*2 + (seg[at*2+1]<v);
}
return at-ptwo;
}
};
template<typename T> struct fenwick {
int n;
vector<T> fen;
fenwick(){}
fenwick(int nn) {
fen.resize(nn+1);
n = nn;
}
auto sum(int i) {
T ans = 0;
while(i) {
ans+=fen[i];
i&=i-1;
}
return ans;
}
auto query(int l, int r) {
return sum(r+1)-sum(l);
}
void update(int i, T val) {
++i;
while(i<=n) {
fen[i]+=val;
i+= i&(-i);
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k; cin >> n >> k;
segtree seg(k+1);
vector<array<int,2>> ab(n);
for(auto& [a,b] : ab) cin >> a >> b;
for(int i=1;i<=k;++i) {
int t; cin >> t;
seg.update(i,t);
}
vi ord(k); iota(all(ord),1);
sort(all(ord),[&](int i, int j) {return seg[i]<seg[j];});
sort(all(ab),[&](auto i, auto j) {return min(i[0],i[1])<min(j[0],j[1]);});
ll ans=0;
fenwick<int> fen(k+1);
int j=0;
for(auto& [a,b] : ab) {
bool flip=a>b;
if(flip) swap(a,b);
while(j<k and seg[ord[j]]<a) {
seg.update(ord[j],oo);
fen.update(ord[j],1);
++j;
}
int id = seg.findLastSmall(b);
bool par=false;
if(id==0) {
par = (k-id + !flip - fen.query(id,k))%2;
} else {
par = (k-id - fen.query(id,k))%2;
}
if(par) ans+=a;
else ans+=b;
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
7 ms |
836 KB |
Output is correct |
15 |
Correct |
19 ms |
1364 KB |
Output is correct |
16 |
Correct |
25 ms |
1820 KB |
Output is correct |
17 |
Correct |
27 ms |
2500 KB |
Output is correct |
18 |
Correct |
26 ms |
2536 KB |
Output is correct |
19 |
Correct |
29 ms |
2548 KB |
Output is correct |
20 |
Correct |
29 ms |
2672 KB |
Output is correct |
21 |
Correct |
26 ms |
2584 KB |
Output is correct |
22 |
Correct |
20 ms |
2124 KB |
Output is correct |
23 |
Correct |
27 ms |
2152 KB |
Output is correct |
24 |
Correct |
20 ms |
2132 KB |
Output is correct |
25 |
Correct |
19 ms |
2168 KB |
Output is correct |
26 |
Correct |
39 ms |
2424 KB |
Output is correct |
27 |
Correct |
28 ms |
2604 KB |
Output is correct |
28 |
Correct |
24 ms |
2548 KB |
Output is correct |
29 |
Correct |
28 ms |
2508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
7 ms |
836 KB |
Output is correct |
15 |
Correct |
19 ms |
1364 KB |
Output is correct |
16 |
Correct |
25 ms |
1820 KB |
Output is correct |
17 |
Correct |
27 ms |
2500 KB |
Output is correct |
18 |
Correct |
26 ms |
2536 KB |
Output is correct |
19 |
Correct |
29 ms |
2548 KB |
Output is correct |
20 |
Correct |
29 ms |
2672 KB |
Output is correct |
21 |
Correct |
26 ms |
2584 KB |
Output is correct |
22 |
Correct |
20 ms |
2124 KB |
Output is correct |
23 |
Correct |
27 ms |
2152 KB |
Output is correct |
24 |
Correct |
20 ms |
2132 KB |
Output is correct |
25 |
Correct |
19 ms |
2168 KB |
Output is correct |
26 |
Correct |
39 ms |
2424 KB |
Output is correct |
27 |
Correct |
28 ms |
2604 KB |
Output is correct |
28 |
Correct |
24 ms |
2548 KB |
Output is correct |
29 |
Correct |
28 ms |
2508 KB |
Output is correct |
30 |
Correct |
69 ms |
6128 KB |
Output is correct |
31 |
Correct |
92 ms |
7236 KB |
Output is correct |
32 |
Correct |
101 ms |
8592 KB |
Output is correct |
33 |
Correct |
145 ms |
11300 KB |
Output is correct |
34 |
Correct |
56 ms |
5848 KB |
Output is correct |
35 |
Correct |
151 ms |
11312 KB |
Output is correct |
36 |
Correct |
144 ms |
11216 KB |
Output is correct |
37 |
Correct |
142 ms |
11280 KB |
Output is correct |
38 |
Correct |
141 ms |
11340 KB |
Output is correct |
39 |
Correct |
141 ms |
11344 KB |
Output is correct |
40 |
Correct |
123 ms |
11072 KB |
Output is correct |
41 |
Correct |
139 ms |
11212 KB |
Output is correct |
42 |
Correct |
163 ms |
11272 KB |
Output is correct |
43 |
Correct |
111 ms |
10576 KB |
Output is correct |
44 |
Correct |
108 ms |
10640 KB |
Output is correct |
45 |
Correct |
107 ms |
10448 KB |
Output is correct |
46 |
Correct |
121 ms |
9432 KB |
Output is correct |
47 |
Correct |
117 ms |
9172 KB |
Output is correct |
48 |
Correct |
134 ms |
11340 KB |
Output is correct |
49 |
Correct |
127 ms |
11372 KB |
Output is correct |