Submission #282937

# Submission time Handle Problem Language Result Execution time Memory
282937 2020-08-25T07:33:19 Z 최은수(#5745) Iqea (innopolis2018_final_C) C++17
36 / 100
1657 ms 262148 KB
#include<iostream>
#include<vector>
#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;
}
vector<pair<unsigned int,unsigned char> >sv1[100010];
vector<pair<unsigned int,unsigned char> >sv2[100010];
vector<vector<int> >ind[100010];
int sz1[100010],sz2[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);
    for(int i=0;i<n;i++)
    {
        auto&t=sxv[i];
        int p1=uf1.par(i);
        int p2=uf2.par(lower_bound(all(syv),t,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];
        for(int&u:cp1)
            for(int&v:cp2)
                sv1[u].eb(v-n+i%32768*131072,i/32768);
        sz1[i]=cp1.size();
        sz2[i]=cp2.size();
    }
    for(int i=0;i<n;i++)
    {
        for(auto&t:sv1[i])
        {
            int fi=t.fi%131072;
            int se=t.se*32768+t.fi/131072;
            sv2[fi].eb(i+se%32768*131072,se/32768);
        }
        sv1[i].clear(),sv1[i].shrink_to_fit();
    }
    int ct=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<(int)sv2[i].size();j++)
        {
            auto&t=sv2[i][j];
            int fi=t.fi%131072;
            int cid=t.se*32768+t.fi/131072;
            if(ind[cid].empty())
                ind[cid].resize(sz1[cid],vector<int>(sz2[cid],0));
            ind[cid][cdep[fi]][cdep[i+n]]=ct;
            if(j==(int)sv2[i].size()-1||fi!=(int)(sv2[i][j+1].fi%131072))
                ct++;
        }
        sv2[i].clear(),sv2[i].shrink_to_fit();
    }
    vector<int>arr(ct,0);
    int q;
    cin>>q;
    for(int qi=0;qi<q;qi++)
    {
        int t,x,y;
        cin>>t>>x>>y;
        int id=lower_bound(all(sxv),pi(x,y),cmp1)-sxv.begin();
        int p1=uf1.par(id);
        int p2=uf2.par(lower_bound(all(syv),pi(x,y),cmp2)-syv.begin())+n;
        if(t==1)
            for(int i=cdep[p1];i>=0;i--)
                for(int j=cdep[p2];j>=0;j--)
                    arr[ind[id][i][j]]=min(arr[ind[id][i][j]],dis[p1][i]+dis[p2][j]-inf);
        else
        {
            int ans=0;
            for(int i=cdep[p1];i>=0;i--)
                for(int j=cdep[p2];j>=0;j--)
                    ans=min(ans,arr[ind[id][i][j]]+dis[p1][i]+dis[p2][j]);
            if(ans==0)
                ans=-1;
            else
                ans+=inf;
            cout<<ans<<'\n';
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12928 KB Output is correct
2 Correct 9 ms 12928 KB Output is correct
3 Correct 9 ms 12928 KB Output is correct
4 Correct 29 ms 16328 KB Output is correct
5 Correct 34 ms 16392 KB Output is correct
6 Correct 30 ms 17056 KB Output is correct
7 Correct 41 ms 18208 KB Output is correct
8 Correct 52 ms 18396 KB Output is correct
9 Correct 47 ms 18692 KB Output is correct
10 Correct 47 ms 18404 KB Output is correct
11 Correct 42 ms 18392 KB Output is correct
12 Correct 42 ms 18376 KB Output is correct
13 Correct 48 ms 18792 KB Output is correct
14 Correct 47 ms 18032 KB Output is correct
15 Correct 44 ms 18260 KB Output is correct
16 Correct 47 ms 18068 KB Output is correct
17 Correct 51 ms 18164 KB Output is correct
18 Correct 41 ms 17988 KB Output is correct
19 Correct 45 ms 17700 KB Output is correct
20 Correct 46 ms 19764 KB Output is correct
21 Correct 44 ms 18380 KB Output is correct
22 Correct 56 ms 20488 KB Output is correct
23 Correct 50 ms 19740 KB Output is correct
24 Correct 56 ms 20832 KB Output is correct
25 Correct 28 ms 17532 KB Output is correct
26 Correct 27 ms 15484 KB Output is correct
27 Correct 21 ms 15100 KB Output is correct
28 Correct 51 ms 20704 KB Output is correct
29 Correct 52 ms 20852 KB Output is correct
30 Correct 54 ms 21196 KB Output is correct
31 Correct 55 ms 20832 KB Output is correct
32 Correct 54 ms 20928 KB Output is correct
33 Correct 55 ms 21016 KB Output is correct
34 Correct 55 ms 20816 KB Output is correct
35 Correct 56 ms 20892 KB Output is correct
36 Correct 28 ms 17528 KB Output is correct
37 Correct 54 ms 20888 KB Output is correct
38 Correct 53 ms 20756 KB Output is correct
39 Correct 58 ms 21012 KB Output is correct
40 Correct 55 ms 21008 KB Output is correct
41 Correct 54 ms 20004 KB Output is correct
42 Correct 64 ms 20236 KB Output is correct
43 Correct 28 ms 17276 KB Output is correct
44 Correct 49 ms 20172 KB Output is correct
45 Correct 52 ms 20292 KB Output is correct
46 Correct 23 ms 15164 KB Output is correct
47 Correct 41 ms 18808 KB Output is correct
48 Correct 37 ms 17628 KB Output is correct
49 Correct 39 ms 17792 KB Output is correct
50 Correct 37 ms 17268 KB Output is correct
51 Correct 36 ms 17520 KB Output is correct
52 Correct 41 ms 18556 KB Output is correct
53 Correct 44 ms 18848 KB Output is correct
54 Correct 42 ms 19080 KB Output is correct
55 Correct 45 ms 19328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12928 KB Output is correct
2 Correct 9 ms 12928 KB Output is correct
3 Correct 9 ms 12928 KB Output is correct
4 Correct 29 ms 16328 KB Output is correct
5 Correct 34 ms 16392 KB Output is correct
6 Correct 30 ms 17056 KB Output is correct
7 Correct 41 ms 18208 KB Output is correct
8 Correct 52 ms 18396 KB Output is correct
9 Correct 47 ms 18692 KB Output is correct
10 Correct 47 ms 18404 KB Output is correct
11 Correct 42 ms 18392 KB Output is correct
12 Correct 42 ms 18376 KB Output is correct
13 Correct 48 ms 18792 KB Output is correct
14 Correct 47 ms 18032 KB Output is correct
15 Correct 44 ms 18260 KB Output is correct
16 Correct 47 ms 18068 KB Output is correct
17 Correct 51 ms 18164 KB Output is correct
18 Correct 41 ms 17988 KB Output is correct
19 Correct 45 ms 17700 KB Output is correct
20 Correct 46 ms 19764 KB Output is correct
21 Correct 44 ms 18380 KB Output is correct
22 Correct 56 ms 20488 KB Output is correct
23 Correct 50 ms 19740 KB Output is correct
24 Correct 56 ms 20832 KB Output is correct
25 Correct 28 ms 17532 KB Output is correct
26 Correct 27 ms 15484 KB Output is correct
27 Correct 21 ms 15100 KB Output is correct
28 Correct 51 ms 20704 KB Output is correct
29 Correct 52 ms 20852 KB Output is correct
30 Correct 54 ms 21196 KB Output is correct
31 Correct 55 ms 20832 KB Output is correct
32 Correct 54 ms 20928 KB Output is correct
33 Correct 55 ms 21016 KB Output is correct
34 Correct 55 ms 20816 KB Output is correct
35 Correct 56 ms 20892 KB Output is correct
36 Correct 28 ms 17528 KB Output is correct
37 Correct 54 ms 20888 KB Output is correct
38 Correct 53 ms 20756 KB Output is correct
39 Correct 58 ms 21012 KB Output is correct
40 Correct 55 ms 21008 KB Output is correct
41 Correct 54 ms 20004 KB Output is correct
42 Correct 64 ms 20236 KB Output is correct
43 Correct 28 ms 17276 KB Output is correct
44 Correct 49 ms 20172 KB Output is correct
45 Correct 52 ms 20292 KB Output is correct
46 Correct 23 ms 15164 KB Output is correct
47 Correct 41 ms 18808 KB Output is correct
48 Correct 37 ms 17628 KB Output is correct
49 Correct 39 ms 17792 KB Output is correct
50 Correct 37 ms 17268 KB Output is correct
51 Correct 36 ms 17520 KB Output is correct
52 Correct 41 ms 18556 KB Output is correct
53 Correct 44 ms 18848 KB Output is correct
54 Correct 42 ms 19080 KB Output is correct
55 Correct 45 ms 19328 KB Output is correct
56 Correct 98 ms 16984 KB Output is correct
57 Correct 94 ms 17800 KB Output is correct
58 Correct 96 ms 16700 KB Output is correct
59 Correct 126 ms 18384 KB Output is correct
60 Correct 153 ms 18856 KB Output is correct
61 Correct 128 ms 19504 KB Output is correct
62 Correct 134 ms 18252 KB Output is correct
63 Correct 121 ms 18660 KB Output is correct
64 Correct 127 ms 18972 KB Output is correct
65 Correct 114 ms 18756 KB Output is correct
66 Correct 128 ms 19720 KB Output is correct
67 Correct 119 ms 18528 KB Output is correct
68 Correct 130 ms 19500 KB Output is correct
69 Correct 120 ms 18904 KB Output is correct
70 Correct 142 ms 19012 KB Output is correct
71 Correct 118 ms 17496 KB Output is correct
72 Correct 122 ms 18644 KB Output is correct
73 Correct 123 ms 18520 KB Output is correct
74 Correct 94 ms 17532 KB Output is correct
75 Correct 90 ms 15800 KB Output is correct
76 Correct 81 ms 15100 KB Output is correct
77 Correct 154 ms 21072 KB Output is correct
78 Correct 147 ms 21204 KB Output is correct
79 Correct 92 ms 17528 KB Output is correct
80 Correct 145 ms 21260 KB Output is correct
81 Correct 143 ms 21028 KB Output is correct
82 Correct 136 ms 20356 KB Output is correct
83 Correct 144 ms 20604 KB Output is correct
84 Correct 101 ms 17352 KB Output is correct
85 Correct 135 ms 20668 KB Output is correct
86 Correct 138 ms 20416 KB Output is correct
87 Correct 82 ms 15360 KB Output is correct
88 Correct 120 ms 18528 KB Output is correct
89 Correct 117 ms 18480 KB Output is correct
90 Correct 116 ms 18404 KB Output is correct
91 Correct 126 ms 18084 KB Output is correct
92 Correct 110 ms 17428 KB Output is correct
93 Correct 124 ms 19012 KB Output is correct
94 Correct 128 ms 19852 KB Output is correct
95 Correct 144 ms 19388 KB Output is correct
96 Correct 139 ms 19380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 485 ms 76712 KB Output is correct
2 Correct 730 ms 143840 KB Output is correct
3 Runtime error 670 ms 262148 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1579 ms 180552 KB Output is correct
2 Correct 1598 ms 184592 KB Output is correct
3 Correct 1589 ms 198800 KB Output is correct
4 Correct 1657 ms 207512 KB Output is correct
5 Correct 1590 ms 210920 KB Output is correct
6 Correct 1609 ms 194632 KB Output is correct
7 Correct 1571 ms 183936 KB Output is correct
8 Correct 1550 ms 216768 KB Output is correct
9 Correct 1564 ms 213900 KB Output is correct
10 Runtime error 891 ms 262144 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12928 KB Output is correct
2 Correct 9 ms 12928 KB Output is correct
3 Correct 9 ms 12928 KB Output is correct
4 Correct 29 ms 16328 KB Output is correct
5 Correct 34 ms 16392 KB Output is correct
6 Correct 30 ms 17056 KB Output is correct
7 Correct 41 ms 18208 KB Output is correct
8 Correct 52 ms 18396 KB Output is correct
9 Correct 47 ms 18692 KB Output is correct
10 Correct 47 ms 18404 KB Output is correct
11 Correct 42 ms 18392 KB Output is correct
12 Correct 42 ms 18376 KB Output is correct
13 Correct 48 ms 18792 KB Output is correct
14 Correct 47 ms 18032 KB Output is correct
15 Correct 44 ms 18260 KB Output is correct
16 Correct 47 ms 18068 KB Output is correct
17 Correct 51 ms 18164 KB Output is correct
18 Correct 41 ms 17988 KB Output is correct
19 Correct 45 ms 17700 KB Output is correct
20 Correct 46 ms 19764 KB Output is correct
21 Correct 44 ms 18380 KB Output is correct
22 Correct 56 ms 20488 KB Output is correct
23 Correct 50 ms 19740 KB Output is correct
24 Correct 56 ms 20832 KB Output is correct
25 Correct 28 ms 17532 KB Output is correct
26 Correct 27 ms 15484 KB Output is correct
27 Correct 21 ms 15100 KB Output is correct
28 Correct 51 ms 20704 KB Output is correct
29 Correct 52 ms 20852 KB Output is correct
30 Correct 54 ms 21196 KB Output is correct
31 Correct 55 ms 20832 KB Output is correct
32 Correct 54 ms 20928 KB Output is correct
33 Correct 55 ms 21016 KB Output is correct
34 Correct 55 ms 20816 KB Output is correct
35 Correct 56 ms 20892 KB Output is correct
36 Correct 28 ms 17528 KB Output is correct
37 Correct 54 ms 20888 KB Output is correct
38 Correct 53 ms 20756 KB Output is correct
39 Correct 58 ms 21012 KB Output is correct
40 Correct 55 ms 21008 KB Output is correct
41 Correct 54 ms 20004 KB Output is correct
42 Correct 64 ms 20236 KB Output is correct
43 Correct 28 ms 17276 KB Output is correct
44 Correct 49 ms 20172 KB Output is correct
45 Correct 52 ms 20292 KB Output is correct
46 Correct 23 ms 15164 KB Output is correct
47 Correct 41 ms 18808 KB Output is correct
48 Correct 37 ms 17628 KB Output is correct
49 Correct 39 ms 17792 KB Output is correct
50 Correct 37 ms 17268 KB Output is correct
51 Correct 36 ms 17520 KB Output is correct
52 Correct 41 ms 18556 KB Output is correct
53 Correct 44 ms 18848 KB Output is correct
54 Correct 42 ms 19080 KB Output is correct
55 Correct 45 ms 19328 KB Output is correct
56 Correct 98 ms 16984 KB Output is correct
57 Correct 94 ms 17800 KB Output is correct
58 Correct 96 ms 16700 KB Output is correct
59 Correct 126 ms 18384 KB Output is correct
60 Correct 153 ms 18856 KB Output is correct
61 Correct 128 ms 19504 KB Output is correct
62 Correct 134 ms 18252 KB Output is correct
63 Correct 121 ms 18660 KB Output is correct
64 Correct 127 ms 18972 KB Output is correct
65 Correct 114 ms 18756 KB Output is correct
66 Correct 128 ms 19720 KB Output is correct
67 Correct 119 ms 18528 KB Output is correct
68 Correct 130 ms 19500 KB Output is correct
69 Correct 120 ms 18904 KB Output is correct
70 Correct 142 ms 19012 KB Output is correct
71 Correct 118 ms 17496 KB Output is correct
72 Correct 122 ms 18644 KB Output is correct
73 Correct 123 ms 18520 KB Output is correct
74 Correct 94 ms 17532 KB Output is correct
75 Correct 90 ms 15800 KB Output is correct
76 Correct 81 ms 15100 KB Output is correct
77 Correct 154 ms 21072 KB Output is correct
78 Correct 147 ms 21204 KB Output is correct
79 Correct 92 ms 17528 KB Output is correct
80 Correct 145 ms 21260 KB Output is correct
81 Correct 143 ms 21028 KB Output is correct
82 Correct 136 ms 20356 KB Output is correct
83 Correct 144 ms 20604 KB Output is correct
84 Correct 101 ms 17352 KB Output is correct
85 Correct 135 ms 20668 KB Output is correct
86 Correct 138 ms 20416 KB Output is correct
87 Correct 82 ms 15360 KB Output is correct
88 Correct 120 ms 18528 KB Output is correct
89 Correct 117 ms 18480 KB Output is correct
90 Correct 116 ms 18404 KB Output is correct
91 Correct 126 ms 18084 KB Output is correct
92 Correct 110 ms 17428 KB Output is correct
93 Correct 124 ms 19012 KB Output is correct
94 Correct 128 ms 19852 KB Output is correct
95 Correct 144 ms 19388 KB Output is correct
96 Correct 139 ms 19380 KB Output is correct
97 Correct 485 ms 76712 KB Output is correct
98 Correct 730 ms 143840 KB Output is correct
99 Runtime error 670 ms 262148 KB Execution killed with signal 9
100 Halted 0 ms 0 KB -