# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
682001 | SanguineChameleon | Event Hopping (BOI22_events) | C++17 | 157 ms | 46732 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// BEGIN BOILERPLATE CODE
#include <bits/stdc++.h>
using namespace std;
#ifdef KAMIRULEZ
const bool kami_loc = true;
const long long kami_seed = 69420;
#else
const bool kami_loc = false;
const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif
const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);
long long rand_range(long long rmin, long long rmax) {
uniform_int_distribution<long long> rdist(rmin, rmax);
return rdist(kami_gen);
}
long double rand_real(long double rmin, long double rmax) {
uniform_real_distribution<long double> rdist(rmin, rmax);
return rdist(kami_gen);
}
void file_io(string fi, string fo) {
if (fi != "" && (!kami_loc || fi == kami_fi)) {
freopen(fi.c_str(), "r", stdin);
}
if (fo != "" && (!kami_loc || fo == kami_fo)) {
freopen(fo.c_str(), "w", stdout);
}
}
void set_up() {
if (kami_loc) {
file_io(kami_fi, kami_fo);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
}
void just_do_it();
void just_exec_it() {
if (kami_loc) {
auto pstart = chrono::steady_clock::now();
just_do_it();
auto pend = chrono::steady_clock::now();
long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
string bar(50, '=');
cout << '\n' << bar << '\n';
cout << "Time: " << ptime << " ms" << '\n';
}
else {
just_do_it();
}
}
int main() {
set_up();
just_exec_it();
return 0;
}
// END BOILERPLATE CODE
// BEGIN MAIN CODE
const int ms = 1e5 + 20;
const int inf = 1e9 + 20;
const int st = 20;
int lg[ms * 2];
int lt[ms];
int rt[ms];
pair<int, int> p[ms * 2];
pair<int, int> mi[ms * 2][st];
int nxt[ms][st];
int get(int lt, int rt) {
int k = lg[rt - lt + 1];
return min(mi[lt][k], mi[rt - (1 << k) + 1][k]).second;
}
void query() {
int u, v;
cin >> u >> v;
if (u == v) {
cout << 0 << '\n';
return;
}
if (lt[v] <= rt[u] && rt[u] <= rt[v]) {
cout << 1 << '\n';
return;
}
if (rt[u] > rt[v]) {
cout << "impossible" << '\n';
return;
}
int res = 0;
for (int k = st - 1; k >= 0; k--) {
if (lt[nxt[v][k]] > rt[u]) {
v = nxt[v][k];
res += (1 << k);
}
}
res += 2;
v = nxt[v][0];
if (lt[v] <= rt[u] && rt[u] <= rt[v]) {
cout << res << '\n';
}
else {
cout << "impossible" << '\n';
}
}
void just_do_it() {
for (int i = 2; i < ms * 2; i++) {
lg[i] = lg[i / 2] + 1;
}
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> lt[i] >> rt[i];
}
for (int i = 1; i <= n; i++) {
p[i * 2 - 1] = {lt[i], i};
p[i * 2] = {rt[i], -i};
}
sort(p + 1, p + n * 2 + 1);
int cnt = 0;
for (int i = 1; i <= n * 2; i++) {
if (p[i].first != p[i - 1].first) {
cnt++;
}
if (p[i].second > 0) {
lt[p[i].second] = cnt;
}
else {
rt[-p[i].second] = cnt;
}
}
for (int i = 1; i <= cnt; i++) {
mi[i][0] = {inf, -1};
}
for (int i = 1; i <= n; i++) {
mi[rt[i]][0] = min(mi[rt[i]][0], {lt[i], i});
}
for (int k = 1; k < st; k++) {
for (int i = 1; i + (1 << k) - 1 <= cnt; i++) {
mi[i][k] = min(mi[i][k - 1], mi[i + (1 << (k - 1))][k - 1]);
}
}
for (int i = 1; i <= n; i++) {
nxt[i][0] = get(lt[i], rt[i]);
}
for (int k = 1; k < st; k++) {
for (int i = 1; i <= n; i++) {
nxt[i][k] = nxt[nxt[i][k - 1]][k - 1];
}
}
for (int i = 0; i < q; i++) {
query();
}
}
// END MAIN CODE
Compilation message (stderr)
# | 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... |