#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
int st[12 * N];
void Add(int node, int l, int r, int x, int y) {
smax(st[node], y);
if (l == r) return;
int mid = l + r >> 1;
if (x <= mid) Add(2 * node, l, mid, x, y);
else Add(2 * node + 1, mid + 1, r, x, y);
}
int Get(int node, int l, int r, int ql, int qr) {
if (r < ql || qr < l || ql > qr) return -1;
if (ql <= l && r <= qr) return st[node];
int mid = l + r >> 1;
return max(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
int bit[3 * N];
void AddBit(int x) {
x += 1;
for (; x > 0; x -= x & (-x)) bit[x]++;
}
int GetBit(int x) {
x += 1; int ans = 0;
for (; x < 3 * N; x += x & (-x)) ans += bit[x];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n), b(n), c(k);
vector<int> all;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
all.push_back(a[i]);
all.push_back(b[i]);
}
for (int i = 0; i < k; i++) {
cin >> c[i];
all.push_back(c[i]);
}
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
map<int, int> mp;
int sz = all.size();
for (int i = 0; i < all.size(); i++) {
mp[all[i]] = i;
}
for (int i = 0; i < n; i++) {
a[i] = mp[a[i]];
b[i] = mp[b[i]];
}
for (int i = 0; i < k; i++) {
c[i] = mp[c[i]];
}
for (int i = 1; i <= 4 * n; i++) st[i] = -1;
for (int i = 0; i < k; i++) Add(1, 0, sz - 1, c[i], i);
vector<int> v(n);
vector<array<int, 3>> svi;
for (int i = 0; i < n; i++) {
int A = min(a[i], b[i]); int B = max(a[i], b[i]);
v[i] = Get(1, 0, sz - 1, A, B - 1);
if (v[i] != -1) svi.push_back({c[v[i]], 1, i});
else svi.push_back({-inf, 1, i});
}
for (int i = 0; i < k; i++) svi.push_back({i, 0, i});
sort(svi.begin(), svi.end(), [&] (array<int, 3> c1, array<int, 3> c2) {
if (c1[0] != c2[0]) return c1[0] > c2[0];
return c1[1] < c2[1];
});
ll fina = 0;
for (auto u : svi) {
if (u[1] == 0) AddBit(c[u[2]]);
else {
int i = u[2];
int A = min(a[i], b[i]);
int B = max(a[i], b[i]);
int x = GetBit(B) % 2;
if (v[i] == -1) {
if (x) fina += all[b[i]];
else fina += all[a[i]];
continue;
}
x += a[i] > b[i];
x %= 2;
if (x) fina += all[a[i]];
else fina += all[b[i]];
}
}
cout << fina << en;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'void Add(int, int, int, int, int)':
fortune_telling2.cpp:15:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int Get(int, int, int, int, int)':
fortune_telling2.cpp:22:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < all.size(); i++) {
| ~~^~~~~~~~~~~~
fortune_telling2.cpp:86:17: warning: unused variable 'A' [-Wunused-variable]
86 | int A = min(a[i], b[i]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |