結果
問題 | No.2002 Range Swap Query |
ユーザー | milanis48663220 |
提出日時 | 2022-07-12 00:42:57 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 4,807 bytes |
コンパイル時間 | 1,715 ms |
コンパイル使用メモリ | 136,036 KB |
実行使用メモリ | 78,160 KB |
最終ジャッジ日時 | 2024-06-22 20:17:24 |
合計ジャッジ時間 | 8,997 ms |
ジャッジサーバーID (参考情報) |
judge1 / 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 | 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 | 16 ms
5,376 KB |
testcase_09 | AC | 16 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 11 ms
5,376 KB |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | AC | 437 ms
8,528 KB |
testcase_19 | RE | - |
testcase_20 | AC | 317 ms
12,664 KB |
testcase_21 | AC | 323 ms
5,376 KB |
ソースコード
#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[500000][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; } }