제출 #1270226

#제출 시각아이디문제언어결과실행 시간메모리
1270226bhnmPassport (JOI23_passport)C++17
0 / 100
25 ms47432 KiB
#include<bits/stdc++.h>
#define ll int
#define pll pair<ll,ll>
using namespace std;
const ll maxn = 2e6 + 5;
const ll inf = 1e8;
const ll mod = 1e9 + 7;
ll n,q,x;
ll l[maxn];
ll r[maxn];
ll idx[maxn];
ll dist[maxn];
ll dist1[maxn];
ll distn[maxn];
ll p[maxn];
vector<pll>g[maxn];
deque<ll>de;
void build(ll id,ll l,ll r)
{
    if(l == r)
    {
        g[idx[l]].push_back({id,0});
        return;
    }
    build(id<<1,l,(l+r)/2);
    build(id<<1|1,(l+r)/2+1,r);
    g[id<<1].push_back({id,0});
    g[id<<1|1].push_back({id,0});
}
void update(ll id,ll l,ll r,ll u,ll v,ll x)
{
    if(l >= u && r <= v)
    {
        g[id].push_back({idx[x],1});
        return;
    }
    if(l > v || r < u)
    {
        return;
    }
    update(id<<1,l,(l+r)/2,u,v,x);
    update(id<<1|1,(l+r)/2+1,r,u,v,x);
}
void bfs01(ll st,ll val,bool check)
{
    if(!check)
    {
        for(ll i = 1;i<=5*n;++i)
        {
            dist[i] = inf;
        }
    }
    dist[st] = val;
    de.push_back(st);
    while(!de.empty())
    {
        ll u = de.front();
        de.pop_front();
        for(pll v : g[u])
        {
            if(dist[v.first] > dist[u] + v.second)
            {
                dist[v.first] = dist[u] + v.second;
                if(!v.second)
                {
                    de.push_front(v.first);
                }
                else
                {
                    de.push_back(v.first);
                }
            }
        }
    }
}
bool cmp(ll a,ll b)
{
    return dist[a] < dist[b];
}
void solve()
{
    cin>>n;
    for(ll i = 1;i<=n;++i)
    {
        cin>>l[i]>>r[i];
        idx[i] = 4 * n + i;
    }
    build(1,1,n);
    for(ll i = 1;i<=n;++i)
    {
        update(1,1,n,l[i],r[i],i);
    }
    bfs01(idx[1],0,false);
    for(ll i = 1;i<=5*n;++i)
    {
        dist1[i] = dist[i];
    }
    bfs01(idx[n],0,false);
    for(ll i = 1;i<=5*n;++i)
    {
        distn[i] = dist[i];
    }
    for(ll i = 1;i<=4*n;++i)
    {
        dist[i] = inf;
    }
    for(ll i = 4 * n + 1;i<=5*n;++i)
    {
        dist[i] = dist1[i] + distn[i];
        if(i > 4 * n + 1 && i < 5 * n)
        {
            dist[i]--;
        }
        p[i] = i;
    }
    sort(p+1,p+5*n+1,cmp);
    for(ll i = 1;i<=5*n;++i)
    {
        bfs01(p[i],dist[p[i]],true);
    }
    cin>>q;
    for(ll i = 1;i<=q;++i)
    {
        cin>>x;
        cout<<dist[idx[x]]<<'\n';
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t;
    t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...