/* WLOG assume a_i < b_i
* then there are 3 kinds of updates
* 1: x < b_i, 2: b_i <= x < a_i, 3: a_i <= x
* ignore updates of type 1, updates of type 2 will ensure a_i is on top, type 3 will flip it
* so for every card, find the last of type 2 then query number of type 3
* process in decreasing order of "last type 2 update"
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<ll, pi> ipi;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define size(v) (ll) v.size()
#define INF (ll) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge")))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
ll n, k, op[200005], ans;
pi arr[200005];
vi disc;
vpi v;
struct node {
ll s, e, m, maxv;
node *l, *r;
node (ll S, ll E) {
s = S, e = E, m = (s+e)/2, maxv = 0;
if (s != e) {
l = new node(s, m);
r = new node(m+1, e);
}
}
void maxup(ll x, ll nv) {
if (s == e) {maxv = max(maxv, nv); return;}
else if (x <= m) l->maxup(x, nv);
else r->maxup(x, nv);
maxv = max(l->maxv, r->maxv);
}
ll maxq(ll x, ll y) {
if (s == x and e == y) return maxv;
else if (y <= m) return l->maxq(x, y);
else if (x > m) return r->maxq(x, y);
else return max(l->maxq(x, m), r->maxq(m+1, y));
}
} *seg;
ll ft[600005]; // note: this fenwick tree is 1-indexed.
ll ls(ll x) { return x & (-x); }
void up(ll p, ll v) {
for (; p <= size(disc); p += ls(p)) ft[p] += v;
}
ll fquery(ll p) { //Returns sum from [1, p]
ll sum = 0;
for (;p; p -= ls(p)) sum += ft[p]; // while p > 0
return sum;
}
inline ll query(ll a, ll b) {
return fquery(b) - fquery(a-1);
}
int main() {
fastio; cin >> n >> k;
FOR(i, 1, n) {
cin >> arr[i].f >> arr[i].s;
disc.pb(arr[i].f);
disc.pb(arr[i].s);
}
FOR(i, 1, k) {
cin >> op[i];
disc.pb(op[i]);
}
sort(all(disc));
disc.resize(unique(all(disc)) - disc.begin());
seg = new node(1, size(disc));
FOR(i, 1, n) {
arr[i].f = lower_bound(all(disc), arr[i].f) - disc.begin() + 1;
arr[i].s = lower_bound(all(disc), arr[i].s) - disc.begin() + 1;
}
FOR(i, 1, k) {
op[i] = lower_bound(all(disc), op[i]) - disc.begin() + 1;
seg->maxup(op[i], i);
}
FOR(i, 1, n) {
pi t = arr[i];
if (t.f > t.s) swap(t.f, t.s);
ll val = 0;
if (t.f != t.s) val = seg->maxq(t.f, t.s-1);
v.pb(pi(val, i));
}
sort(all(v), greater<pi>());
ll idx = k;
for (auto [x, i]:v) {
while (idx > x) up(op[idx--], 1);
int flip = 0;
if (x == 0) flip = 0;
else flip = (arr[i].f < arr[i].s ? 1 : 0);
flip += query(max(arr[i].f, arr[i].s), size(disc));
ans += (flip % 2 ? disc[arr[i].s-1] : disc[arr[i].f-1]);
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
748 KB |
Output is correct |
3 |
Correct |
2 ms |
876 KB |
Output is correct |
4 |
Correct |
2 ms |
876 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
2 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
876 KB |
Output is correct |
8 |
Correct |
2 ms |
876 KB |
Output is correct |
9 |
Correct |
2 ms |
876 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
748 KB |
Output is correct |
13 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
748 KB |
Output is correct |
3 |
Correct |
2 ms |
876 KB |
Output is correct |
4 |
Correct |
2 ms |
876 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
2 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
876 KB |
Output is correct |
8 |
Correct |
2 ms |
876 KB |
Output is correct |
9 |
Correct |
2 ms |
876 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
748 KB |
Output is correct |
13 |
Correct |
2 ms |
748 KB |
Output is correct |
14 |
Correct |
23 ms |
5356 KB |
Output is correct |
15 |
Correct |
56 ms |
10348 KB |
Output is correct |
16 |
Correct |
90 ms |
15636 KB |
Output is correct |
17 |
Correct |
127 ms |
20200 KB |
Output is correct |
18 |
Correct |
137 ms |
20200 KB |
Output is correct |
19 |
Correct |
124 ms |
19944 KB |
Output is correct |
20 |
Correct |
124 ms |
20200 KB |
Output is correct |
21 |
Correct |
115 ms |
19944 KB |
Output is correct |
22 |
Correct |
76 ms |
14440 KB |
Output is correct |
23 |
Correct |
69 ms |
12008 KB |
Output is correct |
24 |
Correct |
63 ms |
10088 KB |
Output is correct |
25 |
Correct |
74 ms |
15976 KB |
Output is correct |
26 |
Correct |
79 ms |
13800 KB |
Output is correct |
27 |
Correct |
91 ms |
15080 KB |
Output is correct |
28 |
Correct |
82 ms |
14952 KB |
Output is correct |
29 |
Correct |
101 ms |
17516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
748 KB |
Output is correct |
3 |
Correct |
2 ms |
876 KB |
Output is correct |
4 |
Correct |
2 ms |
876 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
2 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
876 KB |
Output is correct |
8 |
Correct |
2 ms |
876 KB |
Output is correct |
9 |
Correct |
2 ms |
876 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
748 KB |
Output is correct |
13 |
Correct |
2 ms |
748 KB |
Output is correct |
14 |
Correct |
23 ms |
5356 KB |
Output is correct |
15 |
Correct |
56 ms |
10348 KB |
Output is correct |
16 |
Correct |
90 ms |
15636 KB |
Output is correct |
17 |
Correct |
127 ms |
20200 KB |
Output is correct |
18 |
Correct |
137 ms |
20200 KB |
Output is correct |
19 |
Correct |
124 ms |
19944 KB |
Output is correct |
20 |
Correct |
124 ms |
20200 KB |
Output is correct |
21 |
Correct |
115 ms |
19944 KB |
Output is correct |
22 |
Correct |
76 ms |
14440 KB |
Output is correct |
23 |
Correct |
69 ms |
12008 KB |
Output is correct |
24 |
Correct |
63 ms |
10088 KB |
Output is correct |
25 |
Correct |
74 ms |
15976 KB |
Output is correct |
26 |
Correct |
79 ms |
13800 KB |
Output is correct |
27 |
Correct |
91 ms |
15080 KB |
Output is correct |
28 |
Correct |
82 ms |
14952 KB |
Output is correct |
29 |
Correct |
101 ms |
17516 KB |
Output is correct |
30 |
Correct |
283 ms |
35644 KB |
Output is correct |
31 |
Correct |
415 ms |
49756 KB |
Output is correct |
32 |
Correct |
552 ms |
67292 KB |
Output is correct |
33 |
Correct |
868 ms |
102864 KB |
Output is correct |
34 |
Correct |
257 ms |
30436 KB |
Output is correct |
35 |
Correct |
876 ms |
102736 KB |
Output is correct |
36 |
Correct |
883 ms |
102760 KB |
Output is correct |
37 |
Correct |
893 ms |
102608 KB |
Output is correct |
38 |
Correct |
844 ms |
98256 KB |
Output is correct |
39 |
Correct |
853 ms |
102712 KB |
Output is correct |
40 |
Correct |
784 ms |
97592 KB |
Output is correct |
41 |
Correct |
861 ms |
98372 KB |
Output is correct |
42 |
Correct |
848 ms |
99596 KB |
Output is correct |
43 |
Correct |
464 ms |
97360 KB |
Output is correct |
44 |
Correct |
462 ms |
97360 KB |
Output is correct |
45 |
Correct |
469 ms |
97104 KB |
Output is correct |
46 |
Correct |
457 ms |
58724 KB |
Output is correct |
47 |
Correct |
393 ms |
47440 KB |
Output is correct |
48 |
Correct |
586 ms |
76192 KB |
Output is correct |
49 |
Correct |
564 ms |
76256 KB |
Output is correct |