Submission #282883

# Submission time Handle Problem Language Result Execution time Memory
282883 2020-08-25T06:23:04 Z 최은수(#5745) Iqea (innopolis2018_final_C) C++17
36 / 100
2000 ms 89596 KB
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
struct dsu
{
    int pa[100010];
    inline void init()
    {
        fill(pa,pa+100010,-1);
        return;
    }
    dsu(){init();}
    int par(int x)
    {
        if(pa[x]==-1)
            return x;
        return pa[x]=par(pa[x]);
    }
    inline void uni(int x,int y)
    {
        x=par(x);
        y=par(y);
        if(x==y)
            return;
        pa[x]=y;
        return;
    }
}uf1,uf2;
vector<int>adj[200010];
int sz[200010];
void sdfs(int x,int p)
{
    sz[x]=1;
    for(int&t:adj[x])
        if(t!=p)
            sdfs(t,x),sz[x]+=sz[t];
    return;
}
bool chk[200010];
int dis[200010][18];
void dfs(int x,int p,int d,int cd)
{
    dis[x][cd]=d;
    sz[x]=1;
    for(int&t:adj[x])
        if(!chk[t]&&t!=p)
            dfs(t,x,d+1,cd),sz[x]+=sz[t];
    return;
}
int get(int x,int p,int z)
{
    int ms=-1,mi=-1;
    for(int&t:adj[x])
        if(!chk[t]&&t!=p&&sz[t]>ms)
            ms=sz[t],mi=t;
    if(ms<=sz[z]/2)
        return x;
    return get(mi,x,z);
}
int cdep[200010];
int cpa[200010];
void dnc(int x)
{
    chk[x]=1;
    for(int&t:adj[x])
    {
        if(chk[t])
            continue;
        dfs(t,x,1,cdep[x]);
        int nc=get(t,x,t);
        cpa[nc]=x;
        cdep[nc]=cdep[x]+1;
        dnc(nc);
    }
    return;
}
map<int,int>dmp[100010];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    vector<pi>p(n);
    for(pi&t:p)
        cin>>t.fi>>t.se;
    vector<pi>sxv=p,syv=p;
    auto cmp1=[](const pi&x,const pi&y){return x<y;};
    auto cmp2=[](const pi&x,const pi&y){return pi(x.se,x.fi)<pi(y.se,y.fi);};
    sort(all(sxv),cmp1);
    sort(all(syv),cmp2);
    for(int i=1;i<n;i++)
        if(sxv[i].fi==sxv[i-1].fi&&sxv[i].se==sxv[i-1].se+1)
            uf1.uni(i,i-1);
    for(int i=1;i<n;i++)
        if(syv[i].fi==syv[i-1].fi+1&&syv[i].se==syv[i-1].se)
            uf2.uni(i,i-1);
    for(int i=1;i<n;i++)
    {
        if(sxv[i].fi==sxv[i-1].fi&&sxv[i].se==sxv[i-1].se+1)
        {
            int p1=uf2.par(lower_bound(all(syv),sxv[i-1],cmp2)-syv.begin())+n;
            int p2=uf2.par(lower_bound(all(syv),sxv[i],cmp2)-syv.begin())+n;
            adj[p1].eb(p2);
            adj[p2].eb(p1);
        }
    }
    for(int i=1;i<n;i++)
    {
        if(syv[i].fi==syv[i-1].fi+1&&syv[i].se==syv[i-1].se)
        {
            int p1=uf1.par(lower_bound(all(sxv),syv[i-1],cmp1)-sxv.begin());
            int p2=uf1.par(lower_bound(all(sxv),syv[i],cmp1)-sxv.begin());
            adj[p1].eb(p2);
            adj[p2].eb(p1);
        }
    }
    for(int i=0;i<n*2;i++)
    {
        sort(all(adj[i]));
        adj[i].erase(unique(all(adj[i])),adj[i].end());
    }
    sdfs(uf1.par(0),-1);
    int c1=get(uf1.par(0),-1,uf1.par(0));
    cpa[c1]=-1;
    dnc(c1);
    sdfs(uf2.par(0)+n,-1);
    int c2=get(uf2.par(0)+n,-1,uf2.par(0)+n);
    cpa[c2]=-1;
    dnc(c2);
    int q;
    cin>>q;
    while(q-->0)
    {
        int t,x,y;
        cin>>t>>x>>y;
        int p1=uf1.par(lower_bound(all(sxv),pi(x,y),cmp1)-sxv.begin());
        int p2=uf2.par(lower_bound(all(syv),pi(x,y),cmp2)-syv.begin())+n;
        vector<int>cp1,cp2;
        while(p1!=-1)
            cp1.eb(p1),p1=cpa[p1];
        while(p2!=-1)
            cp2.eb(p2),p2=cpa[p2];
        p1=cp1[0],p2=cp2[0];
        if(t==1)
            for(int&u:cp1)
                for(int&v:cp2)
                    dmp[u][v]=min(dmp[u][v],dis[p1][cdep[u]]+dis[p2][cdep[v]]-inf);
        else
        {
            int ans=0;
            for(int&u:cp1)
                for(int&v:cp2)
                    ans=min(ans,dmp[u][v]+dis[p1][cdep[u]]+dis[p2][cdep[v]]);
            if(ans==0)
                ans=-1;
            else
                ans+=inf;
            cout<<ans<<'\n';
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10496 KB Output is correct
2 Correct 7 ms 10496 KB Output is correct
3 Correct 8 ms 10528 KB Output is correct
4 Correct 27 ms 11928 KB Output is correct
5 Correct 28 ms 11904 KB Output is correct
6 Correct 27 ms 12024 KB Output is correct
7 Correct 38 ms 13048 KB Output is correct
8 Correct 40 ms 13176 KB Output is correct
9 Correct 40 ms 13176 KB Output is correct
10 Correct 41 ms 12920 KB Output is correct
11 Correct 44 ms 13048 KB Output is correct
12 Correct 43 ms 13180 KB Output is correct
13 Correct 40 ms 13176 KB Output is correct
14 Correct 41 ms 13176 KB Output is correct
15 Correct 47 ms 13176 KB Output is correct
16 Correct 39 ms 13048 KB Output is correct
17 Correct 37 ms 12920 KB Output is correct
18 Correct 42 ms 13048 KB Output is correct
19 Correct 42 ms 12792 KB Output is correct
20 Correct 45 ms 13248 KB Output is correct
21 Correct 41 ms 13176 KB Output is correct
22 Correct 46 ms 13564 KB Output is correct
23 Correct 46 ms 13560 KB Output is correct
24 Correct 52 ms 13816 KB Output is correct
25 Correct 21 ms 12152 KB Output is correct
26 Correct 24 ms 12800 KB Output is correct
27 Correct 21 ms 11768 KB Output is correct
28 Correct 56 ms 13868 KB Output is correct
29 Correct 50 ms 13816 KB Output is correct
30 Correct 52 ms 13944 KB Output is correct
31 Correct 50 ms 13944 KB Output is correct
32 Correct 66 ms 13840 KB Output is correct
33 Correct 59 ms 13816 KB Output is correct
34 Correct 52 ms 13816 KB Output is correct
35 Correct 58 ms 13944 KB Output is correct
36 Correct 16 ms 11776 KB Output is correct
37 Correct 56 ms 13816 KB Output is correct
38 Correct 56 ms 13816 KB Output is correct
39 Correct 61 ms 13920 KB Output is correct
40 Correct 58 ms 13944 KB Output is correct
41 Correct 57 ms 13692 KB Output is correct
42 Correct 48 ms 13568 KB Output is correct
43 Correct 19 ms 12032 KB Output is correct
44 Correct 50 ms 13560 KB Output is correct
45 Correct 50 ms 13560 KB Output is correct
46 Correct 21 ms 12064 KB Output is correct
47 Correct 39 ms 12792 KB Output is correct
48 Correct 35 ms 12576 KB Output is correct
49 Correct 35 ms 12408 KB Output is correct
50 Correct 32 ms 12288 KB Output is correct
51 Correct 30 ms 12152 KB Output is correct
52 Correct 39 ms 12792 KB Output is correct
53 Correct 37 ms 12928 KB Output is correct
54 Correct 41 ms 12800 KB Output is correct
55 Correct 41 ms 12920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10496 KB Output is correct
2 Correct 7 ms 10496 KB Output is correct
3 Correct 8 ms 10528 KB Output is correct
4 Correct 27 ms 11928 KB Output is correct
5 Correct 28 ms 11904 KB Output is correct
6 Correct 27 ms 12024 KB Output is correct
7 Correct 38 ms 13048 KB Output is correct
8 Correct 40 ms 13176 KB Output is correct
9 Correct 40 ms 13176 KB Output is correct
10 Correct 41 ms 12920 KB Output is correct
11 Correct 44 ms 13048 KB Output is correct
12 Correct 43 ms 13180 KB Output is correct
13 Correct 40 ms 13176 KB Output is correct
14 Correct 41 ms 13176 KB Output is correct
15 Correct 47 ms 13176 KB Output is correct
16 Correct 39 ms 13048 KB Output is correct
17 Correct 37 ms 12920 KB Output is correct
18 Correct 42 ms 13048 KB Output is correct
19 Correct 42 ms 12792 KB Output is correct
20 Correct 45 ms 13248 KB Output is correct
21 Correct 41 ms 13176 KB Output is correct
22 Correct 46 ms 13564 KB Output is correct
23 Correct 46 ms 13560 KB Output is correct
24 Correct 52 ms 13816 KB Output is correct
25 Correct 21 ms 12152 KB Output is correct
26 Correct 24 ms 12800 KB Output is correct
27 Correct 21 ms 11768 KB Output is correct
28 Correct 56 ms 13868 KB Output is correct
29 Correct 50 ms 13816 KB Output is correct
30 Correct 52 ms 13944 KB Output is correct
31 Correct 50 ms 13944 KB Output is correct
32 Correct 66 ms 13840 KB Output is correct
33 Correct 59 ms 13816 KB Output is correct
34 Correct 52 ms 13816 KB Output is correct
35 Correct 58 ms 13944 KB Output is correct
36 Correct 16 ms 11776 KB Output is correct
37 Correct 56 ms 13816 KB Output is correct
38 Correct 56 ms 13816 KB Output is correct
39 Correct 61 ms 13920 KB Output is correct
40 Correct 58 ms 13944 KB Output is correct
41 Correct 57 ms 13692 KB Output is correct
42 Correct 48 ms 13568 KB Output is correct
43 Correct 19 ms 12032 KB Output is correct
44 Correct 50 ms 13560 KB Output is correct
45 Correct 50 ms 13560 KB Output is correct
46 Correct 21 ms 12064 KB Output is correct
47 Correct 39 ms 12792 KB Output is correct
48 Correct 35 ms 12576 KB Output is correct
49 Correct 35 ms 12408 KB Output is correct
50 Correct 32 ms 12288 KB Output is correct
51 Correct 30 ms 12152 KB Output is correct
52 Correct 39 ms 12792 KB Output is correct
53 Correct 37 ms 12928 KB Output is correct
54 Correct 41 ms 12800 KB Output is correct
55 Correct 41 ms 12920 KB Output is correct
56 Correct 298 ms 12920 KB Output is correct
57 Correct 308 ms 12920 KB Output is correct
58 Correct 299 ms 13048 KB Output is correct
59 Correct 481 ms 14996 KB Output is correct
60 Correct 526 ms 14584 KB Output is correct
61 Correct 502 ms 15080 KB Output is correct
62 Correct 597 ms 14968 KB Output is correct
63 Correct 592 ms 15096 KB Output is correct
64 Correct 638 ms 15096 KB Output is correct
65 Correct 521 ms 14712 KB Output is correct
66 Correct 574 ms 14968 KB Output is correct
67 Correct 517 ms 14716 KB Output is correct
68 Correct 502 ms 15096 KB Output is correct
69 Correct 479 ms 14488 KB Output is correct
70 Correct 572 ms 14968 KB Output is correct
71 Correct 543 ms 14584 KB Output is correct
72 Correct 541 ms 15096 KB Output is correct
73 Correct 577 ms 14712 KB Output is correct
74 Correct 175 ms 13948 KB Output is correct
75 Correct 226 ms 14200 KB Output is correct
76 Correct 191 ms 13784 KB Output is correct
77 Correct 701 ms 15864 KB Output is correct
78 Correct 706 ms 15864 KB Output is correct
79 Correct 132 ms 13868 KB Output is correct
80 Correct 825 ms 15880 KB Output is correct
81 Correct 832 ms 16120 KB Output is correct
82 Correct 613 ms 15352 KB Output is correct
83 Correct 625 ms 15608 KB Output is correct
84 Correct 169 ms 13896 KB Output is correct
85 Correct 731 ms 15352 KB Output is correct
86 Correct 710 ms 15100 KB Output is correct
87 Correct 201 ms 13236 KB Output is correct
88 Correct 483 ms 14712 KB Output is correct
89 Correct 437 ms 14328 KB Output is correct
90 Correct 442 ms 14204 KB Output is correct
91 Correct 400 ms 13432 KB Output is correct
92 Correct 333 ms 13176 KB Output is correct
93 Correct 532 ms 14072 KB Output is correct
94 Correct 489 ms 14584 KB Output is correct
95 Correct 557 ms 14712 KB Output is correct
96 Correct 526 ms 14584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 759 ms 40020 KB Output is correct
2 Correct 325 ms 40008 KB Output is correct
3 Execution timed out 2071 ms 89596 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2047 ms 73528 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10496 KB Output is correct
2 Correct 7 ms 10496 KB Output is correct
3 Correct 8 ms 10528 KB Output is correct
4 Correct 27 ms 11928 KB Output is correct
5 Correct 28 ms 11904 KB Output is correct
6 Correct 27 ms 12024 KB Output is correct
7 Correct 38 ms 13048 KB Output is correct
8 Correct 40 ms 13176 KB Output is correct
9 Correct 40 ms 13176 KB Output is correct
10 Correct 41 ms 12920 KB Output is correct
11 Correct 44 ms 13048 KB Output is correct
12 Correct 43 ms 13180 KB Output is correct
13 Correct 40 ms 13176 KB Output is correct
14 Correct 41 ms 13176 KB Output is correct
15 Correct 47 ms 13176 KB Output is correct
16 Correct 39 ms 13048 KB Output is correct
17 Correct 37 ms 12920 KB Output is correct
18 Correct 42 ms 13048 KB Output is correct
19 Correct 42 ms 12792 KB Output is correct
20 Correct 45 ms 13248 KB Output is correct
21 Correct 41 ms 13176 KB Output is correct
22 Correct 46 ms 13564 KB Output is correct
23 Correct 46 ms 13560 KB Output is correct
24 Correct 52 ms 13816 KB Output is correct
25 Correct 21 ms 12152 KB Output is correct
26 Correct 24 ms 12800 KB Output is correct
27 Correct 21 ms 11768 KB Output is correct
28 Correct 56 ms 13868 KB Output is correct
29 Correct 50 ms 13816 KB Output is correct
30 Correct 52 ms 13944 KB Output is correct
31 Correct 50 ms 13944 KB Output is correct
32 Correct 66 ms 13840 KB Output is correct
33 Correct 59 ms 13816 KB Output is correct
34 Correct 52 ms 13816 KB Output is correct
35 Correct 58 ms 13944 KB Output is correct
36 Correct 16 ms 11776 KB Output is correct
37 Correct 56 ms 13816 KB Output is correct
38 Correct 56 ms 13816 KB Output is correct
39 Correct 61 ms 13920 KB Output is correct
40 Correct 58 ms 13944 KB Output is correct
41 Correct 57 ms 13692 KB Output is correct
42 Correct 48 ms 13568 KB Output is correct
43 Correct 19 ms 12032 KB Output is correct
44 Correct 50 ms 13560 KB Output is correct
45 Correct 50 ms 13560 KB Output is correct
46 Correct 21 ms 12064 KB Output is correct
47 Correct 39 ms 12792 KB Output is correct
48 Correct 35 ms 12576 KB Output is correct
49 Correct 35 ms 12408 KB Output is correct
50 Correct 32 ms 12288 KB Output is correct
51 Correct 30 ms 12152 KB Output is correct
52 Correct 39 ms 12792 KB Output is correct
53 Correct 37 ms 12928 KB Output is correct
54 Correct 41 ms 12800 KB Output is correct
55 Correct 41 ms 12920 KB Output is correct
56 Correct 298 ms 12920 KB Output is correct
57 Correct 308 ms 12920 KB Output is correct
58 Correct 299 ms 13048 KB Output is correct
59 Correct 481 ms 14996 KB Output is correct
60 Correct 526 ms 14584 KB Output is correct
61 Correct 502 ms 15080 KB Output is correct
62 Correct 597 ms 14968 KB Output is correct
63 Correct 592 ms 15096 KB Output is correct
64 Correct 638 ms 15096 KB Output is correct
65 Correct 521 ms 14712 KB Output is correct
66 Correct 574 ms 14968 KB Output is correct
67 Correct 517 ms 14716 KB Output is correct
68 Correct 502 ms 15096 KB Output is correct
69 Correct 479 ms 14488 KB Output is correct
70 Correct 572 ms 14968 KB Output is correct
71 Correct 543 ms 14584 KB Output is correct
72 Correct 541 ms 15096 KB Output is correct
73 Correct 577 ms 14712 KB Output is correct
74 Correct 175 ms 13948 KB Output is correct
75 Correct 226 ms 14200 KB Output is correct
76 Correct 191 ms 13784 KB Output is correct
77 Correct 701 ms 15864 KB Output is correct
78 Correct 706 ms 15864 KB Output is correct
79 Correct 132 ms 13868 KB Output is correct
80 Correct 825 ms 15880 KB Output is correct
81 Correct 832 ms 16120 KB Output is correct
82 Correct 613 ms 15352 KB Output is correct
83 Correct 625 ms 15608 KB Output is correct
84 Correct 169 ms 13896 KB Output is correct
85 Correct 731 ms 15352 KB Output is correct
86 Correct 710 ms 15100 KB Output is correct
87 Correct 201 ms 13236 KB Output is correct
88 Correct 483 ms 14712 KB Output is correct
89 Correct 437 ms 14328 KB Output is correct
90 Correct 442 ms 14204 KB Output is correct
91 Correct 400 ms 13432 KB Output is correct
92 Correct 333 ms 13176 KB Output is correct
93 Correct 532 ms 14072 KB Output is correct
94 Correct 489 ms 14584 KB Output is correct
95 Correct 557 ms 14712 KB Output is correct
96 Correct 526 ms 14584 KB Output is correct
97 Correct 759 ms 40020 KB Output is correct
98 Correct 325 ms 40008 KB Output is correct
99 Execution timed out 2071 ms 89596 KB Time limit exceeded
100 Halted 0 ms 0 KB -