結果

問題 No.2002 Range Swap Query
ユーザー Ricky_ponRicky_pon
提出日時 2022-07-08 22:20:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,757 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 206,076 KB
実行使用メモリ 16,172 KB
最終ジャッジ日時 2023-08-27 22:00:53
合計ジャッジ時間 6,715 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 12 ms
4,376 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


#include <algorithm>
#include <cassert>
#include <vector>

namespace rklib {

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

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

template <class T>
bool chmin_non_negative(T &a, const T &b) {
    if (a < 0 || a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}

template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

}  // namespace rklib


#define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For(i, 0, n)
#define rrep(i, n) rFor(i, n, 0)
#define fi first
#define se second

using namespace std;
using namespace rklib;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

struct query {
    int l, r, x, idx;

    query() {}
    query(int l, int r, int x, int i) : l(l), r(r), x(x), idx(i) {}
};

int main() {
    int n, K, q;
    scanf("%d%d%d", &n, &K, &q);
    int a[K], b[K];
    rep(i, K) {
        scanf("%d%d", &a[i], &b[i]);
        --a[i];
        --b[i];
    }
    vector<query> qs(q);
    rep(i, q) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        --l;
        --x;
        qs[i] = {l, r, x, i};
    }

    constexpr int B = 500;
    sort(qs.begin(), qs.end(), [](const query &q1, const query &q2) {
        int b1 = q1.l % B, b2 = q2.l % B;
        if (b1 == b2) {
            return b1 % 2 == 0 ? q1.r < q2.r : q1.r > q2.r;
        }
        return b1 < b2;
    });

    int v[n], pos[n];
    iota(v, v + n, 0);
    iota(pos, pos + n, 0);
    int ans[q];
    int tl = 0, tr = 0;
    for (auto [l, r, x, idx] : qs) {
        while (tr < r) {
            swap(v[a[tr]], v[b[tr]]);
            pos[v[a[tr]]] = a[tr];
            pos[v[b[tr]]] = b[tr];
            ++tr;
        }
        while (tl > l) {
            --tl;
            swap(pos[a[tl]], pos[b[tl]]);
            v[pos[a[tl]]] = a[tl];
            v[pos[b[tl]]] = b[tl];
        }
        while (tr > r) {
            --tr;
            swap(v[a[tr]], v[b[tr]]);
            pos[v[a[tr]]] = a[tr];
            pos[v[b[tr]]] = b[tr];
        }
        while (tl < l) {
            swap(pos[a[tl]], pos[b[tl]]);
            v[pos[a[tl]]] = a[tl];
            v[pos[b[tl]]] = b[tl];
            ++tl;
        }

        ans[idx] = v[x] + 1;
    }
    rep(i, q) printf("%d\n", ans[i]);
}
0