# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
987296 | crafticat | The Potion of Great Power (CEOI20_potion) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
vector<pii> roads;
int n, d, u, q;
constexpr int inf = 1e9 + 7;
constexpr int none = 1e9;
vector<int> h;
vector<map<int, int>> to, from;
struct Node {
Node *left = nullptr, *right = nullptr;
int l, r, mid, exists = 0, id = 0, realL, realR;
Node(int l, int r, int id) : l(l) ,r(r),id(id) ,mid((r + l) / 2) {
realL = from[id][l];
realR = from[id][r];
}
void push() {
if (r - l > 1) {
if (!left) left = new Node(l,mid,id);
if (!right) right = new Node(mid,r,id);
}
}
};
Node* ins(int x, Node* node, int newX, int id) {
if (node->r - node->l <= 1) {
Node* z = new Node(node->l,node->r,id);
z->exists = node->exists;
z->exists += newX;
return z;
}
node->push();
Node* z = new Node(node->l,node->r,id);
if (x < node->mid) {
z->left = ins(x,node->left,newX,id);
z->right = node->right;
} else {
z->right = ins(x,node->right,newX,id);
z->left = node->left;
}
z->exists = z->left->exists + z->right->exists;
return z;
}
Node* addEdge(vector<set<pii>> &g, int a, int b, vector<Node*> &lastNodes) {
pii p = {h[b],b};
if (g[a].count(p)) {
g[a].erase(p);
return ins(to[a][p.first],lastNodes[a],-1,a);
}
else {
g[a].insert(p);
return ins(to[a][p.first],lastNodes[a],true,a);
}
}
int closest(Node* node, int x) {
if (!node) return inf;
if (node->exists == 0) return inf;
if (node->r - node->l <= 1 && node->exists > 0) return abs(x - node->realL);
if (node->realL <= x && node->realR >= x) {
return min(closest(node->left, x), closest(node->right, x));
}
if (node->realL >= x && node->left && node->left->exists > 0) return closest(node->left, x);
else {
if (node->right && node->right->exists > 0)
return closest(node->right, x);
else
return closest(node->left, x);
}
}
void dfs(Node* x, vector<int> &dfsData) {
if (x == nullptr) return;
if (x->r - x->l <= 1) {
dfsData.push_back(x->l);
return;
}
if (x->left && x->left->exists> 0) dfs(x->left,dfsData);
if (x->right && x->right->exists>0) dfs(x->right,dfsData);
}
vector<int> getDfs(Node* d) {
vector<int> ans;
dfs(d,ans);
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> d >> u >> q;
h.resize(n + 1);
vector<set<int>> ex(n);
vector<int> siz(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
}
for (int i = 0; i < u; ++i) {
int a, b; cin >> a >> b;
roads.emplace_back(a,b);
ex[a].insert(h[b]);
ex[b].insert(h[a]);
}
from.resize(n);
to.resize(n);
for (int i = 0; i < n; ++i) {
int genId =0;
for (auto v : ex[i]) {
to[i][v] = genId;
from[i][genId++] = v;
}
siz[i] = genId;
to[i][inf] = genId;
from[i][genId++] = inf;
}
// Time, Node* (Upper bound time)
vector<map<int,Node*>> nodes(n + 1);
vector<Node*> lastNodes(n + 1);
for (int i = 0; i < n; ++i) {
lastNodes[i] = new Node(0,siz[i],i);
nodes[i].insert({0, lastNodes[i]});
}
vector<set<pii>> g(n + 1);
for (int i = 0; i < u; ++i) {
auto [a,b] = roads[i];
lastNodes[a] = addEdge(g,a,b,lastNodes);
lastNodes[b] = addEdge(g,b,a,lastNodes);
nodes[a].insert({-i - 1, lastNodes[a]});
nodes[b].insert({-i -1, lastNodes[b]});
}
for (int i = 0; i < q; ++i) {
int a,b, t; cin >> a >> b >> t;
auto itB = nodes[b].lower_bound(-t);
auto itA = nodes[a].lower_bound(-t);
Node* nodeB = itB->second, *nodeA = itA->second;
if (nodeB->exists < nodeA->exists) {
swap(a,b);
swap(nodeA,nodeB);
} // A is smaller
if (nodeB->exists == 0 || nodeA->exists == 0) {
cout << none << endl;
continue;
}
int ans = 1e9;
for (auto x : getDfs(nodeA)) {
ans = min(ans, closest(nodeB,from[a][x]));
}
cout << ans << endl;
}
return 0;
}