/*ㅤ∧_∧
( ・∀・)
( つ┳⊃
ε (_)へ⌒ヽフ
( ( ・ω・)
◎―◎ ⊃ ⊃
BePhuongSuperSuwi
From TK4 - CHT
ㅤㅤ/ ⌒\____
/・ ) \
/ノへ ノ /|
ノ \\ |/_/_/*/
#include<bits/stdc++.h>
#define task "main"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
//#define int long long
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii, ii>
#define base 341
#define MASK(i) (1ll << i)
#define oo 2e9
#define isOn(x,i) ((x) & MASK(i))
#define bitOn(x,i) ((x) | MASK(i))
#define bitOff(x,i) ((x) & ~MASK(i))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 1e7 + 2;
int n, l[maxn], r[maxn];
vector <ii> nen, g[maxn];
int lim = 0;
int st[maxn];
void build(int id, int l, int r) {
if (l == r) {
st[l] = id;
return;
}
int m = (l + r) / 2;
build(id * 2, l, m);
build(id * 2 + 1, m + 1, r);
g[id * 2].pb({id, 0});
g[id * 2 + 1].pb({id, 0});
}
void update(int id, int l, int r, int u, int v, int idx) {
if (u > r || v < l) return;
if (l >= u && r <= v) {
// g[idx].pb({st[id], 1});
g[id].pb({idx, 1});
return;
}
int m = (l + r) / 2;
update(id * 2, l, m, u, v, idx);
update(id * 2 + 1, m + 1, r, u, v, idx);
}
int dist1[maxn], dist2[maxn];
void bfs(int st, int dist[]) {
deque <int> q;
// cout << lim << endl;
for (int i = 1; i <= 4 * n; i++) dist[i] = oo;
dist[st] = 0;
// cout << st << " " << dist[st] << endl;
q.push_back(st);
while(!q.empty()) {
int z = q.front();
q.pop_front();
for (auto e : g[z]) {
int x = e.fi, y = e.se;
if (dist[x] > dist[z] + y) {
dist[x] = dist[z] + y;
if (y == 0) q.push_front(x);
else q.push_back(x);
}
}
}
}
int d[maxn];
void djk() {
priority_queue <ii, vector <ii>, greater <ii>> q;
for (int i = 1; i <= 4 * n; i++) d[i] = oo;
for (int i = 1; i <= n; i++) {
d[st[i]] = dist1[st[i]] + dist2[st[i]] - (i != 1 && i != n);
q.push({d[st[i]], st[i]});
// cout << d[st[i]] << " " << st[i] << endl;
}
while(!q.empty()) {
int cost = q.top().fi, z = q.top().se;
q.pop();
for (auto e : g[z]) {
int x = e.fi, y = e.se;
if (d[x] > d[z] + y) {
d[x] = d[z] + y;
q.push({d[x], x});
}
}
}
}
main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
}
build(1, 1, n);
// cout << lim << endl;
for (int i = 1; i <= n; i++) {
// cout << l[i] << " " << r[i] << endl;
update(1, 1, n, l[i], r[i], st[i]);
}
bfs(st[1], dist1);
bfs(st[n], dist2);
djk();
int t = 1; cin >> t;
while(t--) {
int x; cin >> x;
if (d[st[x]] >= oo) cout << "-1" << endl;
else cout << d[st[x]] << endl;
}
}
컴파일 시 표준 에러 (stderr) 메시지
passport.cpp:117:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
117 | main() {
| ^~~~
passport.cpp: In function 'int main()':
passport.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
passport.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |