답안 #785170

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
785170 2023-07-17T06:45:33 Z 이동현(#10024) Raspad (COI17_raspad) C++17
100 / 100
2413 ms 86980 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;

const int NS = (int)1e5 + 4;
int n, m;
int a[NS][54];

int i;
struct Dsu{
    int n, allcnt, rtcnt;
    vector<int> pr;
    vector<vector<int>> merge;
    Dsu(int m){
        n = m;
        pr.resize(n);
        iota(pr.begin(), pr.end(), 0);
        allcnt = rtcnt = 0;
    }

    int fd(int x){
        return (x == pr[x] ? x : pr[x] = fd(pr[x]));
    }

    void unite(int x, int y){
        x = fd(x), y = fd(y);
        if(x == y) return;
        if(x > y){
            swap(x, y);
        }

        pr[y] = x;
        --allcnt;
        if(x < m && y < m){
            --rtcnt;
            merge.push_back({i, x, y});
        }
    }
};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            char c;
            cin >> c;
            a[i][j] = c - '0';
        }
    }

    int ans = 0;
    auto dnc = [&](auto&&self, int s, int e)->void{
        int mid = s + e >> 1;
        if(s < e){
            self(self, s, mid);
            self(self, mid + 1, e);
        }
        else{
            Dsu gr(m);
            for(int j = 0; j < m; ++j){
                if(a[s][j]){
                    ++gr.allcnt;
                    if(j && a[s][j - 1]){
                        gr.unite(j - 1, j);
                    }
                }
            }
            ans += gr.allcnt;
            // cout << s << ' ' << e << ' ' << ans << endl;
            return;
        }

        int urow = mid - s + 1;
        int drow = e - mid;
        Dsu up(urow * m);
        Dsu down(drow * m);
        
        // cout << s << ' ' << e << ' ' << ans << endl;

        vector<vector<int>> uop, dop;
        for(i = mid + 1; i <= e; ++i){
            for(int j = 0; j < m; ++j){
                if(!a[i][j]){
                    continue;
                }
                ++down.allcnt;
                if(j && a[i][j - 1]) down.unite((i - mid - 1) * m + j - 1, (i - mid - 1) * m + j);
                if(i > mid + 1){
                    if(a[i - 1][j]) down.unite((i - mid - 2) * m + j, (i - mid - 1) * m + j);
                }
                else{
                    ++down.rtcnt;
                }
            }

            // cout << s << ' ' << e << ' ' << i << ' ' << down.allcnt << ' ' << down.rtcnt << endl;

            ans += (down.allcnt - down.rtcnt) * (mid - s + 1);
        }

        for(i = mid; i >= s; --i){
            for(int j = 0; j < m; ++j){
                if(!a[i][j]){
                    continue;
                }
                ++up.allcnt;
                if(j && a[i][j - 1]) up.unite((mid - i) * m + j - 1, (mid - i) * m + j);
                if(i < mid){
                    if(a[i + 1][j]) up.unite((mid - i) * m + j, (mid - i - 1) * m + j);
                }
                else{
                    ++up.rtcnt;
                }
            }

            ans += (up.allcnt - up.rtcnt) * (e - mid);
        }

        // cout << s << ' ' << e << ' ' << ans << endl;

        Dsu test(m * 2);
        for(int i = 0; i < 2; ++i){
            for(int j = 0; j < m; ++j){
                if(!a[i + mid][j]) continue;
                ++test.allcnt;
                if(j && a[i + mid][j - 1]) test.unite(i * m + j, i * m + j - 1);
                if(i && a[i + mid - 1][j]) test.unite(i * m + j, i * m - m + j);
            }
        }

        Dsu gr(m);
        int i = 0, ilst = mid;
        while(i < (int)up.merge.size() && up.merge[i][0] == mid){
            gr.unite(up.merge[i][1], up.merge[i][2]);
            test.unite(up.merge[i][1], up.merge[i][2]);
            ++i;
        }
        while(true){
            int ilen = (i < (int)up.merge.size() ? up.merge[i][0] : s - 1);
            ilen = ilst - ilen;
            ilst = ilst - ilen;

            if(ilen){
                Dsu ngr = gr;
                int j = 0, jlst = mid + 1;
                while(j < (int)down.merge.size() && down.merge[j][0] == mid + 1){
                    ngr.unite(down.merge[j][1], down.merge[j][2]);
                    ++j;
                }
                ngr.allcnt = test.allcnt;

                while(true){
                    int jlen = (j < (int)down.merge.size() ? down.merge[j][0] : e + 1);
                    jlen = jlen - jlst;
                    jlst = jlen + jlst;

                    ans += ilen * jlen * ngr.allcnt;
                    if(j == (int)down.merge.size()) break;
                    ngr.unite(down.merge[j][1], down.merge[j][2]);
                    ++j;
                }
            }

            if(i == (int)up.merge.size()) break;
            gr.unite(up.merge[i][1], up.merge[i][2]);
            test.unite(up.merge[i][1], up.merge[i][2]);
            ++i;
        }

        // cout << s << ' ' << e << ' ' << ans << endl;
    };

    dnc(dnc, 0, n - 1);

    cout << ans << '\n';

    return 0;
}

Compilation message

raspad.cpp: In instantiation of 'main()::<lambda(auto:23&&, long long int, long long int)> [with auto:23 = main()::<lambda(auto:23&&, long long int, long long int)>&]':
raspad.cpp:179:22:   required from here
raspad.cpp:59:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |         int mid = s + e >> 1;
      |                   ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 412 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 412 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 9 ms 1272 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 16 ms 1324 KB Output is correct
10 Correct 3 ms 1192 KB Output is correct
11 Correct 12 ms 1356 KB Output is correct
12 Correct 4 ms 1048 KB Output is correct
13 Correct 10 ms 1300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 27120 KB Output is correct
2 Correct 398 ms 54524 KB Output is correct
3 Correct 638 ms 55964 KB Output is correct
4 Correct 130 ms 50468 KB Output is correct
5 Correct 104 ms 17120 KB Output is correct
6 Correct 429 ms 55964 KB Output is correct
7 Correct 266 ms 55988 KB Output is correct
8 Correct 328 ms 44876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 412 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 9 ms 1272 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 16 ms 1324 KB Output is correct
10 Correct 3 ms 1192 KB Output is correct
11 Correct 12 ms 1356 KB Output is correct
12 Correct 4 ms 1048 KB Output is correct
13 Correct 10 ms 1300 KB Output is correct
14 Correct 280 ms 27120 KB Output is correct
15 Correct 398 ms 54524 KB Output is correct
16 Correct 638 ms 55964 KB Output is correct
17 Correct 130 ms 50468 KB Output is correct
18 Correct 104 ms 17120 KB Output is correct
19 Correct 429 ms 55964 KB Output is correct
20 Correct 266 ms 55988 KB Output is correct
21 Correct 328 ms 44876 KB Output is correct
22 Correct 969 ms 62540 KB Output is correct
23 Correct 2178 ms 86948 KB Output is correct
24 Correct 2413 ms 86904 KB Output is correct
25 Correct 1147 ms 86980 KB Output is correct
26 Correct 481 ms 86840 KB Output is correct
27 Correct 1033 ms 78140 KB Output is correct
28 Correct 1554 ms 78140 KB Output is correct
29 Correct 1620 ms 86900 KB Output is correct
30 Correct 508 ms 86796 KB Output is correct
31 Correct 504 ms 86904 KB Output is correct
32 Correct 613 ms 86940 KB Output is correct
33 Correct 1152 ms 77468 KB Output is correct
34 Correct 1472 ms 78172 KB Output is correct