This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#ifndef _DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#endif
using namespace std;
#define int int64_t
template <class T>
istream &operator>>(istream &in, vector<T> &v)
{
for (T &x : v)
in >> x;
return in;
}
struct Range
{
int s, e;
};
struct T
{
int s, i;
bool operator<(T other) const
{
return pair<int, int>(s, i) < pair<int, int>(other.s, other.i);
}
};
int n, q;
vector<Range> ranges;
vector<T> b;
T neutral = {LLONG_MAX / 2, -1};
T update(int l, int r, int i, int k, T x)
{
if (k >= r || k < l)
return b[i];
if (l + 1 == r)
return b[i] = min(b[i], x);
int m = (l + r) / 2;
return b[i] = min(update(l, m, i * 2 + 1, k, x), update(m, r, i * 2 + 2, k, x));
}
T query(int l, int r, int i, int ql, int qr)
{
if (l >= qr || ql >= r)
return neutral;
if (l >= ql && r <= qr)
return b[i];
int m = (l + r) / 2;
return min(query(l, m, i * 2 + 1, ql, qr), query(m, r, i * 2 + 2, ql, qr));
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> n >> q;
ranges = vector<Range>(n);
vector<int> vals;
for (int i = 0; i < n; i++)
{
cin >> ranges[i].s >> ranges[i].e;
vals.push_back(ranges[i].s);
vals.push_back(ranges[i].e);
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
b = vector<T>(vals.size() * 4, neutral);
for (int i = 0; i < n; i++)
{
ranges[i].s = lower_bound(vals.begin(), vals.end(), ranges[i].s) - vals.begin();
ranges[i].e = lower_bound(vals.begin(), vals.end(), ranges[i].e) - vals.begin();
}
for (int i = 0; i < n; i++)
update(0, vals.size(), 0, ranges[i].e, {ranges[i].s, i});
vector<vector<int>> prev(20, vector<int>(n, -1));
for (int i = 0; i < n; i++)
{
prev[0][i] = query(0, vals.size(), 0, ranges[i].s, ranges[i].e + 1).i;
if (prev[0][i] == i)
prev[0][i] = -1;
}
for (int k = 1; k < 20; k++)
for (int i = 0; i < n; i++)
{
if (prev[k - 1][i] != -1)
prev[k][i] = prev[k - 1][prev[k - 1][i]];
}
while (q--)
{
int s, e;
cin >> s >> e;
s--, e--;
if (s == e)
{
cout << "0\n";
continue;
}
int cost = 0;
for (int k = 19; k >= 0; k--)
{
if (prev[k][e] != -1 && ranges[prev[k][e]].s > ranges[s].e)
e = prev[k][e], cost += pow(2, k);
}
e = prev[1][e]; // move to reachable one
cost++;
if (e != -1 && ranges[e].s <= ranges[s].e)
{
cout << cost + 1 << "\n";
}
else
{
cout << "impossible\n";
}
}
}
# | 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... |