This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m;
vector<ll> a, r;
vector<bool> vst, rec;
ll r1 = 0;
void simulateVst(vector<ll> &a) {
ll res = 0;
ll i = 0;
while (i < a.size()) {
r[i] = res;
i = a[i];
vst[i] = true;
i++;
res++;
}
r1 = res;
}
ll simulateRange(vector<ll> &a, ll start) {
ll res = r[start]+1;
ll i = start+1;
while (i != a[start]+1) {
i = a[i];
if (i == start) {
i--;
res++;
}
i++;
res++;
}
return res + (i < r.size() ? r1 - r[i] : 0);
}
ll simulate(vector<ll> &a) {
ll res = 0;
ll i = 0;
while (i < a.size()) {
i = a[i];
i++;
res++;
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
vector<pair<ll, ll>> tp;
for (int i = 0; i < n; i++) {
ll x, y;
cin >> x >> y;
tp.push_back({x, y});
tp.push_back({y, x});
}
sort(tp.begin(), tp.end());
unordered_map<ll, ll> ids;
for (int i = 0; i < 2*n; i++) {
ids[tp[i].first] = i;
}
a = vector<ll>(2*n);
for (int i = 0; i < 2*n; i++) {
a[i] = ids[tp[i].second];
}
ll mx = m == 0 ? simulate(a) : 0;
set<pair<ll, ll>> prec;
ll left = 0, right = a.size();
ll last = mx;
while (m--) {
vst = vector<bool>(a.size());
r = vector<ll>(a.size());
simulateVst(a);
ll mxA = -1;
mx = r1+1;
for (auto &e : prec) {
if (e.second > mx) {
mx = e.second;
mxA = e.first;
}
}
for (int i = left; i < right; i++) {
if (a[i] < i || vst[i] || !vst[a[i]]) continue;
ll val = simulateRange(a, i);
auto iter = prec.lower_bound({i, 0});
if (iter == prec.end()) prec.insert({i, val});
else {
if (iter->first == i) prec.erase(iter);
prec.insert({i, val});
}
if (val > mx) {
mx = val;
mxA = i;
}
}
if (m == 0) continue;
if (mxA != -1) {
left = mxA+2;
right = a[mxA]+2;
set<pair<ll, ll>> prec1;
for (auto &e : prec) {
if (e.first >= mxA && e.first < a[mxA]) continue;
if (e.first == mxA) prec1.insert({e.first+1, e.second + mx-last});
else prec1.insert({e.first < mxA ? e.first : (e.first+2), e.second + mx-last});
}
prec.clear();
for (auto &e : prec1) prec.insert(e);
vector<ll> nw;
for (int j = 0; j < a.size(); j++) {
if (j == mxA) {
nw.push_back(mxA+2);
nw.push_back(a[mxA]+2);
nw.push_back(mxA);
continue;
}
if (a[j] == mxA) nw.push_back(mxA+1);
else nw.push_back(a[j] > mxA ? a[j]+2 : a[j]);
}
a = nw;
left = mxA+2;
}
else {
a.push_back(a.size()+1);
a.push_back(a.size()-1);
prec.clear();
left = a.size()-2; right = a.size();
}
last = mx;
}
std::cout << mx;
}
Compilation message (stderr)
teleporters.cpp: In function 'void simulateVst(std::vector<long long int>&)':
teleporters.cpp:17:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | while (i < a.size()) {
| ~~^~~~~~~~~~
teleporters.cpp: In function 'll simulateRange(std::vector<long long int>&, ll)':
teleporters.cpp:39:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | return res + (i < r.size() ? r1 - r[i] : 0);
| ~~^~~~~~~~~~
teleporters.cpp: In function 'll simulate(std::vector<long long int>&)':
teleporters.cpp:45:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | while (i < a.size()) {
| ~~^~~~~~~~~~
teleporters.cpp: In function 'int main()':
teleporters.cpp:120:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int j = 0; j < a.size(); j++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |