답안 #1016057

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016057 2024-07-07T10:40:20 Z bachhoangxuan Road Service 2 (JOI24_ho_t5) C++17
48 / 100
480 ms 124028 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+5;
const int maxl = 25;
const int inf = 1e9;
#define pii array<int,2>

int par[2*maxn],lt[2*maxn],rt[2*maxn];

int findpar(int u){
    if(u!=par[u]) return par[u]=findpar(par[u]);
    return u;
}
void unions(int u,int v){
    u=findpar(u);v=findpar(v);
    if(u!=v) par[v]=u;
}

int n,m,q,sum[maxn],nxt[maxn],pre[maxn];
pii S[maxn][maxl];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin >> n >> m >> q;
    for(int i=0;i<n*m;i++) par[i]=i,lt[i]=n,rt[i]=0;
    for(int i=0;i<n;i++) for(int j=0;j<m-1;j++){
        char C;cin >> C;
        if(C=='1') unions(i*m+j,i*m+j+1);
    }
    for(int i=0;i<n-1;i++) for(int j=0;j<m;j++){
        char C;cin >> C;
        if(C=='1') unions(i*m+j,(i+1)*m+j);
    }
    for(int i=0;i<n;i++) for(int j=0;j<m;j++){
        int p=findpar(i*m+j);
        lt[p]=min(lt[p],i);
        rt[p]=max(rt[p],i);
    }
    for(int i=0;i<n*m;i++){
        if(lt[i]>rt[i]) continue;
        nxt[lt[i]]=max(nxt[lt[i]],rt[i]);
        //cout << "range " << lt[i] << ' ' << rt[i] << '\n';
    }
    for(int i=1;i<n;i++) nxt[i]=max({i,nxt[i],nxt[i-1]});
    for(int i=1;i<n;i++){
        if(nxt[i-1]<=i-1) sum[i]++;
        sum[i]+=sum[i-1];
    }
    for(int i=0;i<n;i++){
        int x;cin >> x;
        pre[i]=(i?pre[i-1]:-1);
        if(x) pre[i]=i;
    }
    for(int i=0;i<n;i++) S[i][0]={i,max(i,pre[nxt[i]])};
    auto g = [&](int i,int j,int k){
        if(i<0) return i;
        return S[i][j][k];
    };
    auto up = [&](int x){
        if(x==-1) return -1;
        else return nxt[x];
    };
    for(int j=1;j<20;j++){
        for(int i=0;i<n;i++){
            S[i][j][0]=max(g(g(i,j-1,0),j-1,1),g(g(i,j-1,1),j-1,0));
            S[i][j][1]=max({g(g(i,j-1,1),j-1,1),g(i,j,0),g(i,j,1),g(up(g(i,j-1,0)),j-1,0)});
        }
    }
    while(q--){
        int sz;cin >> sz;
        vector<int> cc;
        for(int i=0;i<sz;i++){
            int x,y;cin >> x >> y;x--;y--;
            x=findpar(x*m+y);
            cc.push_back(x);
        }
        sort(cc.begin(),cc.end());
        cc.erase(unique(cc.begin(),cc.end()),cc.end());
        if((int)cc.size()==1){
            cout << 0 << '\n';
            continue;
        }
        vector<pii> a;
        int L=n,R=0;
        for(int x:cc){
            L=min(L,rt[x]);
            R=max(R,lt[x]);
            a.push_back({lt[x],rt[x]});
        }
        if(L<R && sum[R]-sum[L]>0){
            cout << -1 << '\n';
            continue;
        }
        sort(a.begin(),a.end(),[&](pii x,pii y){
            return pii{x[0],-x[1]}<pii{y[0],-y[1]};
        });
        {
            vector<pii> st;
            for(pii x:a){
                while(!st.empty() && st.back()[1]>=x[1]) st.pop_back();
                st.push_back(x);
            }
            a=st;
        }
        auto f = [&](int p){
            if(p==-1) return -1;
            int pos=upper_bound(a.begin(),a.end(),pii{p,inf})-a.begin();
            if(pos>0 && p<=a[pos-1][1]) return -1;
            return pos;
        };
        int ans=1;
        pii dp={-1,pre[L]};
        while(dp[1]<R){
            int t0=f(dp[0]),t1=f(dp[1]);
            //cout << '*' << t0 << ' ' << t1 << '\n';
            if(t0>=0 && t1>=0){
                auto get = [&](pii a,int i){
                    int a0=g(a[0],i,1),a1=g(a[1],i,0);
                    int b0=g(up(a[0]),i,0),b1=g(a[1],i,1);
                    return pii{max(a0,a1),max({a0,a1,b0,b1})};
                };
                int mx=R,pos=t1;
                if(pos<(int)a.size()) mx=min(mx,a[pos][0]);
                for(int i=19;i>=0;i--){
                    auto [x,y]=get(dp,i);
                    if(x<mx && y<mx) dp={x,y},ans+=(1<<i);
                }
            }
            if(dp[1]>=R) break;
            auto jmp = [&](int x){
                if(x==-1) return -1;
                int res=nxt[x];
                int pos=upper_bound(a.begin(),a.end(),pii{x,inf})-a.begin();
                if(pos<(int)a.size()) res=min(res,a[pos][1]);
                return res;
            };
            pii ndp{dp[1],-1};
            ndp[1]=max({pre[jmp(dp[1])],jmp(dp[0]),dp[1]});
            ans++;dp=ndp;
            //cout << '*' << dp[0] << ' ' << dp[1] << '\n';
        }
        cout << ans << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 214 ms 119328 KB Output is correct
3 Correct 27 ms 13656 KB Output is correct
4 Correct 26 ms 14172 KB Output is correct
5 Correct 43 ms 14160 KB Output is correct
6 Correct 25 ms 14160 KB Output is correct
7 Correct 160 ms 119124 KB Output is correct
8 Correct 176 ms 119120 KB Output is correct
9 Correct 155 ms 119120 KB Output is correct
10 Correct 22 ms 13652 KB Output is correct
11 Correct 125 ms 84064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 214 ms 119328 KB Output is correct
3 Correct 27 ms 13656 KB Output is correct
4 Correct 26 ms 14172 KB Output is correct
5 Correct 43 ms 14160 KB Output is correct
6 Correct 25 ms 14160 KB Output is correct
7 Correct 160 ms 119124 KB Output is correct
8 Correct 176 ms 119120 KB Output is correct
9 Correct 155 ms 119120 KB Output is correct
10 Correct 22 ms 13652 KB Output is correct
11 Correct 125 ms 84064 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 195 ms 119176 KB Output is correct
14 Correct 57 ms 41736 KB Output is correct
15 Correct 39 ms 14164 KB Output is correct
16 Correct 38 ms 14160 KB Output is correct
17 Correct 25 ms 14172 KB Output is correct
18 Correct 177 ms 119196 KB Output is correct
19 Correct 162 ms 119120 KB Output is correct
20 Correct 20 ms 13656 KB Output is correct
21 Correct 122 ms 84052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 214 ms 119328 KB Output is correct
3 Correct 27 ms 13656 KB Output is correct
4 Correct 26 ms 14172 KB Output is correct
5 Correct 43 ms 14160 KB Output is correct
6 Correct 25 ms 14160 KB Output is correct
7 Correct 160 ms 119124 KB Output is correct
8 Correct 176 ms 119120 KB Output is correct
9 Correct 155 ms 119120 KB Output is correct
10 Correct 22 ms 13652 KB Output is correct
11 Correct 125 ms 84064 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 195 ms 119176 KB Output is correct
14 Correct 57 ms 41736 KB Output is correct
15 Correct 39 ms 14164 KB Output is correct
16 Correct 38 ms 14160 KB Output is correct
17 Correct 25 ms 14172 KB Output is correct
18 Correct 177 ms 119196 KB Output is correct
19 Correct 162 ms 119120 KB Output is correct
20 Correct 20 ms 13656 KB Output is correct
21 Correct 122 ms 84052 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 218 ms 119332 KB Output is correct
24 Correct 187 ms 89116 KB Output is correct
25 Correct 58 ms 16536 KB Output is correct
26 Correct 57 ms 16108 KB Output is correct
27 Correct 79 ms 16512 KB Output is correct
28 Correct 226 ms 121676 KB Output is correct
29 Correct 191 ms 121616 KB Output is correct
30 Correct 173 ms 121544 KB Output is correct
31 Correct 172 ms 119528 KB Output is correct
32 Correct 191 ms 123988 KB Output is correct
33 Correct 55 ms 16476 KB Output is correct
34 Correct 117 ms 84052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 214 ms 119328 KB Output is correct
3 Correct 27 ms 13656 KB Output is correct
4 Correct 26 ms 14172 KB Output is correct
5 Correct 43 ms 14160 KB Output is correct
6 Correct 25 ms 14160 KB Output is correct
7 Correct 160 ms 119124 KB Output is correct
8 Correct 176 ms 119120 KB Output is correct
9 Correct 155 ms 119120 KB Output is correct
10 Correct 22 ms 13652 KB Output is correct
11 Correct 125 ms 84064 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 195 ms 119176 KB Output is correct
14 Correct 57 ms 41736 KB Output is correct
15 Correct 39 ms 14164 KB Output is correct
16 Correct 38 ms 14160 KB Output is correct
17 Correct 25 ms 14172 KB Output is correct
18 Correct 177 ms 119196 KB Output is correct
19 Correct 162 ms 119120 KB Output is correct
20 Correct 20 ms 13656 KB Output is correct
21 Correct 122 ms 84052 KB Output is correct
22 Correct 440 ms 121684 KB Output is correct
23 Correct 243 ms 121424 KB Output is correct
24 Correct 105 ms 42392 KB Output is correct
25 Correct 81 ms 16212 KB Output is correct
26 Correct 111 ms 16152 KB Output is correct
27 Correct 95 ms 16408 KB Output is correct
28 Correct 56 ms 16208 KB Output is correct
29 Correct 222 ms 121516 KB Output is correct
30 Correct 197 ms 121428 KB Output is correct
31 Correct 234 ms 121428 KB Output is correct
32 Correct 221 ms 121440 KB Output is correct
33 Correct 62 ms 15896 KB Output is correct
34 Correct 273 ms 86352 KB Output is correct
35 Correct 205 ms 68944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 214 ms 119328 KB Output is correct
3 Correct 27 ms 13656 KB Output is correct
4 Correct 26 ms 14172 KB Output is correct
5 Correct 43 ms 14160 KB Output is correct
6 Correct 25 ms 14160 KB Output is correct
7 Correct 160 ms 119124 KB Output is correct
8 Correct 176 ms 119120 KB Output is correct
9 Correct 155 ms 119120 KB Output is correct
10 Correct 22 ms 13652 KB Output is correct
11 Correct 125 ms 84064 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 195 ms 119176 KB Output is correct
14 Correct 57 ms 41736 KB Output is correct
15 Correct 39 ms 14164 KB Output is correct
16 Correct 38 ms 14160 KB Output is correct
17 Correct 25 ms 14172 KB Output is correct
18 Correct 177 ms 119196 KB Output is correct
19 Correct 162 ms 119120 KB Output is correct
20 Correct 20 ms 13656 KB Output is correct
21 Correct 122 ms 84052 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 218 ms 119332 KB Output is correct
24 Correct 187 ms 89116 KB Output is correct
25 Correct 58 ms 16536 KB Output is correct
26 Correct 57 ms 16108 KB Output is correct
27 Correct 79 ms 16512 KB Output is correct
28 Correct 226 ms 121676 KB Output is correct
29 Correct 191 ms 121616 KB Output is correct
30 Correct 173 ms 121544 KB Output is correct
31 Correct 172 ms 119528 KB Output is correct
32 Correct 191 ms 123988 KB Output is correct
33 Correct 55 ms 16476 KB Output is correct
34 Correct 117 ms 84052 KB Output is correct
35 Correct 440 ms 121684 KB Output is correct
36 Correct 243 ms 121424 KB Output is correct
37 Correct 105 ms 42392 KB Output is correct
38 Correct 81 ms 16212 KB Output is correct
39 Correct 111 ms 16152 KB Output is correct
40 Correct 95 ms 16408 KB Output is correct
41 Correct 56 ms 16208 KB Output is correct
42 Correct 222 ms 121516 KB Output is correct
43 Correct 197 ms 121428 KB Output is correct
44 Correct 234 ms 121428 KB Output is correct
45 Correct 221 ms 121440 KB Output is correct
46 Correct 62 ms 15896 KB Output is correct
47 Correct 273 ms 86352 KB Output is correct
48 Correct 205 ms 68944 KB Output is correct
49 Correct 480 ms 121520 KB Output is correct
50 Correct 397 ms 121168 KB Output is correct
51 Correct 125 ms 42072 KB Output is correct
52 Correct 79 ms 18964 KB Output is correct
53 Correct 68 ms 16984 KB Output is correct
54 Correct 88 ms 16848 KB Output is correct
55 Correct 222 ms 124028 KB Output is correct
56 Correct 193 ms 121936 KB Output is correct
57 Correct 197 ms 123220 KB Output is correct
58 Correct 238 ms 121480 KB Output is correct
59 Correct 60 ms 16232 KB Output is correct
60 Correct 352 ms 86328 KB Output is correct
61 Correct 276 ms 68688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 214 ms 119328 KB Output is correct
3 Correct 27 ms 13656 KB Output is correct
4 Correct 26 ms 14172 KB Output is correct
5 Correct 43 ms 14160 KB Output is correct
6 Correct 25 ms 14160 KB Output is correct
7 Correct 160 ms 119124 KB Output is correct
8 Correct 176 ms 119120 KB Output is correct
9 Correct 155 ms 119120 KB Output is correct
10 Correct 22 ms 13652 KB Output is correct
11 Correct 125 ms 84064 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 195 ms 119176 KB Output is correct
14 Correct 57 ms 41736 KB Output is correct
15 Correct 39 ms 14164 KB Output is correct
16 Correct 38 ms 14160 KB Output is correct
17 Correct 25 ms 14172 KB Output is correct
18 Correct 177 ms 119196 KB Output is correct
19 Correct 162 ms 119120 KB Output is correct
20 Correct 20 ms 13656 KB Output is correct
21 Correct 122 ms 84052 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 218 ms 119332 KB Output is correct
24 Correct 187 ms 89116 KB Output is correct
25 Correct 58 ms 16536 KB Output is correct
26 Correct 57 ms 16108 KB Output is correct
27 Correct 79 ms 16512 KB Output is correct
28 Correct 226 ms 121676 KB Output is correct
29 Correct 191 ms 121616 KB Output is correct
30 Correct 173 ms 121544 KB Output is correct
31 Correct 172 ms 119528 KB Output is correct
32 Correct 191 ms 123988 KB Output is correct
33 Correct 55 ms 16476 KB Output is correct
34 Correct 117 ms 84052 KB Output is correct
35 Incorrect 0 ms 348 KB Output isn't correct
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 214 ms 119328 KB Output is correct
3 Correct 27 ms 13656 KB Output is correct
4 Correct 26 ms 14172 KB Output is correct
5 Correct 43 ms 14160 KB Output is correct
6 Correct 25 ms 14160 KB Output is correct
7 Correct 160 ms 119124 KB Output is correct
8 Correct 176 ms 119120 KB Output is correct
9 Correct 155 ms 119120 KB Output is correct
10 Correct 22 ms 13652 KB Output is correct
11 Correct 125 ms 84064 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 195 ms 119176 KB Output is correct
14 Correct 57 ms 41736 KB Output is correct
15 Correct 39 ms 14164 KB Output is correct
16 Correct 38 ms 14160 KB Output is correct
17 Correct 25 ms 14172 KB Output is correct
18 Correct 177 ms 119196 KB Output is correct
19 Correct 162 ms 119120 KB Output is correct
20 Correct 20 ms 13656 KB Output is correct
21 Correct 122 ms 84052 KB Output is correct
22 Correct 440 ms 121684 KB Output is correct
23 Correct 243 ms 121424 KB Output is correct
24 Correct 105 ms 42392 KB Output is correct
25 Correct 81 ms 16212 KB Output is correct
26 Correct 111 ms 16152 KB Output is correct
27 Correct 95 ms 16408 KB Output is correct
28 Correct 56 ms 16208 KB Output is correct
29 Correct 222 ms 121516 KB Output is correct
30 Correct 197 ms 121428 KB Output is correct
31 Correct 234 ms 121428 KB Output is correct
32 Correct 221 ms 121440 KB Output is correct
33 Correct 62 ms 15896 KB Output is correct
34 Correct 273 ms 86352 KB Output is correct
35 Correct 205 ms 68944 KB Output is correct
36 Incorrect 0 ms 348 KB Output isn't correct
37 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 214 ms 119328 KB Output is correct
3 Correct 27 ms 13656 KB Output is correct
4 Correct 26 ms 14172 KB Output is correct
5 Correct 43 ms 14160 KB Output is correct
6 Correct 25 ms 14160 KB Output is correct
7 Correct 160 ms 119124 KB Output is correct
8 Correct 176 ms 119120 KB Output is correct
9 Correct 155 ms 119120 KB Output is correct
10 Correct 22 ms 13652 KB Output is correct
11 Correct 125 ms 84064 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 195 ms 119176 KB Output is correct
14 Correct 57 ms 41736 KB Output is correct
15 Correct 39 ms 14164 KB Output is correct
16 Correct 38 ms 14160 KB Output is correct
17 Correct 25 ms 14172 KB Output is correct
18 Correct 177 ms 119196 KB Output is correct
19 Correct 162 ms 119120 KB Output is correct
20 Correct 20 ms 13656 KB Output is correct
21 Correct 122 ms 84052 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 218 ms 119332 KB Output is correct
24 Correct 187 ms 89116 KB Output is correct
25 Correct 58 ms 16536 KB Output is correct
26 Correct 57 ms 16108 KB Output is correct
27 Correct 79 ms 16512 KB Output is correct
28 Correct 226 ms 121676 KB Output is correct
29 Correct 191 ms 121616 KB Output is correct
30 Correct 173 ms 121544 KB Output is correct
31 Correct 172 ms 119528 KB Output is correct
32 Correct 191 ms 123988 KB Output is correct
33 Correct 55 ms 16476 KB Output is correct
34 Correct 117 ms 84052 KB Output is correct
35 Correct 440 ms 121684 KB Output is correct
36 Correct 243 ms 121424 KB Output is correct
37 Correct 105 ms 42392 KB Output is correct
38 Correct 81 ms 16212 KB Output is correct
39 Correct 111 ms 16152 KB Output is correct
40 Correct 95 ms 16408 KB Output is correct
41 Correct 56 ms 16208 KB Output is correct
42 Correct 222 ms 121516 KB Output is correct
43 Correct 197 ms 121428 KB Output is correct
44 Correct 234 ms 121428 KB Output is correct
45 Correct 221 ms 121440 KB Output is correct
46 Correct 62 ms 15896 KB Output is correct
47 Correct 273 ms 86352 KB Output is correct
48 Correct 205 ms 68944 KB Output is correct
49 Correct 480 ms 121520 KB Output is correct
50 Correct 397 ms 121168 KB Output is correct
51 Correct 125 ms 42072 KB Output is correct
52 Correct 79 ms 18964 KB Output is correct
53 Correct 68 ms 16984 KB Output is correct
54 Correct 88 ms 16848 KB Output is correct
55 Correct 222 ms 124028 KB Output is correct
56 Correct 193 ms 121936 KB Output is correct
57 Correct 197 ms 123220 KB Output is correct
58 Correct 238 ms 121480 KB Output is correct
59 Correct 60 ms 16232 KB Output is correct
60 Correct 352 ms 86328 KB Output is correct
61 Correct 276 ms 68688 KB Output is correct
62 Incorrect 0 ms 348 KB Output isn't correct
63 Halted 0 ms 0 KB -