This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 2e5+5;
int n, k, a[maxn], b[maxn], t[maxn], mx[maxn*4];
vector <pii> v1, v2;
vector <int> fen[maxn];
void build1() {
for (int i = 0; i < k; i++) {
for (int j = k-i; j <= k; j += j&(-j)) fen[j-1].pb(v1[i].S);
}
for (int i = 0; i < k; i++) sort(all(fen[i]));
}
void build2(int l = 0, int r = k, int id = 1) {
if (l+1 == r) {
mx[id] = v2[l].S; return;
}
int mid = (l+r)/2;
build2(l, mid, id*2), build2(mid, r, id*2+1);
mx[id] = max(mx[id*2], mx[id*2+1]);
}
int get1(int i, int j) {
int ans = 0;
for (i = k-i; i; i &= i-1) {
int p = lower_bound(all(fen[i-1]), j)-fen[i-1].begin();
ans += fen[i-1].size()-p;
}
return ans;
}
int get2(int s, int e, int l = 0, int r = k, int id = 1) {
if (s <= l and r <= e) return mx[id];
int mid = (l+r)/2;
if (e <= mid) return get2(s, e, l, mid, id*2);
if (s >= mid) return get2(s, e, mid, r, id*2+1);
return max(get2(s, e, l, mid, id*2), get2(s, e, mid, r, id*2+1));
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
for (int i = 0; i < k; i++) {
cin >> t[i];
v1.pb(mp(i, t[i]));
v2.pb(mp(t[i], i));
}
sort(all(v2));
build1(), build2();
int ans = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) ans += a[i];
else if (a[i] < b[i]) {
bool s = 0;
int j = lower_bound(all(v2), mp(a[i], 0))-v2.begin();
int k = lower_bound(all(v2), mp(b[i], 0))-v2.begin();
int time = -1;
if (j < k) {
s = 1;
time = get2(j, k);
}
s ^= get1(time+1, b[i])&1;
if (s) ans += b[i];
else ans += a[i];
// cout << i << ' ' << s << '\n';
}
else {
bool s = 0;
int j = lower_bound(all(v2), mp(b[i], 0))-v2.begin();
int k = lower_bound(all(v2), mp(a[i], 0))-v2.begin();
int time = -1;
if (j < k) time = get2(j, k);
s ^= get1(time+1, a[i])&1;
if (s) ans += b[i];
else ans += a[i];
// cout << i << ' ' << s << '\n';
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |