結果

問題 No.905 Sorted?
ユーザー potoooooooopotoooooooo
提出日時 2019-10-11 22:18:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 1,202 bytes
コンパイル時間 1,987 ms
コンパイル使用メモリ 174,000 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-04 02:01:21
合計ジャッジ時間 4,858 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 6 ms
5,376 KB
testcase_08 AC 191 ms
5,376 KB
testcase_09 AC 113 ms
5,376 KB
testcase_10 AC 189 ms
5,376 KB
testcase_11 AC 171 ms
5,376 KB
testcase_12 AC 199 ms
5,376 KB
testcase_13 AC 221 ms
5,376 KB
testcase_14 AC 211 ms
5,376 KB
testcase_15 AC 205 ms
5,376 KB
testcase_16 AC 208 ms
5,376 KB
testcase_17 AC 215 ms
5,376 KB
testcase_18 AC 155 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// one-based numbering
struct UnionFind {
    vector<int> data;
    // i: (data[i] < 0) -> group size, (data[i] > 0) -> parent;
    UnionFind(int n) {
        data.resize(n+1, -1);
    }
    int find(int x) {
        if(data[x] < 0) return x;
        else return data[x] = find(data[x]);
    }
    int size(int x) {
        return -data[find(x)];
    }
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if(x == y) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }
    bool same(int x, int y) {
        x = find(x); y = find(y);
        return x == y;
    }
};


int main() {
    int n; cin >> n;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    UnionFind f(n), g(n);
    for (int i = 1; i < n; i++) {
        if (a[i-1] <= a[i]) f.unite(i, i+1);
        if (a[i-1] >= a[i]) g.unite(i, i+1);
    }
    int q; cin >> q;
    while (q--) {
        int l, r; cin >> l >> r;
        int fans = (int) f.same(l+1, r+1);
        int gans = (int) g.same(l+1, r+1);
        cout << fans << " " << gans << endl;
    }
    return 0;
}
0