#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 998244353
#define mxn 1000
#define inf 1000000000000000001ll
vector<pair<ll,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;
if(aq==bq)
bq++;
if(aq==cq)
cq--;
if(bq>cq)
continue;
if(aq<bq)
g[aq].push_back(make_pair(0,make_pair(bq, cq)));
else if(aq>cq)
g[aq].push_back(make_pair(1,make_pair(bq, cq)));
else if(bq<aq&&aq<cq)
{
g[aq].push_back(make_pair(1,make_pair(bq, aq-1)));
g[aq].push_back(make_pair(0,make_pair(aq+1, 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<pair<ll, ll>,pair<ll, ll> > > qq;
for(auto ch : g[1])
qq.insert(make_pair(make_pair(ch.first,1),ch.second));
while(qq.size())
{
pair<pair<ll, 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.second] + abs(a[bg.first.second]-a[(*it)]);
for(auto ch : g[(*it)])
qq.insert(make_pair(make_pair(ch.first+bg.first.first,(*it)),ch.second));
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