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 name ""
#define all(x) x.begin(), x.end()
const int N = 2e5 + 7;
struct merge_sort_tree{
int _l, _r, _m;
vector<int> v;
merge_sort_tree *lt, *rt;
merge_sort_tree(int l, int r, const vector<int> &e) : _l(l), _r(r), _m((l + r) >> 1), v(r - l + 1) {
if(l == r) v[0] = e[l], lt = rt = NULL;
else {
lt = new merge_sort_tree(_l, _m, e);
rt = new merge_sort_tree(_m + 1, _r, e);
merge(all(lt->v), all(rt->v), v.begin());
}
}
int query(int qs, int qe, int x, int y) {
if(qe < _l || qs > _r) return 0 ;
if(qs <= _l && _r <= qe) return upper_bound(all(v), y) - lower_bound(all(v), x);
return lt->query(qs, qe, x, y) + rt->query(qs, qe, x, y);
}
};
int c_index[3 * N], tree[12 * N];
int a[N], b[N], c[N];
int n, m;
void build(int id , int l, int r) {
if(l == r){
tree[id] = c_index[l];
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
tree[id] = max(tree[id << 1], tree[id << 1 | 1]);
}
int get(int u, int v, int id, int l, int r) {
if(v < l || u > r) return 0;
if(u <= l && r <= v) return tree[id];
int mid = (l + r) >> 1;
return max(get(u, v, id << 1, l, mid), get(u, v, id << 1 | 1, mid + 1, r));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
if(fopen("bug.inp", "r"))
freopen("bug.inp", "r", stdin), freopen("bug.out", "w", stdout);
else if (fopen(name".inp", "r"))
freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
cin >> n >> m;
vector<int> compress;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
compress.push_back(a[i]);
compress.push_back(b[i]);
}
for (int i = 1; i <= m; i++) cin >> c[i], compress.push_back(c[i]);
sort(all(compress));
compress.erase(unique(all(compress)), compress.end());
for (int i = 1; i <= n; i++) a[i] = lower_bound(all(compress), a[i]) - compress.begin() + 1;
for (int i = 1; i <= n; i++) b[i] = lower_bound(all(compress), b[i]) - compress.begin() + 1;
for (int i = 1; i <= m; i++){
c[i] = lower_bound(all(compress), c[i]) - compress.begin() + 1;
c_index[c[i]] = i;
}
build(1, 1, compress.size());
merge_sort_tree ms(0, m + 1, vector<int> (c, c + m + 1));
basic_string<int> ans;
for (int i = 1; i <= n; i++) {
if(a[i] == b[i]){
ans += compress[a[i] - 1];
continue;
}
bool swapp = (a[i] > b[i] ? (swap(a[i], b[i]), true) : false);
int last_qr = get(a[i], b[i] - 1, 1, 1, compress.size());
int cnt = ms.query(last_qr + 1, m, b[i], compress.size());
if(last_qr != 0 || swapp) swap(a[i], b[i]);
ans += compress[~cnt & 1 ? a[i] - 1 : b[i] - 1];
}
long long val = 0;
for (int i : ans) val += i;
cout << val;
return 0;
}
Compilation message (stderr)
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen("bug.inp", "r", stdin), freopen("bug.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:49:48: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen("bug.inp", "r", stdin), freopen("bug.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:51:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |