QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#787327 | #9730. Elevator II | qinsj | RE | 0ms | 0kb | C++20 | 2.7kb | 2024-11-27 11:08:41 | 2024-11-27 11:08:42 |
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define db(args...) {\
string _s = #args;replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout << '\n';}
void err(istream_iterator<string> it){}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << ' ';
err(++it, args...);
}
void solve() {
int n, f;
cin >> n >> f;
#define a3 array<int, 3>
vector<a3> a(n);
vector<int> l(n), r(n);
ll res = 0;
vector<int> p = {f};
int mxl = 0;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
mxl = max(mxl, l[i]);
p.push_back(r[i]);
res += r[i] - l[i];
a[i] = {l[i], r[i], i};
}
sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end()), p.end());
p.push_back(2e9);
int m = p.size();
vector<int> add[m];
vector<int> del[m];
for (auto [l, r, id] : a) {
auto p1 = lower_bound(p.begin(), p.end(), l) - p.begin();
auto p2 = lower_bound(p.begin(), p.end(), r + 1) - p.begin();
// cerr << m << ' ' << p1 << ' ' << p2 << "\n";
add[p1].push_back(id);
del[p2].push_back(id);
}
vector<int> jump(m, -1);
multiset<pii> pq;
for (int i = 0; i < m; i++) {
for (auto id : add[i]) {
pq.insert({r[id], id});
}
for (auto id : del[i]) {
pq.extract({r[id], id});
}
if (pq.size()) {
jump[i] = (*pq.rbegin()).second;
}
}
vector<int> visi(n);
sort(a.begin(), a.end());
vector<int> ans;
while (f < mxl) {
auto id = lower_bound(p.begin(), p.end(), f) - p.begin();
// cerr << n << ' ' << f << ' ' << id << ' ' << m << '\n';
if (id == m - 2) break;
int nid = jump[id];
if (nid != -1 && visi[nid]) {
auto costid = lower_bound(a.begin(), a.end(), a3{f + 1, 0, 0}) - a.begin();
visi[a[costid][2]] = 1;
ans.push_back(a[costid][2]);
res += a[costid][0] - f;
f = a[costid][1];
} else {
ans.push_back(nid);
visi[nid] = 1;
f = r[nid];
}
}
for (int i = n - 1; i >= 0; i--) {
if (!visi[a[i][2]]) ans.push_back(a[i][2]);
}
cout << res << '\n';
for (auto x : ans) {
cout << x + 1 << " \n" [x == ans.back()];
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
/*
g++ 1.cpp -std=c++20 -o 1 && ./1 < in.txt > out.txt
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 4 2 3 6 1 3 2 7 5 6 2 5 2 4 6 8