Submission #1118778

# Submission time Handle Problem Language Result Execution time Memory
1118778 2024-11-26T06:18:37 Z salmon Passport (JOI23_passport) C++14
100 / 100
1632 ms 200320 KB
#include <bits/stdc++.h>
using namespace std;

int N;
int l[200100],r[200100];
queue<pair<int,int>> q;
int d0[200100];
int dn[200100];
int ans[200100];
int Q;
const int inf = 1e9;
vector<pair<int,int>> v;

struct node{

    int s,e,m;
    vector<int> v;
    node *l,*r;

    node(int S, int E){
        s = S;
        e = E;
        m = (s + e)/2;
        v = {};

        if(s != e){
            l = new node(s,m);
            r = new node(m + 1, e);
        }
    }

    void update(int S, int E, int k){
        if(S <= s && e <= E){
            v.push_back(k);
            return;
        }

        if(S <= m){
            l -> update(S,E,k);
        }
        if(m < E){
            r -> update(S,E,k);
        }
    }

    void pop(int i, int k){
        while(!v.empty()){
            q.push({v.back(),k});
            v.pop_back();
        }

        if(s == e) return;

        if(i <= m) l -> pop(i,k);
        else r -> pop(i,k);
    }
}*root;

struct fode{

    int s,e,m;
    int v;
    fode *l, *r;

    fode(int S, int E, int *lst){
        s = S;
        e = E;
        m = (s + e)/2;

        if(s == e){
            if(lst[s] != -1) v = lst[s];
            else v = inf;
        }
        else{
            l = new fode(s,m,lst);
            r = new fode(m + 1,e,lst);

            v = min(l -> v, r -> v);
        }
    }

    int query(int S, int E){
        if(S <= s && e <= E) return v;

        int V = inf;

        if(S <= m) V = min(V,l -> query(S,E));
        if(m < E) V = min(V,r -> query(S,E));

        return V;
    }
}*noot, *ooot;

int main(){

    scanf(" %d",&N);

    root = new node(0,N-1);

    for(int i = 0; i < N; i++){
        scanf(" %d",&l[i]);
        scanf(" %d",&r[i]);

        l[i]--;
        r[i]--;
    }

    for(int i = 0; i < N; i++){
        root -> update(l[i],r[i],i);
        d0[i] = -1;
    }

    root -> pop(0,1);

    while(!q.empty()){
        pair<int,int> ii = q.front();
        q.pop();

        int i = ii.first;

        if(d0[i] != -1) continue;

        d0[i] = ii.second;
        root -> pop(i,ii.second + 1);
    }

    root = new node(0,N-1);

    for(int i = 0; i < N; i++){
        root -> update(l[i],r[i],i);
        dn[i] = -1;
    }

    root -> pop(N - 1, 1);

    while(!q.empty()){
        pair<int,int> ii = q.front();
        q.pop();

        int i = ii.first;

        if(dn[i] != -1) continue;

        dn[i] = ii.second;
        root -> pop(i,ii.second + 1);
    }

    ooot = new fode(0,N-1,d0);
    noot = new fode(0,N-1,dn);
    root = new node(0,N-1);

    for(int i = 0; i < N; i++){
        int num = 1;

        if(d0[i] >= 0 && dn[i] >= 0) num += min(d0[i] - 1, ooot -> query(l[i],r[i])) + min(dn[i] - 1, noot -> query(l[i],r[i]));
        else num = inf;

        if(num <= inf - 10) v.push_back({num,i});
        root -> update(l[i],r[i],i);
        ans[i] = -1;
    }

    sort(v.begin(),v.end());

    auto it = 0;

    while(!q.empty() || it != v.size()){
        pair<int,int> ii;

        if(it == v.size()){
            ii = q.front();
            q.pop();
        }
        else if(q.empty()){
            ii = make_pair(v[it].second,v[it].first);
            it++;
        }
        else{
            if(q.front().second <= v[it].first){
                ii = q.front();
                q.pop();
            }
            else{
                ii = make_pair(v[it].second,v[it].first);
                it++;
            }
        }

        int i = ii.first;

        if(ans[i] != -1) continue;
        ans[i] = ii.second;

        root -> pop(i,ans[i] + 1);
    }

    scanf(" %d",&Q);

    for(int i = 0 ; i < Q; i++){
        int h;
        scanf(" %d",&h);
        h--;
        printf("%d\n",ans[h]);
    }
}

Compilation message

