답안 #898385

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
898385 2024-01-04T15:06:46 Z vjudge1 Passport (JOI23_passport) C++17
6 / 100
56 ms 48980 KB
#include <bits/stdc++.h>
using namespace std;



template<class T>
int len(T &a){
    return a.size();
}

using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";


mt19937 mt(time(nullptr));
const int mod = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;



struct Node{
    int l, r, i;
};

struct Sparsemin{


    vector<vector<Node>> t;
    int n, logn;

    void init(int _n){
        n = _n;
        logn = 32 - __builtin_clz(n);
        t.assign(logn, vector<Node> (n));
    }

    Node merge(Node a, Node b){
        if(a.l < b.l) return a;
        if(b.l < a.l) return b;
        if(a.r > b.r) return a;
        if(b.r > a.r) return b;
        if(a.i < b.i) return a;
        return b;
    }

    void build(vector<int> &l, vector<int> &r){
        init(len(l));

        for(int i = 0; i < n; i ++){
            t[0][i] = {l[i], r[i], i};
        }

        for(int j = 1; j < logn; j ++){
            for(int i = 0; i + (1 << j) - 1 < n; i ++){
                t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
            }
        }
    }

    int get(int l, int r){
        int k = 31 - __builtin_clz(r - l + 1);
        return merge(t[k][l], t[k][l + (1 << k)]).i;
    }
};



struct Sparsemax{
    vector<vector<Node>> t;
    int n, logn;

    void init(int _n){
        n = _n;
        logn = 32 - __builtin_clz(n);
        t.assign(logn, vector<Node> (n));
    }

    Node merge(Node a, Node b){
        if(a.r > b.r) return a;
        if(b.r > a.r) return b;
        if(a.l < b.l) return a;
        if(b.l < a.l) return b;
        if(a.i > b.i) return a;
        return b;
    }

    void build(vector<int> &l, vector<int> &r){
        init(len(l));

        for(int i = 0; i < n; i ++){
            t[0][i] = {l[i], r[i], i};
        }

        for(int j = 1; j < logn; j ++){
            for(int i = 0; i + (1 << j) - 1 < n; i ++){
                t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
            }
        }
    }

    int get(int l, int r){
        int k = 31 - __builtin_clz(r - l + 1);
        //cout << l << ' ' << r << ' ' << k << endl;
        return merge(t[k][l], t[k][r - (1 << k) + 1]).i;
    }
};



void solve(){
    int n; cin >> n;
    vector<int> l(n), r(n);
    for(int i = 0; i < n; i ++){
        cin >> l[i] >> r[i];
        l[i] --; r[i] --;
    }
    Sparsemax tmx;
    tmx.build(l, r);

    int q; cin >> q;

    while(q --){
        int x; cin >> x;
        int cnt = 1; x--;
        while(r[x] != n - 1){
            int nx = tmx.get(l[x], r[x]);
            if(r[nx] == r[x]){
                cnt = -1; break;
            }
            x = nx;
            cnt ++;
        }
        cout << cnt << '\n';
    }
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    auto start = chrono::high_resolution_clock::now();
    #ifndef LOCAL
    if(fileio.size()){
        freopen((fileio + ".in").c_str(), "r", stdin);
        freopen((fileio + ".out").c_str(), "w", stdout);
    }
    #endif
    int testcases = 1;
    #ifdef Tests
    cin >> testcases;
    #endif
    while(testcases --){
        solve();
        cout << '\n';
        #ifdef LOCAL
        cout << "_________________________" << endl;
        #endif
    }
    #ifdef LOCAL
    auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
    
    cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
    #endif
    return 0;
}

Compilation message

passport.cpp: In function 'int main()':
passport.cpp:145:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
  145 |     auto start = chrono::high_resolution_clock::now();
      |          ^~~~~
passport.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |         freopen((fileio + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |         freopen((fileio + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 47 ms 48196 KB Output is correct
5 Correct 56 ms 48720 KB Output is correct
6 Correct 48 ms 48980 KB Output is correct
7 Correct 43 ms 47196 KB Output is correct
8 Correct 37 ms 38236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 0 ms 344 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 0 ms 344 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 0 ms 344 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 47 ms 48196 KB Output is correct
5 Correct 56 ms 48720 KB Output is correct
6 Correct 48 ms 48980 KB Output is correct
7 Correct 43 ms 47196 KB Output is correct
8 Correct 37 ms 38236 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Incorrect 0 ms 344 KB Output isn't correct
11 Halted 0 ms 0 KB -