// na hanooz omidi vjood dare... hanooz ye karayi hast ke bknm :)
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "(" << (#x) << "): " << (x) << endl;
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
const int maxn5 = 2e5 + 10;
const int lg = 20;
int suf[maxn5], pre[maxn5], ans[maxn5];
int l[maxn5], r[maxn5];
bool mark[maxn5];
namespace rmq{
pair <int, int> mn[lg][maxn5];
void build(int n){
for(int i = 1; i < lg; i++) for(int j = 0; j + (1 << i) - 1 < n; j++)
mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]);
}
pair <int, int> get_min(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return min(mn[k][l], mn[k][r - (1 << k) + 1]);
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int i = 0; i < n; i++){
cin >> l[i] >> r[i];
l[i]--; r[i]--;
rmq::mn[0][i] = {l[i], i};
}
rmq::build(n);
fill(pre, pre + n + 5, maxn5);
fill(suf, suf + n + 5, maxn5);
pre[0] = 0;
for(int i = 1; i < n; i++){
int v = rmq::get_min(l[i], r[i]).se;
pre[i] = pre[v] + 1;
if(l[i] == 0)
pre[i] = 0;
}
for(int i = 0; i < n; i++)
rmq::mn[0][i] = {-r[i], i};
rmq::build(n);
suf[n - 1] = 0;
ans[n - 1] = min(maxn5, pre[n - 1]);
for(int i = n - 2; i >= 0; i--){
int v = rmq::get_min(l[i], r[i]).se;
suf[i] = suf[v] + 1;
if(r[i] == n - 1)
suf[i] = 0;
ans[i] = min(maxn5, pre[i] + suf[i]);
}
//debug(suf[4]);
//debug(pre[4]);
//debug(ans[4]);
while(true){
int v = -1;
for(int i = 0; i < n; i++) if(!mark[i] && (v == -1 || ans[v] > ans[i]))
v = i;
if(v == -1)
break;
mark[v] = true;
//debug(v);
//debug(ans[v]);
for(int i = 0; i < n; i++)
if(l[i] <= v && v <= r[i] && ans[i] > ans[v] + 1){
assert(i);
ans[i] = ans[v] + 1;
}
//debug(ans[4]);
}
int q; cin >> q;
for(int i = 0; i < q; i++){
int x; cin >> x;
x--;
cout << (ans[x] >= maxn5 ? -1 : ans[x] + 1) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
2080 ms |
29836 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
2080 ms |
29836 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |