제출 #1211779

#제출 시각아이디문제언어결과실행 시간메모리
1211779Zbyszek99Passport (JOI23_passport)C++20
100 / 100
1075 ms127864 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

struct node
{
    pii min_ = {1e9,0};
    pii max_ = {-1e9,0};
    node operator+(const node& other)
    {
        node res;
        res.min_ = min(min_,other.min_);
        res.max_ = max(max_,other.max_);
        return res;
    }
};

const int tree_siz = 1024*512-1;
node drzewo[tree_siz+1];

node get_seg(int akt, int p1, int p2, int s1, int s2)
{
    if(p2 < s1 || p1 > s2) return {{1e9,0},{-1e9,0}};
    if(p1 >= s1 && p2 <= s2) return drzewo[akt];
    return get_seg(akt*2,p1,(p1+p2)/2,s1,s2) + get_seg(akt*2+1,(p1+p2)/2+1,p2,s1,s2);
}

vector<pii> graph[1000'001];
int cur_vert;
int start_vert;
int L[200'001];
int R[200'001];
int best_left[200'001];
int best_right[200'001];

int left_ans[200'001];
int right_ans[200'001];
int dist[1000'001];
int real_vert[1000'001];
bool odw[1000'001];
int n;

int bulid_tree(int akt, int l, int r)
{
    if(l == r)
    {
        real_vert[akt] = cur_vert++;
        if(l < n) graph[l].pb({real_vert[akt],0});
        return real_vert[akt];
    }
    int left = bulid_tree(akt*2,l,(l+r)/2);
    int right = bulid_tree(akt*2+1,(l+r)/2+1,r);
    real_vert[akt] = cur_vert++;
    graph[left].pb({real_vert[akt],0});
    graph[right].pb({real_vert[akt],0});
    return real_vert[akt];
}

void add_edge(int akt, int p1, int p2, int s1, int s2, int v)
{
    if(p2 < s1 || p1 > s2) return;
    if(p1 >= s1 && p2 <= s2)
    {
        graph[real_vert[akt]].pb({v,1});
        return;
    }
    add_edge(akt*2,p1,(p1+p2)/2,s1,s2,v);
    add_edge(akt*2+1,(p1+p2)/2+1,p2,s1,s2,v);
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    cin >> n;
    start_vert = n;
    cur_vert = n+1;
    rep(i,n)
    {
        dist[i] = -1;
        cin >> L[i] >> R[i];
        L[i]--;
        R[i]--;
        drzewo[tree_siz/2+1+i] = {{L[i],i},{R[i],i}};
    }
    for(int i = tree_siz/2; i >= 1; i--)
    {
        drzewo[i] = drzewo[i*2] + drzewo[i*2+1];
    }
    rep(i,n)
    {
        node best = get_seg(1,0,tree_siz/2,L[i],R[i]);
        if(best.min_.ff != L[i]) best_left[i] = best.min_.ss;
        else best_left[i] = -1;
        if(best.max_.ff != R[i]) best_right[i] = best.max_.ss;
        else best_right[i] = -1;
    }
    vector<pii> v_sort;
    rep(i,n) v_sort.pb({L[i],i});
    sort(all(v_sort));
    forall(it,v_sort)
    {
        int i = it.ss;
        if(best_left[i] == -1) left_ans[i] = -1;
        else if(left_ans[best_left[i]] != -1) left_ans[i] = left_ans[best_left[i]]+1;
        else left_ans[i] = -1;
        if(L[i] == 0) left_ans[i] = 1;
    }
    v_sort = {};
    rep(i,n) v_sort.pb({-R[i],i});
    sort(all(v_sort));
    forall(it,v_sort)
    {
        int i = it.ss;
        if(best_right[i] == -1) right_ans[i] = -1;
        else if(right_ans[best_right[i]] != -1) right_ans[i] = right_ans[best_right[i]]+1;
        else right_ans[i] = -1;
        if(R[i] == n-1) right_ans[i] = 1;
    }
    rep(i,n)
    {
        if(left_ans[i] != -1 && right_ans[i] != -1) graph[start_vert].pb({i,left_ans[i] + right_ans[i]-1});
    }
    bulid_tree(1,0,tree_siz/2);
    rep(i,n) add_edge(1,0,tree_siz/2,L[i],R[i],i);
    priority_queue<pii,vector<pii>,greater<pii>> pq;
    pq.push({0,start_vert});
    while(!pq.empty())
    {
        pii t = pq.top();
        pq.pop();
        if(odw[t.ss]) continue;
        odw[t.ss] = 1;
        dist[t.ss] = t.ff;
        forall(it,graph[t.ss])
        {
            pq.push({t.ff+it.ss,it.ff});
        }
    }
    int q;
    cin >> q;
    rep(qq,q)
    {
        int x;
        cin >> x;
        x--;
        cout << dist[x] << "\n";
    }
}
#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...