// Just by leaving I’m helping them another day
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;
const int inf = 1e9 + 5;
struct sgttree // hlada maximum
{
int n;
vector<pair<int, int> > st;
void init(int N, vector<int> a)
{
n = N;
st.assign(2 * n, { -inf, -1 });
for (int i = n; i < 2 * n; i++) st[i] = { a[i - n], i - n };
for (int i = n - 1; i >= 0; i--) st[i] = max(st[(i << 1)], st[(i << 1) + 1]);
}
pair<int, int> query(int l, int r)
{
pair<int, int> p(-inf, -1);
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1)
{
if (l & 1) p = max(p, st[l++]);
if (r & 1) p = max(p, st[--r]);
}
return p;
}
};
void bfs(int n, vector<int>& d, vector<vector<int> >& g) // vzdialenost inf ak sa niekam nevieme dostat
{
set<pair<int, int> > pq;
for (int i = 0; i < n; i++) pq.insert({ d[i], i });
while (!pq.empty())
{
int u = pq.begin()->second;
pq.erase(pq.begin());
for (int v : g[u]) if (d[u] + 1 < d[v])
{
pq.erase({ d[v], v });
d[v] = d[u] + 1;
pq.insert({ d[v], v });
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> l(n), r(n);
for (int i = 0; i < n; i++) cin >> l[i] >> r[i], l[i] = -(l[i] - 1), r[i]--;
sgttree mi, mx;
mi.init(n, r);
mx.init(n, l);
for (int i = 0; i < n; i++) l[i] = -l[i];
vector<vector<int> > g(n), gr(n);
vector<int> d0(n, inf), dn(n, inf);
for (int i = 0; i < n; i++)
{
if (l[i] == 0) d0[i] = 0;
if (r[i] == n - 1) dn[i] = 0;
}
for (int i = 0; i < n; i++)
{
for (int j = l[i]; j <= r[i]; j++) g[i].push_back(j), gr[j].push_back(i);
}
bfs(n, d0, gr);
bfs(n, dn, gr);
vector<int> d(n, inf);
for (int i = 0; i < n; i++) d[i] = min(inf, d0[i] + dn[i] + 1);
bfs(n, d, gr);
int q;
cin >> q;
while (q--)
{
int x;
cin >> x;
//cout << " ";
x--;
if (d[x] == inf) cout << "-1\n";
else cout << d[x] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2139 ms |
1012804 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
1056 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
1056 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
25 ms |
16220 KB |
Output is correct |
17 |
Correct |
4 ms |
1116 KB |
Output is correct |
18 |
Correct |
52 ms |
34640 KB |
Output is correct |
19 |
Correct |
47 ms |
32952 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
10 ms |
5468 KB |
Output is correct |
22 |
Correct |
53 ms |
41040 KB |
Output is correct |
23 |
Correct |
40 ms |
27984 KB |
Output is correct |
24 |
Correct |
39 ms |
27476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
1056 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
25 ms |
16220 KB |
Output is correct |
17 |
Correct |
4 ms |
1116 KB |
Output is correct |
18 |
Correct |
52 ms |
34640 KB |
Output is correct |
19 |
Correct |
47 ms |
32952 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
10 ms |
5468 KB |
Output is correct |
22 |
Correct |
53 ms |
41040 KB |
Output is correct |
23 |
Correct |
40 ms |
27984 KB |
Output is correct |
24 |
Correct |
39 ms |
27476 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
28 ms |
18012 KB |
Output is correct |
28 |
Correct |
4 ms |
1272 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
8 ms |
5212 KB |
Output is correct |
31 |
Correct |
26 ms |
18772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2139 ms |
1012804 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |