#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 10;
int n, m, q, D, a[N], ord[N];
vector <int> dta[N], s[N];
vector <pair <int, int> > root[N];
struct node {
int lhs, rhs;
int s;
} st[N * 100];
int p[N], nNode;
int update(int id, int L, int R, int i) {
if (L == R) {
st[++nNode] = st[id];
st[nNode].s ^= 1;
return nNode;
}
int cur = ++nNode;
int mid = L + R >> 1;
st[cur] = st[id];
if (i <= mid) st[cur].lhs = update(st[id].lhs, L, mid, i);
else st[cur].rhs = update(st[id].rhs, mid + 1, R, i);
st[cur].s = st[st[cur].lhs].s + st[st[cur].rhs].s;
return cur;
}
void get(int id, int L, int R, vector <int>& cur) {
if (st[id].s == 0) return;
if (L == R) {
cur.push_back(L);
return;
}
int mid = L + R >> 1;
get(st[id].lhs, L, mid, cur);
get(st[id].rhs, mid + 1, R, cur);
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> D >> m >> q;
vector <pair <int, int> > s = {{-1, -1}};
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) p[i] = i;
sort(p, p + n, [&](int u, int v) {
return a[u] < a[v];
});
sort(a, a + n);
for (int i = 0; i < n; ++i) ord[p[i]] = i;
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
u = ord[u], v = ord[v];
s.push_back({u, v});
dta[u].push_back(v);
dta[v].push_back(u);
}
for (int i = 0; i < n; ++i) {
sort(dta[i].begin(), dta[i].end());
dta[i].erase(unique(dta[i].begin(), dta[i].end()), dta[i].end());
}
for (int i = 0; i < n; ++i) root[i].push_back({0, 0});
for (int i = 1; i <= m; ++i) {
auto [u, v] = s[i];
int _u = lower_bound(dta[v].begin(), dta[v].end(), u) - dta[v].begin();
int _v = lower_bound(dta[u].begin(), dta[u].end(), v) - dta[u].begin();
int r = root[u].back().second;
root[u].push_back({i, update(r, 0, dta[u].size() - 1, _v)});
r = root[v].back().second;
root[v].push_back({i, update(r, 0, dta[v].size() - 1, _u)});
}
while (q--) {
int u, v, id;
cin >> u >> v >> id;
u = ord[u], v = ord[v];
int _u = lower_bound(dta[v].begin(), dta[v].end(), u) - dta[v].begin();
int _v = lower_bound(dta[u].begin(), dta[u].end(), v) - dta[u].begin();
vector <int> cur[2];
int r = (--lower_bound(root[u].begin(), root[u].end(), make_pair(id, (int)1e9))) -> second;
get(r, 0, dta[u].size() - 1, cur[0]);
r = (--lower_bound(root[v].begin(), root[v].end(), make_pair(id, (int)1e9))) -> second;
get(r, 0, dta[v].size() - 1, cur[1]);
for (auto& x: cur[0]) x = a[dta[u][x]];
for (auto& x: cur[1]) x = a[dta[v][x]];
int ans = 1e9;
int i = 0, j = 0;
while (i < cur[0].size() && j < cur[1].size()) {
ans = min(ans, abs(cur[0][i] - cur[1][j]));
if (cur[0][i] < cur[1][j]) ++i;
else ++j;
}
cout << ans << endl;
}
}
Compilation message
potion.cpp: In function 'int update(int, int, int, int)':
potion.cpp:33:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int mid = L + R >> 1;
| ~~^~~
potion.cpp: In function 'void get(int, int, int, std::vector<int>&)':
potion.cpp:47:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = L + R >> 1;
| ~~^~~
potion.cpp: In function 'int main()':
potion.cpp:77:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | auto [u, v] = s[i];
| ^
potion.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | while (i < cur[0].size() && j < cur[1].size()) {
| ~~^~~~~~~~~~~~~~~
potion.cpp:102:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | while (i < cur[0].size() && j < cur[1].size()) {
| ~~^~~~~~~~~~~~~~~
potion.cpp:89:13: warning: unused variable '_u' [-Wunused-variable]
89 | int _u = lower_bound(dta[v].begin(), dta[v].end(), u) - dta[v].begin();
| ^~
potion.cpp:90:13: warning: unused variable '_v' [-Wunused-variable]
90 | int _v = lower_bound(dta[u].begin(), dta[u].end(), v) - dta[u].begin();
| ^~
/usr/bin/ld: /tmp/cc1ThHhP.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc8b2bCO.o:potion.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/cc1ThHhP.o: in function `main':
grader.cpp:(.text.startup+0xec): undefined reference to `init(int, int, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x184): undefined reference to `curseChanges(int, int*, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x1d9): undefined reference to `question(int, int, int)'
collect2: error: ld returned 1 exit status