結果

問題 No.2002 Range Swap Query
ユーザー vwxyzvwxyz
提出日時 2023-03-30 19:24:48
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,311 bytes
コンパイル時間 1,567 ms
コンパイル使用メモリ 130,388 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2023-10-22 04:12:38
合計ジャッジ時間 14,544 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdio>
using namespace std;

int N, K, Q;
vector<int> A, B;
vector<pair<int, pair<int, int>>> query;
vector<int> ans_lst;
int D;

vector<vector<int>> mo;
vector<int> perm;
vector<int> idx;

void init()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> N >> K >> Q;
    A.resize(K);
    B.resize(K);
    for (int k = 0; k < K; k++)
    {
        cin >> A[k] >> B[k];
        A[k]--; B[k]--;
    }
    query.resize(Q);
    for (int q = 0; q < Q; q++)
    {
        int l, r, x;
        cin >> l >> r >> x;
        l--; x--;
        query[q] = {l, {r, x}};
    }
    D = max(1, (int)sqrt(K));
    mo.resize((K+D-1)/D+1);
    for (int q = 0; q < Q; q++)
    {
        int l = query[q].first, r = query[q].second.first;
        mo[l/D].push_back(q);
    }
    perm.resize(N);
    idx.resize(N);
    for (int i = 0; i < N; i++)
    {
        perm[i] = i;
        idx[i] = i;
    }
}

void solve()
{
    ans_lst.resize(Q);
    int L = 0, R = 0;
    for (int d = 0; d < (N+D-1)/D+1; d++)
    {
        sort(mo[d].begin(), mo[d].end(), [&](int i, int j) {
            return query[i].second.first > query[j].second.first;
        });
        for (int q : mo[d])
        {
            int l = query[q].first, r = query[q].second.first, x = query[q].second.second;
            while (L > l)
            {
                L--;
                swap(idx[A[L]], idx[B[L]]);
                swap(perm[idx[A[L]]], perm[idx[B[L]]]);
            }
            while (R < r)
            {
                swap(perm[A[R]], perm[B[R]]);
                idx[perm[A[R]]] = A[R];
                idx[perm[B[R]]] = B[R];
                R++;
            }
            while (L < l)
            {
                swap(idx[A[L]], idx[B[L]]);
                swap(perm[idx[A[L]]], perm[idx[B[L]]]);
                L++;
            }
            while (r < R)
            {
                R--;
                swap(perm[A[R]], perm[B[R]]);
                idx[perm[A[R]]] = A[R];
                idx[perm[B[R]]] = B[R];
            }
            ans_lst[q] = perm[x]+1;
        }
    }
    for (int i = 0; i < Q; i++)
    {
        cout << ans_lst[i] << '\n';
    }
}

int main()
{
    init();
    solve();
    return 0;
}
0