#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 998244353
#define mxn 1000
#define inf 1000000000000000001ll
vector<pair<ll, ll>> g[100100];
int main()
{
ios::sync_with_stdio(0);
ll t = 1;
//cin >> t;
while(t--)
{
ll n, q;
cin >> n >> q;
ll a[n+1];
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 0; i < q; i++)
{
ll aq, bq, cq;
cin >> aq >> bq >> cq;
g[aq].push_back(make_pair(bq, cq));
}
ll h[100100];
for(int i = 1; i <= n; i++)
h[i] = inf;
h[1] = 0;
set<ll> indx;
for(int i = 2; i <= n; i++)
indx.insert(i);
set<pair<ll,pair<ll, ll>>> qq;
for(auto ch : g[1])
qq.insert(make_pair(1,ch));
while(qq.size())
{
pair<ll,pair<ll, ll>> bg = *qq.begin();
qq.erase(qq.begin());
ll st = bg.second.first;
ll en = bg.second.second;
auto it = indx.lower_bound(st);
while(it!=indx.end()&&(*it)<=en)
{
h[(*it)] = h[bg.first] + abs(a[bg.first]-a[(*it)]);
for(auto ch : g[(*it)])
{
qq.insert(make_pair((*it),ch));
}
indx.erase(it);
it = indx.lower_bound(st);
}
}
for(int i = 1; i <= n; i++)
if(h[i]==inf)
cout << -1 << " ";
else
cout << h[i] << " ";
cout << endl;
}
return 0;
}
Copy