# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
878492 | prohacker | Passport (JOI23_passport) | C++14 | 2029 ms | 74376 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int,int>
using namespace std;
const int N = 2e5+10;
const int INF = INT_MAX;
const int mod = 1e9+7;
const string NAME = "solve";
int n,cnt;
int d[3][4*N];
vector<pii> adj[4*N];
int idx[4*N];
int q;
void build(int id = 1, int l = 1, int r = n) {
if(l == r) {
idx[id] = l;
return;
}
idx[id] = ++cnt;
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid+1,r);
adj[idx[id << 1]].push_back({idx[id],0});
adj[idx[id << 1 | 1]].push_back({idx[id],0});
}
void update(int root, int u, int v, int id = 1, int l = 1, int r = n) {
if(u <= l and r <= v) {
adj[idx[id]].push_back({root,1});
return;
}
int mid = l + r >> 1;
if(u <= mid) {
update(root,u,v,id << 1,l,mid);
}
if(mid+1 <= v) {
update(root,u,v,id << 1 | 1,mid+1,r);
}
}
queue<int> pq;
void dijkstra(int d[]) {
while(!pq.empty()) {
int u = pq.front();
pq.pop();
for(auto p:adj[u]) {
int v = p.first;
int w = p.second;
if(d[u] + w < d[v]) {
d[v] = d[u] + w;
pq.push(v);
}
}
}
}
signed main()
{
if (fopen((NAME + ".inp").c_str(), "r")) {
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
cnt = n;
build();
for(int i = 1 ; i <= n ; i++) {
int l,r; cin >> l >> r;
update(i,l,r);
}
for(int j = 0 ; j < 3 ; j++) {
for(int i = 1 ; i <= cnt ; i++) {
d[j][i] = 1e8;
}
}
d[0][1] = 0;
pq.push(1);
dijkstra(d[0]);
d[1][n] = 0;
pq.push(n);
dijkstra(d[1]);
for(int i = 1 ; i <= cnt ; i++) {
d[2][i] = d[0][i] + d[1][i];
if(1 < i and i < n) {
d[2][i]--;
}
pq.push(i);
}
dijkstra(d[2]);
cin >> q;
while(q--) {
int x; cin >> x;
if(d[2][x] >= 1e8) {
d[2][x] = -1;
}
cout << d[2][x] << '\n';
}
return 0;
}
Compilation message (stderr)
# | 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... |