結果

問題 No.905 Sorted?
ユーザー ugis_progugis_prog
提出日時 2019-10-11 22:04:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,869 bytes
コンパイル時間 1,406 ms
コンパイル使用メモリ 170,460 KB
実行使用メモリ 9,776 KB
最終ジャッジ日時 2023-08-16 17:54:59
合計ジャッジ時間 3,935 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 6 ms
4,384 KB
testcase_08 WA -
testcase_09 AC 107 ms
6,076 KB
testcase_10 WA -
testcase_11 AC 157 ms
8,336 KB
testcase_12 AC 182 ms
7,792 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 184 ms
9,704 KB
testcase_16 AC 195 ms
9,732 KB
testcase_17 AC 200 ms
9,776 KB
testcase_18 AC 146 ms
9,636 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int (i)=0;(i) < (N); (i)++)
#define all(V) V.begin(), V.end()
using i64 = int_fast64_t;
using P = pair<i64,i64>;

class UnionFind{
private:
    using uint = std::uint_fast64_t;
    std::vector<uint> par;
    std::vector<uint> sizes;

    uint find(uint x){
        if(x == par[x]) return x;
        return par[x] = find(par[x]);
    }
public:
    UnionFind() = delete;
    UnionFind(uint size) : par(size), sizes(size, 1){
        for(uint i = 0; i < size; ++i) par[i] = i;
    }

    bool unite(uint x, uint y){
        x = find(x);
        y = find(y);
        if(x == y) return false;
        
        if(sizes[x] < sizes[y]) std::swap(x, y);
        if(sizes[x] == sizes[y] && x > y) std::swap(x, y);
        sizes[x] += sizes[y];
        sizes[y] = 0;
        par[y] = x;
        return true;
    }
    bool isSame(uint x, uint y ){ return find(par[x]) == find(par[y]);}
    uint getSize(uint x){return find(sizes[par[x]]);}
}; 

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    rep(i,N) cin >> A[i];
    UnionFind up(2*N), down(2*N);

    int left = 0, right = 0;
    int last = A[0];
    while(right < N){
        while(last <= A[right] && right < N) {
            up.unite(N+left, right);
            last = A[right];
            right++;
        }
        if(right < N) last = A[right];
        left = right;
    } 
    left = 0, right = 0;
    last = A[0];
    while(right < N){
        while(last >= A[right] && right < N) {
            down.unite(N+left, right);
            last = A[right];
            right++;
        }
        if(right < N) last = A[right];
        left = right;
    }

    int Q, l , r;
    cin >> Q;
    rep(i, Q){
        cin >> l >> r;
        cout << (up.isSame(l, r) ? 1 : 0) << " ";
        cout << (down.isSame(l, r) ? 1 : 0) << endl;
    }
     
}
0