This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5;
vector<int> t[N * 4];
int n, k, a[N], b[N], c[N];
void build(int v, int l, int r) {
if (l == r) {
t[v].push_back(c[l]);
return;
}
int mid = (l + r) / 2;
build(v * 2, l, mid);
build(v * 2 + 1, mid + 1, r);
merge(t[v * 2].begin(), t[v * 2].end(), t[v * 2 + 1].begin(), t[v * 2 + 1].end(), back_inserter(t[v]));
}
int last(int v, int l, int r, int tl, int tr) {
if (l == r) {
if (tl <= c[l] && c[l] <= tr)
return l;
else
return -1;
}
int mid = (l + r) / 2;
if (lower_bound(t[v * 2 + 1].begin(), t[v * 2 + 1].end(), tl) == upper_bound(t[v * 2 + 1].begin(), t[v * 2 + 1].end(), tr))
return last(v * 2, l, mid, tl, tr);
else
return last(v * 2 + 1, mid + 1, r, tl, tr);
}
int count(int v, int l, int r, int tl, int tr, int lower) {
if (tl > tr)
return 0;
else if (l == tl && r == tr)
return t[v].end() - lower_bound(t[v].begin(), t[v].end(), lower);
int mid = (l + r) / 2;
return count(v * 2, l, mid, tl, min(mid, tr), lower) + count(v * 2 + 1, mid + 1, r, max(mid + 1, tl), tr, lower);
}
int solveTestCase(int test) {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
for (int i = 0; i < k; i++) cin >> c[i];
build(1, 0, k - 1);
int ans = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
ans += a[i];
continue;
}
int tl = last(1, 0, k - 1, min(a[i], b[i]), max(a[i], b[i]) - 1);
int f = count(1, 0, k - 1, tl + 1, k - 1, max(a[i], b[i]));
if (tl == -1) {
if (f % 2)
ans += b[i];
else
ans += a[i];
} else {
if (f % 2)
ans += min(a[i], b[i]);
else
ans += max(a[i], b[i]);
}
}
cout << ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
//cin >> test;
for (int i = 1; i <= test; i++)
solveTestCase(i);
}
Compilation message (stderr)
fortune_telling2.cpp: In function 'long long int solveTestCase(long long int)':
fortune_telling2.cpp:76:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |