#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <random>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()* ((uint64_t) new char | 1));
#define endl "\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(),v.end()
#define watch(x) cout<<(#x)<<" = "<<x<<endl
const int dr[]{ -1, -1, 0, 1, 1, 1, 0, -1 };
const int dc[]{ 0, 1, 1, 1, 0, -1, -1, -1 };
#if __cplusplus >= 201402L
template<typename T>
vector<T> create(size_t n) {
return vector<T>(n);
}
template<typename T, typename ... Args>
auto create(size_t n, Args ... args) {
return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#else
#endif
}
const int N = 1e5 + 9;
int a[N];
vector<pair<int, int>> L[N], G[N];
ll dist[N];
void merge_intervals(vector<pair<int, int>>& e) {
sort(all(e));
vector<pair<int, int>> tmp = move(e);
for (auto& it : tmp) {
if (e.empty() || e.back().second < it.first)
e.push_back(it);
else
e.back().second = max(e.back().second, it.second);
}
}
int n;
void dijkstra() {
memset(dist, INF, sizeof dist);
dist[1] = 0;
priority_queue<pair<ll, int>> q;
q.push({ 0,1 });
set<int> remL, remG;
for (int i = 2; i <= n; i++) {
remL.insert(i);
remG.insert(i);
}
while (q.size()) {
int cur = q.top().second;
ll w = -q.top().first;
q.pop();
if (w > dist[cur])continue;
remL.erase(cur);
remG.erase(cur);
for (auto& it : L[cur]) {
int b = it.first, c = it.second;
while (remL.size()) {
auto it = remL.lower_bound(b);
if (it == remL.end() || *it > c)break;
if (dist[*it] > dist[cur] + abs(a[cur] - a[*it])) {
dist[*it] = dist[cur] + abs(a[cur] - a[*it]);
q.push({ -dist[*it],*it });
}
remL.erase(it);
}
}
for (auto& it : G[cur]) {
int b = it.first, c = it.second;
while (remG.size()) {
auto it = remG.lower_bound(b);
if (it == remG.end() || *it > c)break;
if (dist[*it] > dist[cur] + abs(a[cur] - a[*it])) {
dist[*it] = dist[cur] + abs(a[cur] - a[*it]);
q.push({ -dist[*it],*it });
}
remG.erase(it);
}
}
}
}
int main() {
run();
int q;
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i];
while (q--) {
int a, b, c;
cin >> a >> b >> c;
if (max(b, a + 1) <= c)
G[a].push_back({ max(b,a + 1),c });
if (b <= min(c, a - 1))
L[a].push_back({ b,min(c,a - 1) });
}
for (auto& it : G)
merge_intervals(it);
for (auto& it : L)
merge_intervals(it);
dijkstra();
for (int i = 1; i <= n; i++) {
if (dist[i] == INF)dist[i] = -1;
cout << dist[i] << ' ';
}
}
Copy