#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;
}
Compilation message
potion.cpp:31:45: error: ISO C++ forbids declaration of 'operator()' with no type [-fpermissive]
31 | operator() (const int &a, const int &b) const {
| ^~~~~
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:37:5: error: 'set' was not declared in this scope
37 | set<int, cmp> adjX, adjY;
| ^~~
potion.cpp:5:1: note: 'std::set' is defined in header '<set>'; did you forget to '#include <set>'?
4 | #include <algorithm>
+++ |+#include <set>
5 | using namespace std;
potion.cpp:37:9: error: expected primary-expression before 'int'
37 | set<int, cmp> adjX, adjY;
| ^~~
potion.cpp:45:21: error: 'adjX' was not declared in this scope; did you mean 'adj'?
45 | if (la % 2) adjX.insert(i);
| ^~~~
| adj
potion.cpp:54:21: error: 'adjY' was not declared in this scope; did you mean 'adj'?
54 | if (la % 2) adjY.insert(i);
| ^~~~
| adj
potion.cpp:56:9: error: 'adjX' was not declared in this scope; did you mean 'adj'?
56 | if (adjX.empty() || adjY.empty()) return 1e9;
| ^~~~
| adj
potion.cpp:56:25: error: 'adjY' was not declared in this scope; did you mean 'adj'?
56 | if (adjX.empty() || adjY.empty()) return 1e9;
| ^~~~
| adj
potion.cpp:57:14: error: 'adjX' was not declared in this scope; did you mean 'adj'?
57 | auto i = adjX.begin();
| ^~~~
| adj
potion.cpp:58:14: error: 'adjY' was not declared in this scope; did you mean 'adj'?
58 | auto j = adjY.begin();
| ^~~~
| adj