/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int n, q, dist[3][3*N], vis[3][3*N], x[N], id[2*N], L[N], R[N], cnt;
vector<int> adj[2][3*N];
vector<pii> vec[3*N];
void build(int ind=1) {
id[ind] = ++cnt;
if(ind>=(1<<lg)) {
int f = ind - (1<<lg) + 1;
if(f<=n) adj[0][f].pb(id[ind]);
return;
}
build(2*ind); build(2*ind+1);
adj[0][id[2*ind+1]].pb(id[ind]);
adj[0][id[2*ind]].pb(id[ind]);
}
void query(int v, int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(l>=rc || lc>=r) return;
if(lc>=l && rc<=r) {
adj[1][id[ind]].pb(v);
return;
}
int mid = (lc+rc)/2;
query(v, l, r, 2*ind, lc, mid);
query(v, l, r, 2*ind+1, mid, rc);
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n;
cnt=n;
for(int i=1; i<=n; i++) cin>>L[i]>>R[i];
cin>>q;
for(int i=1; i<=q; i++) cin>>x[i];
build(1);
for(int i=1; i<=n; i++) {
query(i, L[i], i);
query(i, i+1, R[i]+1);
}
int ptr = 0;
vec[0].pb({0, 1});
vec[0].pb({1, n});
while(ptr<n+3) {
if(size(vec[ptr]) == 0) {
ptr++; continue;
}
pii v = vec[ptr].back();
vec[ptr].pop_back();
if(vis[v.F][v.S] == 1) continue;
vis[v.F][v.S] = 1, dist[v.F][v.S] = ptr;
if(vis[1][v.S]==1 && vis[0][v.S]==1 && vis[2][v.S] == 0) {
vec[dist[1][v.S]+dist[0][v.S]-(v.S>1&&v.S<n)].pb({2, v.S});
}
for(auto it : adj[0][v.S]) {
if(vis[v.F][it]==0)vec[ptr].pb({v.F, it});
}
for(auto it : adj[1][v.S]) {
if(vis[v.F][it]==0)vec[ptr+1].pb({v.F, it});
}
}
for(int i=1; i<=q; i++) {
if(vis[2][x[i]] == 1) cout<<dist[2][x[i]]<<endl;
else cout<<-1<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
86708 KB |
Output is correct |
2 |
Correct |
39 ms |
86620 KB |
Output is correct |
3 |
Correct |
42 ms |
92756 KB |
Output is correct |
4 |
Correct |
938 ms |
270140 KB |
Output is correct |
5 |
Correct |
374 ms |
153624 KB |
Output is correct |
6 |
Correct |
244 ms |
133484 KB |
Output is correct |
7 |
Correct |
414 ms |
269508 KB |
Output is correct |
8 |
Correct |
250 ms |
165412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
92836 KB |
Output is correct |
2 |
Correct |
47 ms |
82512 KB |
Output is correct |
3 |
Correct |
56 ms |
86640 KB |
Output is correct |
4 |
Correct |
41 ms |
86620 KB |
Output is correct |
5 |
Correct |
43 ms |
92968 KB |
Output is correct |
6 |
Correct |
48 ms |
82512 KB |
Output is correct |
7 |
Correct |
42 ms |
92760 KB |
Output is correct |
8 |
Correct |
40 ms |
86620 KB |
Output is correct |
9 |
Correct |
44 ms |
86876 KB |
Output is correct |
10 |
Correct |
44 ms |
86688 KB |
Output is correct |
11 |
Correct |
44 ms |
86784 KB |
Output is correct |
12 |
Correct |
45 ms |
87092 KB |
Output is correct |
13 |
Correct |
46 ms |
86868 KB |
Output is correct |
14 |
Correct |
53 ms |
86724 KB |
Output is correct |
15 |
Correct |
46 ms |
92900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
92836 KB |
Output is correct |
2 |
Correct |
47 ms |
82512 KB |
Output is correct |
3 |
Correct |
56 ms |
86640 KB |
Output is correct |
4 |
Correct |
41 ms |
86620 KB |
Output is correct |
5 |
Correct |
43 ms |
92968 KB |
Output is correct |
6 |
Correct |
48 ms |
82512 KB |
Output is correct |
7 |
Correct |
42 ms |
92760 KB |
Output is correct |
8 |
Correct |
40 ms |
86620 KB |
Output is correct |
9 |
Correct |
44 ms |
86876 KB |
Output is correct |
10 |
Correct |
44 ms |
86688 KB |
Output is correct |
11 |
Correct |
44 ms |
86784 KB |
Output is correct |
12 |
Correct |
45 ms |
87092 KB |
Output is correct |
13 |
Correct |
46 ms |
86868 KB |
Output is correct |
14 |
Correct |
53 ms |
86724 KB |
Output is correct |
15 |
Correct |
46 ms |
92900 KB |
Output is correct |
16 |
Correct |
54 ms |
84220 KB |
Output is correct |
17 |
Correct |
50 ms |
83468 KB |
Output is correct |
18 |
Correct |
52 ms |
93784 KB |
Output is correct |
19 |
Correct |
41 ms |
87636 KB |
Output is correct |
20 |
Correct |
51 ms |
93272 KB |
Output is correct |
21 |
Correct |
49 ms |
87280 KB |
Output is correct |
22 |
Correct |
48 ms |
93824 KB |
Output is correct |
23 |
Correct |
49 ms |
93904 KB |
Output is correct |
24 |
Correct |
49 ms |
93696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
92836 KB |
Output is correct |
2 |
Correct |
47 ms |
82512 KB |
Output is correct |
3 |
Correct |
56 ms |
86640 KB |
Output is correct |
4 |
Correct |
41 ms |
86620 KB |
Output is correct |
5 |
Correct |
43 ms |
92968 KB |
Output is correct |
6 |
Correct |
48 ms |
82512 KB |
Output is correct |
7 |
Correct |
42 ms |
92760 KB |
Output is correct |
8 |
Correct |
40 ms |
86620 KB |
Output is correct |
9 |
Correct |
44 ms |
86876 KB |
Output is correct |
10 |
Correct |
44 ms |
86688 KB |
Output is correct |
11 |
Correct |
44 ms |
86784 KB |
Output is correct |
12 |
Correct |
45 ms |
87092 KB |
Output is correct |
13 |
Correct |
46 ms |
86868 KB |
Output is correct |
14 |
Correct |
53 ms |
86724 KB |
Output is correct |
15 |
Correct |
46 ms |
92900 KB |
Output is correct |
16 |
Correct |
54 ms |
84220 KB |
Output is correct |
17 |
Correct |
50 ms |
83468 KB |
Output is correct |
18 |
Correct |
52 ms |
93784 KB |
Output is correct |
19 |
Correct |
41 ms |
87636 KB |
Output is correct |
20 |
Correct |
51 ms |
93272 KB |
Output is correct |
21 |
Correct |
49 ms |
87280 KB |
Output is correct |
22 |
Correct |
48 ms |
93824 KB |
Output is correct |
23 |
Correct |
49 ms |
93904 KB |
Output is correct |
24 |
Correct |
49 ms |
93696 KB |
Output is correct |
25 |
Correct |
46 ms |
92912 KB |
Output is correct |
26 |
Correct |
42 ms |
92924 KB |
Output is correct |
27 |
Correct |
52 ms |
94072 KB |
Output is correct |
28 |
Correct |
60 ms |
93520 KB |
Output is correct |
29 |
Correct |
47 ms |
93416 KB |
Output is correct |
30 |
Correct |
68 ms |
93400 KB |
Output is correct |
31 |
Correct |
50 ms |
93664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
86708 KB |
Output is correct |
2 |
Correct |
39 ms |
86620 KB |
Output is correct |
3 |
Correct |
42 ms |
92756 KB |
Output is correct |
4 |
Correct |
938 ms |
270140 KB |
Output is correct |
5 |
Correct |
374 ms |
153624 KB |
Output is correct |
6 |
Correct |
244 ms |
133484 KB |
Output is correct |
7 |
Correct |
414 ms |
269508 KB |
Output is correct |
8 |
Correct |
250 ms |
165412 KB |
Output is correct |
9 |
Correct |
44 ms |
92836 KB |
Output is correct |
10 |
Correct |
47 ms |
82512 KB |
Output is correct |
11 |
Correct |
56 ms |
86640 KB |
Output is correct |
12 |
Correct |
41 ms |
86620 KB |
Output is correct |
13 |
Correct |
43 ms |
92968 KB |
Output is correct |
14 |
Correct |
48 ms |
82512 KB |
Output is correct |
15 |
Correct |
42 ms |
92760 KB |
Output is correct |
16 |
Correct |
40 ms |
86620 KB |
Output is correct |
17 |
Correct |
44 ms |
86876 KB |
Output is correct |
18 |
Correct |
44 ms |
86688 KB |
Output is correct |
19 |
Correct |
44 ms |
86784 KB |
Output is correct |
20 |
Correct |
45 ms |
87092 KB |
Output is correct |
21 |
Correct |
46 ms |
86868 KB |
Output is correct |
22 |
Correct |
53 ms |
86724 KB |
Output is correct |
23 |
Correct |
46 ms |
92900 KB |
Output is correct |
24 |
Correct |
54 ms |
84220 KB |
Output is correct |
25 |
Correct |
50 ms |
83468 KB |
Output is correct |
26 |
Correct |
52 ms |
93784 KB |
Output is correct |
27 |
Correct |
41 ms |
87636 KB |
Output is correct |
28 |
Correct |
51 ms |
93272 KB |
Output is correct |
29 |
Correct |
49 ms |
87280 KB |
Output is correct |
30 |
Correct |
48 ms |
93824 KB |
Output is correct |
31 |
Correct |
49 ms |
93904 KB |
Output is correct |
32 |
Correct |
49 ms |
93696 KB |
Output is correct |
33 |
Correct |
46 ms |
92912 KB |
Output is correct |
34 |
Correct |
42 ms |
92924 KB |
Output is correct |
35 |
Correct |
52 ms |
94072 KB |
Output is correct |
36 |
Correct |
60 ms |
93520 KB |
Output is correct |
37 |
Correct |
47 ms |
93416 KB |
Output is correct |
38 |
Correct |
68 ms |
93400 KB |
Output is correct |
39 |
Correct |
50 ms |
93664 KB |
Output is correct |
40 |
Correct |
892 ms |
308108 KB |
Output is correct |
41 |
Correct |
356 ms |
156444 KB |
Output is correct |
42 |
Correct |
372 ms |
175564 KB |
Output is correct |
43 |
Correct |
365 ms |
186304 KB |
Output is correct |
44 |
Correct |
254 ms |
134444 KB |
Output is correct |
45 |
Correct |
256 ms |
133968 KB |
Output is correct |
46 |
Correct |
165 ms |
118580 KB |
Output is correct |
47 |
Correct |
396 ms |
202288 KB |
Output is correct |
48 |
Correct |
366 ms |
170628 KB |
Output is correct |