#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> adj[100'005], intersect[100'005];
struct chash {
uint32_t C = uint32_t(2e9 * acos(0)) + 7;
int R = chrono::steady_clock::now().time_since_epoch().count();
int operator()(int x) const {
return __builtin_bswap64((R ^ x) * C);
}
};
unordered_map<int, ll, chash> dist[100'005];
int sparse[17][200'005];
const ll INF = 1e18;
ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
int n = x.size(), m = l.size();
for (int i = 0; i < n; i++) {
sparse[0][i] = h[i];
}
for (int j = 1; j < 17; j++) {
for (int i = 0; i < n; i++) {
sparse[j][i] = max(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
}
}
for (int i = 0; i < m; i++) {
int j = l[i];
while (j <= r[i]) {
if (y[i] <= h[j]) {
adj[j].push_back(i);
intersect[i].push_back(j);
j++;
continue;
}
int mx = 0;
for (int k = 16; k >= 0; k--) {
if (y[i] > max(mx, sparse[k][j])) {
mx = max(mx, sparse[k][j]);
j += (1 << k);
}
}
if (j <= r[i]) {
adj[j].push_back(i);
intersect[i].push_back(j);
j++;
}
}
// for (auto j : intersect[i]) {
// cout << j << ' ';
// }
// cout << '\n';
assert(intersect[i].size() <= 11);
}
y.push_back(0);
priority_queue<pair<ll, pair<int, int>>> pq;
pq.push({0, {s, m}});
while (pq.size()) {
ll d = -pq.top().first;
int i = pq.top().second.first;
int j = y[pq.top().second.second];
pq.pop();
for (auto nxtedge : adj[i]) {
for (auto nxt : intersect[nxtedge]) {
ll newd = d + abs(j - y[nxtedge]) + abs(x[i] - x[nxt]);
if (dist[nxt].find(nxtedge) == dist[nxt].end() || newd < dist[nxt][nxtedge]) {
dist[nxt][nxtedge] = newd;
pq.push({-newd, {nxt, nxtedge}});
}
}
}
}
ll ans = INF;
for (int i = 0; i < m; i++) {
if (dist[g].find(i) != dist[g].end()) {
ans = min(ans, dist[g][i] + y[i]);
}
}
if (ans >= INF) return -1;
return ans;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int N, M;
cin >> N >> M;
vector<int> X(N), H(N);
for (int i = 0; i < N; i++) {
cin >> X[i] >> H[i];
}
vector<int> L(M), R(M), Y(M);
for (int i = 0; i < M; i++) {
cin >> L[i] >> R[i] >> Y[i];
}
int S, G;
cin >> S >> G;
cout << min_distance(X, H, L, R, Y, S, G) << '\n';
return 0;
}
Compilation message
/usr/bin/ld: /tmp/ccC6SE9g.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccOREoLj.o:walk.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status