結果

問題 No.274 The Wall
ユーザー むかでむかで
提出日時 2020-11-04 23:32:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 4,269 bytes
コンパイル時間 2,347 ms
コンパイル使用メモリ 211,888 KB
実行使用メモリ 132,184 KB
最終ジャッジ日時 2023-09-29 16:23:01
合計ジャッジ時間 5,030 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 125 ms
69,012 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 349 ms
132,184 KB
testcase_12 AC 22 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 14 ms
4,376 KB
testcase_16 AC 126 ms
50,100 KB
testcase_17 AC 117 ms
47,860 KB
testcase_18 AC 126 ms
51,004 KB
testcase_19 AC 25 ms
4,380 KB
testcase_20 AC 28 ms
4,380 KB
testcase_21 AC 30 ms
4,380 KB
testcase_22 AC 32 ms
4,376 KB
testcase_23 AC 31 ms
4,376 KB
testcase_24 AC 31 ms
4,380 KB
testcase_25 AC 32 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

//-------------------------------------------------
//--Strongly Connected Components
//-------------------------------------------------
class SCC
{
private:
    using SCCGraph = vector<vector<int> >;
    int N,sz;
    SCCGraph G, rG;
    stack<int> st;
    vector<bool> seen;
    vector<int> belong;
    
    void dfs(int v){
        seen[v] = true;
        for(auto u:G[v]){
            if (seen[u]) continue;
            dfs(u);
        }
        st.push(v);
    }
    void rdfs(int v, int k){
        seen[v] = true;
        belong[v] = k;
        for(auto u:rG[v]){
            if (seen[u]) continue;
            rdfs(u,k);
        }
    }
public:
    SCC(int n):N(n),G(n),rG(n),seen(n),belong(n){}
    void add_edge(int u, int v){
        G[u].push_back(v);
        rG[v].push_back(u);
    }
    void build(){
        for(int v=0; v<N; v++) seen[v]=0;
        for(int v=0; v<N; v++)
            if (!seen[v]) dfs(v);
        for(int v=0; v<N; v++) seen[v]=0;
        sz=0;
        while(!st.empty()){
            int v = st.top(); st.pop();
            if (!seen[v]) rdfs(v,sz++);
        }
    }
    vector<vector<int> > get_list(){
        vector<vector<int> > ret(sz);
        for(int v=0; v<N; v++)
            ret[belong[v]].push_back(v);
        return ret;
    }
    int size(){return sz;};
    int operator[](int v){return belong[v];}
};

//-------------------------------------------------
//--Two SAT
//-------------------------------------------------
class TwoSAT
{
private:
    int N;
    SCC scc;
public:
    TwoSAT(int n):scc(n<<1),N(n){}
    void add_if(int x, bool f, int y, bool g){
        scc.add_edge(x<<1|f,y<<1|g);
        scc.add_edge(y<<1|!g,x<<1|!f);
    }
    void add_clause(int x, bool f, int y, bool g){
        scc.add_edge(x<<1|!f,y<<1|g);
        scc.add_edge(y<<1|!g,x<<1|f);
    }
    void add_var(int x, bool f){
        scc.add_edge(x<<1|!f,x<<1|f);
    }
    vector<bool> solve(){
        scc.build();
        vector<bool> ret(N);
        for(int i=0; i<N; i++){
            int u = scc[i<<1|1];
            int v = scc[i<<1];
            if (u > v)
                ret[i] = true;
            else if(u < v)
                ret[i] = false;
            else
                return vector<bool>();
        }
        return ret;
    }
};

struct Range {int l,r;};
bool cross(Range a, Range b){
    if (a.l>b.l) swap(a,b);
    return (b.l<=a.r);
}

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M; cin>>N>>M;

    auto rev = [&](const Range &rg){
        return Range{M-rg.r-1, M-rg.l-1};
    };
    
    vector<Range> rs(N);
    rep(i,N){
        int l,r; cin>>l>>r;
        rs[i] = Range{l,r};
    }

    TwoSAT ts(N);
    rep(i,N)FOR(j,i+1,N-1){
        rep(f,2)rep(g,2){
            Range a = f?rev(rs[i]):rs[i];
            Range b = g?rev(rs[j]):rs[j];
            if (cross(a,b)) ts.add_clause(i,f,j,g);
        }
    }
    auto ans = ts.solve();
    cout<<(ans.empty()?"NO":"YES")<<"\n";

    return 0;
}
0