#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pi pair<int, int>
typedef long long ll;
constexpr int LIM = 2e5;
constexpr int base = (1 << 20);
map<int, int>con;
int tri[2 * base];
int tri2[2 * base];
int que(int l, int r) {
l += base;
r += base;
int ans = 0;
while(l <= r) {
if(l & 1) {
ans = max(ans, tri[l]);
}
if(!(r & 1)) {
ans = max(ans, tri[r]);
}
l = (l + 1) / 2;
r = (r - 1) / 2;
}
return ans;
}
void upd2(int v, int x) {
v += base;
tri2[v] = x;
v /= 2;
while(v > 0) {
tri2[v] = tri2[2 * v] + tri2[2 * v + 1];
v /= 2;
}
}
int que2(int l, int r) {
l += base;
r += base;
int ans = 0;
while(l <= r) {
if(l & 1) {
ans += tri2[l];
}
if(!(r & 1)) {
ans += tri2[r];
}
l = (l + 1) / 2;
r = (r - 1) / 2;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, t;
cin >> n >> t;
vector<pair<int, pi> >tab;
for(int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
tab.push_back({max(a, b), {a, b}});
con[a] = 0;
con[b] = 0;
con[a - 1] = 0;
con[b - 1] = 0;
}
sort(tab.begin(), tab.end());
vector<pi>zap;
for(int i = 1; i <= t; i++) {
int k;
cin >> k;
con[k] = 0;
zap.push_back({k, i});
}
sort(zap.begin(), zap.end());
reverse(zap.begin(), zap.end());
int k = 1;
for(auto x : con) {
con[x.st] = k++;
}
for(auto x : zap) {
tri[con[x.st] + base] = max(con[tri[x.st] + base], x.nd);
}
for(int i = base - 1; i >= 1; i--) {
tri[i] = max(tri[2 * i], tri[2 * i + 1]);
}
k = 0;
ll ans = 0;
for(auto x : tab) {
while(k < zap.size() && zap[k].st >= x.st) {
upd2(zap[k].nd, 1);
k++;
}
int kt;
if(x.nd.st < x.nd.nd) {
kt = que(con[x.nd.st], con[x.nd.nd - 1]);
}
else {
kt = que(con[x.nd.nd], con[x.nd.st - 1]);
}
int a = que2(kt + 1, con.size()) & 1;
if(!kt) {
if(a) {
ans += x.nd.nd;
}
else {
ans += x.nd.st;
}
}
else {
if(!a) {
ans += x.st;
}
else {
ans += min(x.nd.st, x.nd.nd);
}
}
}
cout << ans << '\n';
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:91:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | while(k < zap.size() && zap[k].st >= x.st) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |