#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, INF = 1e9 + 10;
struct SegTree
{
int l, r, mid;
SegTree *lc, *rc;
int val = INF;
SegTree(int l_, int r_) : l(l_), r(r_)
{
mid = (l + r) / 2;
if (l == r - 1)
{
return;
}
lc = new SegTree(l, mid);
rc = new SegTree(mid, r);
}
~SegTree()
{
if (l == r - 1)
{
return;
}
delete lc;
delete rc;
}
void update(int i, int u)
{
if (l == r - 1)
{
val = u;
return;
}
if (i < mid)
{
lc->update(i, u);
}
else
{
rc->update(i, u);
}
val = min(lc->val, rc->val);
}
int query(int ll, int rr)
{
if (ll <= l && rr >= r)
{
return val;
}
int ret = INF;
if (ll < mid)
{
ret = min(ret, lc->query(ll, rr));
}
if (mid < rr)
{
ret = min(ret, rc->query(ll, rr));
}
return ret;
}
};
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++)
{
int l, r;
cin >> l >> r;
l--;
a[i] = make_pair(l, r);
}
vector<int> dpl(n, INF), dpr(n, INF);
{
SegTree st(0, n);
st.update(0, 0);
dpl[0] = 0;
for (int i = 1; i < n; i++)
{
dpl[i] = min(dpl[i], a[i].first > 0 ? 1 + st.query(a[i].first, i) : 0);
st.update(i, dpl[i]);
}
}
{
SegTree st(0, n);
st.update(n - 1, 0);
dpr[n - 1] = 0;
for (int i = n - 2; i >= 0; i--)
{
dpr[i] = a[i].second < n ? 1 + st.query(i, a[i].second) : 0;
st.update(i, dpr[i]);
}
}
vector<pair<int, int>> res(n);
for (int i = 0; i < n; i++)
{
res[i] = make_pair(min(INF, dpl[i] + dpr[i]), i);
}
sort(res.begin(), res.end());
vector<int> ans(n, INF);
SegTree st(0, n);
for (int tt = 0; tt < n; tt++)
{
for (auto [d, i] : res)
{
ans[i] = min(ans[i], min(d, 1 + st.query(a[i].first, a[i].second)));
st.update(i, ans[i]);
}
}
int q;
cin >> q;
while (q--)
{
int i;
cin >> i;
i--;
cout << (ans[i] == INF ? -1 : ans[i] + 1) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2083 ms |
24008 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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
760 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
11 |
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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
760 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
11 |
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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
760 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
2083 ms |
24008 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |