답안 #729021

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
729021 2023-04-23T11:55:20 Z MilosMilutinovic Passport (JOI23_passport) C++14
48 / 100
2000 ms 208592 KB
#include <bits/stdc++.h>
using namespace std;

const int inf=0x3f3f3f3f;
int n,q,l[200005],r[200005],dl[800005],dr[800005],dis[800005],idx[200005];
vector<int> g[800005],gup[800005],gdown[800005];

void bfs(int* d)
{
    set<pair<int,int>> st;
    for(int i=1;i<=n;i++) st.emplace(d[i],i);
    while(!st.empty())
    {
        auto it=st.begin();
        int x=it->second;
        st.erase(it);
        if(x<=n)
        {
            for(int y=idx[x];y>=1;y/=2)
            {
                if(d[n+y]>d[x])
                {
                    if(st.find({d[n+y],n+y})!=st.end()) st.erase(st.find({d[n+y],n+y}));
                    d[n+y]=d[x];st.emplace(d[n+y],n+y);
                }
            }
        }
        else
        {
            for(int y:g[x])
            {
                int f=d[x]+(y<=n);
                if(d[y]>f)
                {
                    if(st.find({d[y],y})!=st.end()) st.erase(st.find({d[y],y}));
                    d[y]=f;st.emplace(f,y);
                }
            }
        }
    }
}

void build(int v,int l,int r)
{
    if(l==r) {idx[l]=v;return;}
    int mid=l+r>>1;
    gup[n+2*v].push_back(n+v);
    gup[n+2*v+1].push_back(n+v);
    gdown[n+v].push_back(n+2*v);
    gdown[n+v].push_back(n+2*v+1);
    build(v*2,l,mid);
    build(v*2+1,mid+1,r);
}
void add(int v,int l,int r,int ql,int qr,int i)
{
    if(l>r||l>qr||r<ql) return;
    if(ql<=l&&r<=qr)
    {
        g[i].push_back(n+v);
        g[n+v].push_back(i);
        gup[i].push_back(n+v);
        gup[n+v].push_back(i);
        gdown[i].push_back(n+v);
        gdown[n+v].push_back(i);
        return;
    }
    int mid=l+r>>1;
    add(v*2,l,mid,ql,qr,i);
    add(v*2+1,mid+1,r,ql,qr,i);
}

signed main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d%d",&l[i],&r[i]);
    build(1,1,n);
    for(int i=1;i<=n;i++) add(1,1,n,l[i],r[i],i);
    for(int i=1;i<=800000;i++) dl[i]=dr[i]=dis[i]=inf;
    dl[1]=0;dr[n]=0;
    bfs(dl);bfs(dr);
    dl[1]=1;dr[n]=1;
    for(int i=1;i<=n;i++) dis[i]=dl[i]+dr[i];
    bfs(dis);
    for(int i=1;i<=n;i++) if(dis[i]>=inf) dis[i]=-1; else dis[i]--;
    scanf("%d",&q);
    while(q--)
    {
        int x;scanf("%d",&x);
        printf("%d\n",dis[x]);
    }
}

Compilation message

passport.cpp: In function 'void build(int, int, int)':
passport.cpp:46:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |     int mid=l+r>>1;
      |             ~^~
passport.cpp: In function 'void add(int, int, int, int, int, int)':
passport.cpp:67:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |     int mid=l+r>>1;
      |             ~^~
passport.cpp: In function 'int main()':
passport.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
passport.cpp:75:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     for(int i=1;i<=n;i++) scanf("%d%d",&l[i],&r[i]);
      |                           ~~~~~^~~~~~~~~~~~~~~~~~~~
passport.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
passport.cpp:88:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         int x;scanf("%d",&x);
      |               ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 66004 KB Output is correct
2 Correct 38 ms 65992 KB Output is correct
3 Correct 34 ms 65992 KB Output is correct
4 Execution timed out 2086 ms 208592 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 65976 KB Output is correct
2 Correct 32 ms 66052 KB Output is correct
3 Correct 32 ms 66044 KB Output is correct
4 Correct 33 ms 65992 KB Output is correct
5 Correct 32 ms 66052 KB Output is correct
6 Correct 41 ms 65952 KB Output is correct
7 Correct 32 ms 66000 KB Output is correct
8 Correct 33 ms 65996 KB Output is correct
9 Correct 33 ms 66064 KB Output is correct
10 Correct 42 ms 65996 KB Output is correct
11 Correct 43 ms 66080 KB Output is correct
12 Correct 47 ms 66044 KB Output is correct
13 Correct 35 ms 66128 KB Output is correct
14 Correct 34 ms 66076 KB Output is correct
15 Correct 33 ms 66076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 65976 KB Output is correct
2 Correct 32 ms 66052 KB Output is correct
3 Correct 32 ms 66044 KB Output is correct
4 Correct 33 ms 65992 KB Output is correct
5 Correct 32 ms 66052 KB Output is correct
6 Correct 41 ms 65952 KB Output is correct
7 Correct 32 ms 66000 KB Output is correct
8 Correct 33 ms 65996 KB Output is correct
9 Correct 33 ms 66064 KB Output is correct
10 Correct 42 ms 65996 KB Output is correct
11 Correct 43 ms 66080 KB Output is correct
12 Correct 47 ms 66044 KB Output is correct
13 Correct 35 ms 66128 KB Output is correct
14 Correct 34 ms 66076 KB Output is correct
15 Correct 33 ms 66076 KB Output is correct
16 Correct 44 ms 67156 KB Output is correct
17 Correct 43 ms 66928 KB Output is correct
18 Correct 50 ms 67404 KB Output is correct
19 Correct 46 ms 67508 KB Output is correct
20 Correct 43 ms 66928 KB Output is correct
21 Correct 38 ms 67008 KB Output is correct
22 Correct 45 ms 66784 KB Output is correct
23 Correct 41 ms 67072 KB Output is correct
24 Correct 48 ms 67276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 65976 KB Output is correct
2 Correct 32 ms 66052 KB Output is correct
3 Correct 32 ms 66044 KB Output is correct
4 Correct 33 ms 65992 KB Output is correct
5 Correct 32 ms 66052 KB Output is correct
6 Correct 41 ms 65952 KB Output is correct
7 Correct 32 ms 66000 KB Output is correct
8 Correct 33 ms 65996 KB Output is correct
9 Correct 33 ms 66064 KB Output is correct
10 Correct 42 ms 65996 KB Output is correct
11 Correct 43 ms 66080 KB Output is correct
12 Correct 47 ms 66044 KB Output is correct
13 Correct 35 ms 66128 KB Output is correct
14 Correct 34 ms 66076 KB Output is correct
15 Correct 33 ms 66076 KB Output is correct
16 Correct 44 ms 67156 KB Output is correct
17 Correct 43 ms 66928 KB Output is correct
18 Correct 50 ms 67404 KB Output is correct
19 Correct 46 ms 67508 KB Output is correct
20 Correct 43 ms 66928 KB Output is correct
21 Correct 38 ms 67008 KB Output is correct
22 Correct 45 ms 66784 KB Output is correct
23 Correct 41 ms 67072 KB Output is correct
24 Correct 48 ms 67276 KB Output is correct
25 Correct 32 ms 66064 KB Output is correct
26 Correct 31 ms 66064 KB Output is correct
27 Correct 45 ms 67292 KB Output is correct
28 Correct 48 ms 66912 KB Output is correct
29 Correct 39 ms 67024 KB Output is correct
30 Correct 40 ms 66952 KB Output is correct
31 Correct 39 ms 67060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 66004 KB Output is correct
2 Correct 38 ms 65992 KB Output is correct
3 Correct 34 ms 65992 KB Output is correct
4 Execution timed out 2086 ms 208592 KB Time limit exceeded
5 Halted 0 ms 0 KB -