# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
595529 | ThegeekKnight16 | Event Hopping (BOI22_events) | C++14 | 855 ms | 18520 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.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int Inicio[MAXN], Fim[MAXN], Marc[MAXN];
vector<int> Indices;
int dp[MAXN][25];
int prox[MAXN];
bool cmp(int a, int b)
{
if (Fim[a] == Fim[b]) return Inicio[a] < Inicio[b];
return Fim[a] > Fim[b];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, Q;
cin >> N >> Q;
for (int i = 1; i <= N; i++)
{
cin >> Inicio[i] >> Fim[i];
Indices.push_back(i);
}
sort(Indices.begin(), Indices.end(), cmp);
vector<int> Usados, naoUsados;
int L = 1e9 + 10; int Encima = -1;
for (auto id : Indices)
{
if (Inicio[id] >= L) {naoUsados.push_back(id); prox[id] = Encima;}
else
{
Usados.push_back(id);
L = Inicio[id];
Marc[id] = 1;
Encima = id;
}
}
int p = 0;
for (int i = 0; i < Usados.size(); i++)
{
int id = Usados[i]; int pd = Usados[p];
while (p < i && Fim[id] < Inicio[Usados[p]]) pd = Usados[++p];
cerr << id << " " << pd << '\n';
if (Fim[id] < Inicio[pd] || Fim[id] > Fim[pd] || id == pd) prox[id] = N+1;
else prox[id] = pd;
}
prox[N+1] = N+1; Fim[N+1] = 1e9 + 10; Inicio[N+1] = 1e9 + 10;
for (int i = 1; i <= N+1; i++) {dp[i][0] = prox[i]; cerr << prox[i] << " ";}
for (int k = 1; k <= 20; k++)
{
for (int i = 1; i <= N+1; i++)
{
dp[i][k] = dp[ dp[i][k-1] ][k-1];
}
}
for (int q = 1; q <= Q; q++)
{
int X, Y;
cin >> X >> Y;
int D = X; int resp = 0;
for (int k = 20; k >= 0; k--)
{
int id = dp[D][k];
if (Fim[id] <= Fim[Y])
{
D = id;
resp += (1 << k);
}
}
if (Fim[D] < Inicio[Y] || Fim[D] > Fim[Y]) cout << "impossible" << '\n';
else
{
if (D != Y) resp++;
cout << resp << '\n';
}
}
}
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... |