結果

問題 No.2002 Range Swap Query
ユーザー milanis48663220milanis48663220
提出日時 2022-07-12 00:43:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,808 bytes
コンパイル時間 1,903 ms
コンパイル使用メモリ 135,912 KB
実行使用メモリ 101,708 KB
最終ジャッジ日時 2024-06-22 20:18:01
合計ジャッジ時間 12,359 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 13 ms
5,376 KB
testcase_09 AC 14 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 906 ms
97,136 KB
testcase_13 AC 935 ms
97,132 KB
testcase_14 AC 929 ms
97,140 KB
testcase_15 AC 862 ms
93,480 KB
testcase_16 AC 915 ms
97,012 KB
testcase_17 AC 817 ms
101,708 KB
testcase_18 AC 406 ms
8,536 KB
testcase_19 TLE -
testcase_20 AC 318 ms
12,672 KB
testcase_21 AC 315 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;
        while(large-small > 1){
            int c = (small+large)/2;
            auto i = go(idx, c);
            if(i == -1) {
                large = c;
                continue;
            }
            int array_idx = cp.data[i].first;
            if(array_idx < l) large = c;
            else small = c;
        }
        auto i = go(idx, small);
        cout << cp.data[i].second+1 << endl;
    }
}
0