結果

問題 No.2002 Range Swap Query
ユーザー milanis48663220milanis48663220
提出日時 2022-07-12 00:41:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,701 bytes
コンパイル時間 1,882 ms
コンパイル使用メモリ 135,724 KB
実行使用メモリ 132,832 KB
最終ジャッジ日時 2023-09-04 22:51:01
合計ジャッジ時間 15,659 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,476 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 17 ms
4,380 KB
testcase_09 AC 17 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 12 ms
4,380 KB
testcase_12 AC 1,132 ms
128,716 KB
testcase_13 AC 1,112 ms
129,564 KB
testcase_14 AC 1,102 ms
128,536 KB
testcase_15 AC 1,053 ms
123,312 KB
testcase_16 AC 1,119 ms
128,424 KB
testcase_17 AC 936 ms
132,832 KB
testcase_18 AC 477 ms
10,160 KB
testcase_19 TLE -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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 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();
    auto prev_pos = vec2d(m, 20, -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