結果

問題 No.2002 Range Swap Query
ユーザー Ricky_ponRicky_pon
提出日時 2022-07-08 22:28:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 956 ms / 2,000 ms
コード長 2,757 bytes
コンパイル時間 2,476 ms
コンパイル使用メモリ 203,600 KB
実行使用メモリ 12,012 KB
最終ジャッジ日時 2024-04-20 03:50:57
合計ジャッジ時間 10,262 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 5 ms
6,940 KB
testcase_09 AC 5 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 6 ms
6,944 KB
testcase_12 AC 956 ms
12,012 KB
testcase_13 AC 951 ms
11,904 KB
testcase_14 AC 928 ms
11,976 KB
testcase_15 AC 865 ms
11,736 KB
testcase_16 AC 126 ms
11,904 KB
testcase_17 AC 325 ms
11,904 KB
testcase_18 AC 240 ms
7,552 KB
testcase_19 AC 314 ms
8,832 KB
testcase_20 AC 96 ms
10,492 KB
testcase_21 AC 88 ms
7,288 KB
権限があれば一括ダウンロードができます

ソースコード

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