結果

問題 No.274 The Wall
ユーザー KKT89KKT89
提出日時 2024-03-07 22:23:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 836 ms / 2,000 ms
コード長 3,407 bytes
コンパイル時間 2,811 ms
コンパイル使用メモリ 226,516 KB
実行使用メモリ 260,736 KB
最終ジャッジ日時 2024-03-07 22:23:45
合計ジャッジ時間 5,591 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 264 ms
127,616 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 836 ms
260,736 KB
testcase_12 AC 25 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 6 ms
6,548 KB
testcase_15 AC 12 ms
6,548 KB
testcase_16 AC 112 ms
49,920 KB
testcase_17 AC 113 ms
49,280 KB
testcase_18 AC 120 ms
51,584 KB
testcase_19 AC 20 ms
6,548 KB
testcase_20 AC 23 ms
6,548 KB
testcase_21 AC 24 ms
6,548 KB
testcase_22 AC 26 ms
6,548 KB
testcase_23 AC 25 ms
6,548 KB
testcase_24 AC 26 ms
6,548 KB
testcase_25 AC 25 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

vector<int> scc(const vector<vector<int>> &g) {
    int n = (int)g.size();
    int cnt = 0;
    vector<vector<int>> rg(n);
    vector<int> order;
    vector<int> res(n, -1);
    vector<bool> used(n);
    for (int i = 0; i < n; ++i) {
        for (int j : g[i]) rg[j].push_back(i);
    }

    auto dfs1 = [&](auto self, int s) -> void {
        used[s] = true;
        for (int t : g[s]) {
            if (!used[t]) self(self, t);
        }
        order.push_back(s);
    };
    auto dfs2 = [&](auto self, int s) -> void {
        for (int t : rg[s]) {
            if (res[t] == -1) {
                res[t] = res[s];
                self(self, t);
            }
        }
    };

    order.reserve(n);
    for (int i = 0; i < n; ++i) {
        if (!used[i]) {
            dfs1(dfs1, i);
        }
    }
    reverse(order.begin(), order.end());
    for (int i : order) {
        if (res[i] == -1) {
            res[i] = cnt++;
            dfs2(dfs2, i);
        }
    }
    return res;
}

struct twosat {
    int n;
    vector<vector<int>> g;
    twosat(int n) : n(n), g(2*n) {}

    // (i = f1) || (j = f2)
    void add_clause(int i, bool f1, int j, bool f2) {
        g[(i << 1) ^ !f1].emplace_back((j << 1) ^ f2);
        g[(j << 1) ^ !f2].emplace_back((i << 1) ^ f1);
    }

    // (i = f1) -> (j = f2) <=> (i = !f1) || (j = f2)
    void add_if(int i, bool f1, int j, bool f2) {
        add_clause(i, !f1, j, f2);
    }

    // i
    void set_true(int i) {
        add_clause(i, true, i, true);
    }
    // !i
    void set_false(int i) {
        add_clause(i, false, i, false);
    }

    vector<bool> solve() {
        vector<int> c = scc(g);
        vector<bool> res(n);
        for (int i = 0; i < n; ++i) {
            if (c[i << 1] == c[i << 1 | 1]) return vector<bool>();
            res[i] = (c[i << 1] < c[i << 1 | 1]);
        }
        return res;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    int m; cin >> m;
    vector<int> l(n), r(n);
    for (int i = 0; i < n; ++i) {
        cin >> l[i] >> r[i];
        l[i] += 1;
        r[i] += 1;
    }
    twosat ts(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (i == j) continue;
            auto check = [&](int l1, int r1, int l2, int r2) -> bool {
                return max(l1, l2) > min(r1, r2);
            };
            if (!check(l[i], r[i], l[j], r[j])) {
                ts.add_if(i, false, j, true);
            }
            if (!check(l[i], r[i], m+1-r[j], m+1-l[j])) {
                ts.add_if(i, false, j, false);
            }
            if (!check(m+1-r[i], m+1-l[i], l[j], r[j])) {
                ts.add_if(i, true, j, true);
            }
            if (!check(m+1-r[i], m+1-l[i], m+1-r[j], m+1-l[j])) {
                ts.add_if(i, true, j, false);
            }
        }
    }
    if (ts.solve().size()) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}
0