# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
342479 | mjhmjh1104 | The Potion of Great Power (CEOI20_potion) | C++14 | 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 <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
map<pair<int, int>, vector<int>> m;
vector<int> adj[100006];
vector<int> h;
void init(int N, int D, int H[]) {
for (int i = 0; i < N; i++) h.push_back(H[i]);
}
void curseChanges(int U, int A[], int B[]) {
for (int i = 0; i < U; i++) {
int a = A[i], b = B[i];
if (a > b) swap(a, b);
m[{ a, b }].push_back(i + 1);
adj[a].push_back(b);
adj[b].push_back(a);
}
auto it = m.begin();
while (it != m.end()) {
sort(it->second.begin(), it->second.end());
it = next(it);
}
}
struct cmp {
operator() (const int &a, const int &b) const {
return h[a] < h[b];
}
};
int question(int x, int y, int v) {
set<int, cmp> adjX, adjY;
for (auto &i: adj[x]) {
int a = x;
int b = i;
if (a > b) swap(a, b);
auto it = m.find({ a, b });
if (it == m.end()) continue;
int la = upper_bound(it->second.begin(), it->second.end(), v) - it->second.begin();
if (la % 2) adjX.insert(i);
}
for (auto &i: adj[y]) {
int a = y;
int b = i;
if (a > b) swap(a, b);
auto it = m.find({ a, b });
if (it == m.end()) continue;
int la = upper_bound(it->second.begin(), it->second.end(), v) - it->second.begin();
if (la % 2) adjY.insert(i);
}
if (adjX.empty() || adjY.empty()) return 1e9;
auto i = adjX.begin();
auto j = adjY.begin();
int res = 1e9;
while (i != adjX.end() && j != adjY.end()) {
res = min(res, abs(h[*i] - h[*j]));
if (h[*i] <= h[*j]) i = next(i);
else j = next(j);
}
return res;
}