結果

問題 No.905 Sorted?
ユーザー potoooooooopotoooooooo
提出日時 2019-10-11 22:18:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 1,202 bytes
コンパイル時間 1,610 ms
コンパイル使用メモリ 171,028 KB
実行使用メモリ 4,744 KB
最終ジャッジ日時 2023-08-16 18:26:02
合計ジャッジ時間 5,452 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 198 ms
4,376 KB
testcase_09 AC 124 ms
4,380 KB
testcase_10 AC 192 ms
4,744 KB
testcase_11 AC 186 ms
4,476 KB
testcase_12 AC 216 ms
4,376 KB
testcase_13 AC 221 ms
4,632 KB
testcase_14 AC 221 ms
4,672 KB
testcase_15 AC 210 ms
4,740 KB
testcase_16 AC 228 ms
4,656 KB
testcase_17 AC 228 ms
4,588 KB
testcase_18 AC 166 ms
4,620 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 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