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;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 4e6 + 100;
struct fen{
vector <ll> f;
int n;
fen(int N) {
f.assign(N + 1, 0);
n = N;
}
void upd(int i, int x) {
for (; i <= n; i += i & (-i))
f[i] += x;
}
ll get(int i) {
ll s = 0;
for (; i; i -= i & (-i))
s += f[i];
return s;
}
ll range(int l, int r) {
return get(r) - get(l - 1);
}
};
struct seg{
vector <ll> v;
int n;
seg(int N) {
n = N;
v.assign(n * 4 + 100, 0);
}
void upd(int id, int l, int r, int u, int x) {
if (l > u || r < u)
return;
if (l == r) {
v[id] = max(v[id], (ll)x);
return;
}
int m = (l + r) / 2;
upd(id * 2, l, m, u, x);
upd(id * 2 + 1, m + 1, r, u, x);
v[id] = max(v[id * 2], v[id * 2 + 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u || u > v)
return 0;
if (l >= u && r <= v)
return this -> v[id];
int m = (l + r) / 2;
return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
};
bool cmp(pair <int, int> &a, pair <int, int> &b) {
return max(a.f, a.s) > max(b.f, b.s);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector <pair <int, int>> st;
vector <pair <int, int>> save;
vector <int> coor, rev;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
st.pb({a, b});
coor.pb(a);
coor.pb(b);
}
for (int i = 1; i <= q; i++) {
int x;
cin >> x;
save.pb({x, i});
coor.pb(x);
}
coor.pb(0);
uniquev(coor);
sort(all(st), cmp);
sort(all(save), greater <pair <int, int>>());
int sz = n * 2 + q + 10;
rev.resize(sz);
for (auto &x : st) {
int ind = 0;
ind = lower_bound(all(coor), x.f) - coor.begin();
rev[ind] = x.f;
x.f = ind;
ind = lower_bound(all(coor), x.s) - coor.begin();
rev[ind] = x.s;
x.s = ind;
}
seg ST(sz);
for (auto &x : save) {
int ind = 0;
ind = lower_bound(all(coor), x.f) - coor.begin();
rev[ind] = x.f;
x.f = ind;
ST.upd(1, 1, sz, x.f, x.s);
}
ll ans = 0, j = 0;
fen F(q + 4);
for (auto x : st) {
int mx = max(x.f, x.s), mi = min(x.f, x.s);
int id = 0;
if (mx != mi) {
id = ST.get(1, 1, sz, mi, mx - 1);
}
while (j < sz(save) && save[j].f >= mx) {
F.upd(save[j].s, 1);
j++;
}
if (id == 0) {
if (F.range(1, q) % 2) {
ans += rev[x.s];
} else {
ans += rev[x.f];
}
} else {
if (F.range(id + 1, q) % 2) {
ans += rev[mi];
} else {
ans += rev[mx];
}
}
}
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... |