結果

問題 No.2002 Range Swap Query
ユーザー milanis48663220milanis48663220
提出日時 2022-07-12 00:48:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,142 ms / 2,000 ms
コード長 4,645 bytes
コンパイル時間 1,787 ms
コンパイル使用メモリ 135,636 KB
実行使用メモリ 101,712 KB
最終ジャッジ日時 2024-06-22 20:23:09
合計ジャッジ時間 11,003 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 12 ms
5,376 KB
testcase_09 AC 15 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 866 ms
97,268 KB
testcase_13 AC 888 ms
97,264 KB
testcase_14 AC 877 ms
97,268 KB
testcase_15 AC 822 ms
93,476 KB
testcase_16 AC 911 ms
97,140 KB
testcase_17 AC 738 ms
101,712 KB
testcase_18 AC 377 ms
8,660 KB
testcase_19 AC 1,142 ms
81,336 KB
testcase_20 AC 329 ms
12,556 KB
testcase_21 AC 304 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

template<typename T>
class Compress{
    public:
    vector<T> data;
    int offset;
    Compress(vector<T> data_, int offset=0): offset(offset){
        data = data_;
        sort(begin(data), end(data));
        data.erase(unique(begin(data), end(data)), end(data));
    };
    int operator[](T x) {
        auto p = lower_bound(data.begin(), data.end(), x);
        assert(x == *p);
        return offset+(p-data.begin());
	}
    T inv(int x){
        return data[x-offset];
    }
    int size(){
        return data.size();
    }
};

using P = pair<int, int>;
int prev_pos[1000000][20];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, k, q; cin >> n >> k >> q;
    vector<int> a(k), b(k);
    for(int i = 0; i < k; i++){
        cin >> a[i] >> b[i]; a[i]--; b[i]--;
    }
    vector<vector<int>> operations(n);
    vector<P> ps;
    for(int i = 0; i < k; i++){
        operations[a[i]].push_back(i);
        operations[b[i]].push_back(i);
        ps.push_back(P(i, a[i]));
        ps.push_back(P(i, b[i]));
        ps.push_back(P(i+1, a[i]));
        ps.push_back(P(i+1, b[i]));
    }
    auto cp = Compress<P>(ps);
    int m = cp.size();
    for(int i = 0; i < m; i++){
        for(int j = 0; j < 20; j++){
            prev_pos[i][j] = -1;
        }
    }
    for(int i = 0; i < k; i++){
        prev_pos[cp[P(i+1, a[i])]][0] = cp[P(i, b[i])];
        prev_pos[cp[P(i+1, b[i])]][0] = cp[P(i, a[i])];

        auto p = lower_bound(operations[a[i]].begin(), operations[a[i]].end(), i);
        if(p != operations[a[i]].begin()){
            int idx = *prev(p);
            if(idx == i-1){
                if(a[i-1] == a[i]) prev_pos[cp[P(i, a[i])]][0] = cp[P(i-1, b[i-1])];
                else prev_pos[cp[P(i, a[i])]][0] = cp[P(i-1, a[i-1])];
            }else{
                prev_pos[cp[P(i, a[i])]][0] = cp[P(idx+1, a[i])];
            }
        }
        p = lower_bound(operations[b[i]].begin(), operations[b[i]].end(), i);
        if(p != operations[b[i]].begin()){
            int idx = *prev(p);
            if(idx == i-1){
                if(a[i-1] == b[i]) prev_pos[cp[P(i, b[i])]][0] = cp[P(i-1, b[i-1])];
                else prev_pos[cp[P(i, b[i])]][0] = cp[P(i-1, a[i-1])];
            }else{
                prev_pos[cp[P(i, b[i])]][0] = cp[P(idx+1, b[i])];
            }
        }
    }
    for(int j = 0; j+1 < 20; j++){
        for(int i = 0; i < m; i++){
            if(prev_pos[i][j] == -1) continue;
            prev_pos[i][j+1] = prev_pos[prev_pos[i][j]][j];
        }
    }
    auto go = [&](int idx, int cnt){
        for(int j = 0; j < 20; j++){
            if(cnt&(1<<j)) idx = prev_pos[idx][j];
            if(idx == -1) break;
        }
        return idx;
    };

    while(q--){
        int l, r, x; cin >> l >> r >> x; l--; r--; x--;
        if(operations[x].empty()){
            cout << x+1 << endl;
            continue;
        }

        auto p = upper_bound(operations[x].begin(), operations[x].end(), r);
        if(p == operations[x].begin()){
            cout << x+1 << endl;
            continue;
        }
        int ope_idx = *prev(p);
        int idx = cp[P(ope_idx+1, x)];
        int small = 0, large = 2*k;
        for(int j = 19; j >= 0; j--){
            int i = prev_pos[idx][j];
            if(i == -1) continue;
            if(cp.data[i].first < l) continue;
            idx = i;
        }
        cout << cp.data[idx].second+1 << endl;
    }
}
0