passport.cpp: In function 'int main()':
passport.cpp:167:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |     while(!q.empty() || it != v.size()){
      |                         ~~~^~~~~~~~~~~
passport.cpp:170:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |         if(it == v.size()){
      |            ~~~^~~~~~~~~~~
passport.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |     scanf(" %d",&N);
      |     ~~~~~^~~~~~~~~~
passport.cpp:101:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         scanf(" %d",&l[i]);
      |         ~~~~~^~~~~~~~~~~~~
passport.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         scanf(" %d",&r[i]);
      |         ~~~~~^~~~~~~~~~~~~
passport.cpp:197:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  197 |     scanf(" %d",&Q);
      |     ~~~~~^~~~~~~~~~
passport.cpp:201:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  201 |         scanf(" %d",&h);
      |         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1611 ms 197844 KB Output is correct
5 Correct 523 ms 151524 KB Output is correct
6 Correct 344 ms 155528 KB Output is correct
7 Correct 285 ms 121240 KB Output is correct
8 Correct 204 ms 113756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 2 ms 352 KB Output is correct
6 Correct 1 ms 608 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 2 ms 608 KB Output is correct
13 Correct 2 ms 608 KB Output is correct
14 Correct 2 ms 608 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 2 ms 352 KB Output is correct
6 Correct 1 ms 608 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 2 ms 608 KB Output is correct
13 Correct 2 ms 608 KB Output is correct
14 Correct 2 ms 608 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 9 ms 2400 KB Output is correct
17 Correct 7 ms 2144 KB Output is correct
18 Correct 9 ms 2400 KB Output is correct
19 Correct 10 ms 2400 KB Output is correct
20 Correct 6 ms 2400 KB Output is correct
21 Correct 5 ms 2352 KB Output is correct
22 Correct 4 ms 1888 KB Output is correct
23 Correct 6 ms 2388 KB Output is correct
24 Correct 6 ms 2136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 2 ms 352 KB Output is correct
6 Correct 1 ms 608 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 2 ms 608 KB Output is correct
13 Correct 2 ms 608 KB Output is correct
14 Correct 2 ms 608 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 9 ms 2400 KB Output is correct
17 Correct 7 ms 2144 KB Output is correct
18 Correct 9 ms 2400 KB Output is correct
19 Correct 10 ms 2400 KB Output is correct
20 Correct 6 ms 2400 KB Output is correct
21 Correct 5 ms 2352 KB Output is correct
22 Correct 4 ms 1888 KB Output is correct
23 Correct 6 ms 2388 KB Output is correct
24 Correct 6 ms 2136 KB Output is correct
25 Correct 2 ms 472 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 7 ms 2388 KB Output is correct
28 Correct 7 ms 2348 KB Output is correct
29 Correct 6 ms 2388 KB Output is correct
30 Correct 7 ms 2336 KB Output is correct
31 Correct 8 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1611 ms 197844 KB Output is correct
5 Correct 523 ms 151524 KB Output is correct
6 Correct 344 ms 155528 KB Output is correct
7 Correct 285 ms 121240 KB Output is correct
8 Correct 204 ms 113756 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Correct 1 ms 352 KB Output is correct
13 Correct 2 ms 352 KB Output is correct
14 Correct 1 ms 608 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 352 KB Output is correct
17 Correct 1 ms 352 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Correct 2 ms 608 KB Output is correct
20 Correct 2 ms 608 KB Output is correct
21 Correct 2 ms 608 KB Output is correct
22 Correct 2 ms 608 KB Output is correct
23 Correct 1 ms 608 KB Output is correct
24 Correct 9 ms 2400 KB Output is correct
25 Correct 7 ms 2144 KB Output is correct
26 Correct 9 ms 2400 KB Output is correct
27 Correct 10 ms 2400 KB Output is correct
28 Correct 6 ms 2400 KB Output is correct
29 Correct 5 ms 2352 KB Output is correct
30 Correct 4 ms 1888 KB Output is correct
31 Correct 6 ms 2388 KB Output is correct
32 Correct 6 ms 2136 KB Output is correct
33 Correct 2 ms 472 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 7 ms 2388 KB Output is correct
36 Correct 7 ms 2348 KB Output is correct
37 Correct 6 ms 2388 KB Output is correct
38 Correct 7 ms 2336 KB Output is correct
39 Correct 8 ms 2388 KB Output is correct
40 Correct 1632 ms 200320 KB Output is correct
41 Correct 557 ms 155172 KB Output is correct
42 Correct 820 ms 184204 KB Output is correct
43 Correct 831 ms 190948 KB Output is correct
44 Correct 406 ms 158148 KB Output is correct
45 Correct 526 ms 156916 KB Output is correct
46 Correct 211 ms 59596 KB Output is correct
47 Correct 504 ms 167756 KB Output is correct
48 Correct 915 ms 159520 KB Output is correct