結果

問題 No.3842 マス目分割
コンテスト
ユーザー D M
提出日時 2026-12-01 10:16:12
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 80 ms / 2,000 ms
+ 614µs
コード長 1,603 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,239 ms
コンパイル使用メモリ 384,480 KB
実行使用メモリ 18,956 KB
最終ジャッジ日時 2026-12-01 10:16:24
合計ジャッジ時間 11,217 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

struct Interval {
    int l, r, id;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<vector<int>> obs(n+1);

    for(int i=0; i<n; i++){
        int a,b;
        cin >> a >> b;
        obs[a].push_back(b);
    }

    dsu uf(2*n);

    vector<Interval> prev,cur;

    int cnt=0;
    int components=0;

    for(int i=1; i<=n; i++){

        sort(obs[i].begin(),obs[i].end());

        // 番兵
        obs[i].push_back(n+1);

        cur.clear();

        // 空き区間の生成
        int l=1;

        for(int b:obs[i]){
            if(l<b){
                cur.push_back({l,b-1,cnt++});
                components++;
            }
            l=b+1;
        }

        // 前の行との連結判定
        int p=0,q=0;

        while(p<(int)prev.size() &&
              q<(int)cur.size()){

            auto [l1,r1,id1]=prev[p];
            auto [l2,r2,id2]=cur[q];

            // 区間が重なっている場合
            if(max(l1,l2)<=min(r1,r2)){
                if(!uf.same(id1,id2)){
                    uf.merge(id1,id2);
                    components--;
                }
            }

            // 右端が小さい方を進める
            if(r1<r2){
                p++;
            }
            else if(r1>r2){
                q++;
            }
            else{
                p++;
                q++;
            }
        }

        swap(prev,cur);
    }

    cout << (components==2 ? "Yes" : "No") << '\n';
}
0