Problem:
Problem L: Space Travel
Story
遠い昔、はるかかなたの銀河系で。
時は内乱の嵐が吹き荒れるさなか、凶悪な銀河帝国の軍勢が反乱軍の秘密基地を襲った。
恐るべき帝国宇宙艦隊の追撃から逃れた、ウーク・スターウォーカーによって率いられる自由の戦士たちは、銀河の辺境に新たな秘密基地を築くことにした。
反乱軍の一員であり、凄腕のプログラマーであるあなたにあたえられたミッションは、銀河系のそれぞれの惑星から最も離れた惑星を見つけることである。
Problem
銀河系には $N$ 個の惑星があり、$M$ 個の「橋」と呼ばれる秘密のルートがある。
惑星にはそれぞれ $1,2, \ldots N$ の番号が、橋にはそれぞれ $1,2, \ldots M$ の番号がついている。
各惑星の位置は実三次元空間上の点として表され、惑星 $p$ は $(x_p,y_p,z_p)$ に位置する。
$i$ 番目の橋は、惑星 $u_i$ から $v_i$ へと向かう秘密のルートである。
$i$ 番目の橋を使って惑星 $v_i$ から $u_i$ へ直接移動することはできないことに注意せよ。
惑星 $p$ と $q$ の距離を以下のように定義する。
$\mathrm{d} (p,q) = |x_p - x_q| + |y_p - y_q| + |z_p - z_q|$
惑星 $p$ から $0$ 個以上の橋を伝って到達可能な惑星の集合を $S_p$ とする。
各 $p$ について、$\displaystyle \max_{s \in S_p} \mathrm{d} (p,s)$ を求めよ。
ただし、反乱軍は常に凶悪な銀河帝国の軍勢に狙われているため、橋以外のルートで惑星間を移動することはできない。
Input
入力は以下の形式で与えられる。
$N$ $M$
$x_1$ $y_1$ $z_1$
$\vdots$
$x_N$ $y_N$ $z_N$
$u_1$ $v_1$
$\vdots$
$u_M$ $v_M$
Constraints
入力は以下の条件を満たす。
$1 \leq N \leq 2 \times 10^5$
$1 \leq M \leq 5 \times 10^5$
$1 \leq u_i , v_i \leq N$
$|x_p|,|y_p|,|z_p| \leq 10^8$
$u_i \neq v_i$
$i \neq j$ なら $(u_i,v_i) \neq (u_j,v_j)$
$p \neq q$ なら $(x_p,y_p,z_p) \neq (x_q,y_q,z_q)$
入力は全て整数である
Output
$N$ 行出力せよ。
$i$ 行目には $\displaystyle \max_{s \in S_i} \mathrm{d} (i,s)$ を出力せよ。
Sample Input 1
2 1
1 1 1
2 2 2
1 2
Sample Output 1
3
0
Sample Input 2
2 2
1 1 1
2 2 2
1 2
2 1
Sample Output 2
3
3
Sample Input 3
6 6
0 8 0
2 3 0
2 5 0
4 3 0
4 5 0
6 0 0
1 3
2 3
3 5
5 4
4 2
4 6
Sample Output 3
14
7
9
5
7
0
Sample Input 4
10 7
-65870833 -68119923 -51337277
-59513976 -24997697 -46968492
-37069671 -90713666 -45043609
-31144219 43731960 -5258464
-27501033 90001758 13168637
-96651565 -67773915 56786711
44851572 -29156912 28758396
16384813 -79097935 7386228
88805434 -79256976 31470860
92682611 32019492 -87335887
6 7
7 9
6 5
1 2
2 4
4 1
9 8
Sample Output 4
192657310
138809442
0
192657310
0
270544279
99779950
0
96664294
0

Submitted solutions:

code_snippet: // AOJ #3075 Space Travel
// 2025.4.17

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(ll n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

int main(){
    int n = Cin(), m = Cin();
    vector<ll> x(n),y(n),z(n);
    for(int i=0;i<n;i++) x[i] = Cin(), y[i] = Cin(), z[i] = Cin();

    vector<vector<int>> g(n), rg(n);
    for(int i=0;i<m;i++){
        int u = Cin()-1, v = Cin()-1;
        g[u].push_back(v);
        rg[v].push_back(u);
    }

    vector<char> vis(n,false);
    vector<int> ord; ord.reserve(n);
    for(int s=0;s<n;s++){
        if(!vis[s]){
            stack<pair<int,int>> st;
            st.emplace(s,0);
            vis[s]=1;
            while(!st.empty()){
                auto &tp = st.top();
                int u=tp.first, &i=tp.second;
                if(i < (int)g[u].size()){
                    int v=g[u][i++];
                    if(!vis[v]){
                        vis[v]=1;
                        st.emplace(v,0);
                    }
                } else {
                    ord.push_back(u);
                    st.pop();
                }
            }
        }
    }

    vector<int> comp(n,-1);
    int C=0;
    for(int idx=n-1;idx>=0;idx--){
        int u=ord[idx];
        if(comp[u]==-1){
            stack<int> st;
            st.push(u);
            comp[u]=C;
            while(!st.empty()){
                int w=st.top(); st.pop();
                for(int v: rg[w]){
                    if(comp[v]==-1){
                        comp[v]=C;
                        st.push(v);
                    }
                }
            }
            C++;
        }
    }

    vector<vector<int>> gg(C);
    vector<int> indeg(C,0);
    for(int u=0;u<n;u++){
        for(int v:g[u]){
            if(comp[u]!=comp[v]){
                gg[comp[u]].push_back(comp[v]);
                indeg[comp[v]]++;
            }
        }
    }

    vector<int> topo; topo.reserve(C);
    queue<int> q;
    for(int i=0;i<C;i++) if(indeg[i]==0) q.push(i);
    while(!q.empty()){
        int u=q.front(); q.pop();
        topo.push_back(u);
        for(int v:gg[u]) if(--indeg[v]==0) q.push(v);
    }

    vector<ll> ans(n, 0);

    for(int msk=0; msk<8; msk++){
        int sx = (msk & 1) ?  1 : -1;
        int sy = (msk & 2) ?  1 : -1;
        int sz = (msk & 4) ?  1 : -1;

        vector<ll> A(n);
        for(int i=0;i<n;i++) A[i] = sx*x[i] + sy*y[i] + sz*z[i];

        vector<ll> maxA(C, LLONG_MIN), dp;
        for(int i=0;i<n;i++){
            int c = comp[i];
            maxA[c] = max(maxA[c], A[i]);
        }

        dp = maxA;
        for(int idx = C-1; idx >= 0; idx--){
            int u = topo[idx];
            for(int v: gg[u]) dp[u] = max(dp[u], dp[v]);
        }

        for(int i=0;i<n;i++) ans[i] = max(ans[i], dp[comp[i]] - A[i]);
    }
    for(int i=0;i<n;i++) Cout(ans[i]);
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 49620
final_rank: 1

code_snippet: #include "bits/stdc++.h"

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;


template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}

vector<int> scc(vector<vector<int>>& edges){
    int n = edges.size();
    vector<vector<int>> rev(n);

    for(int i = 0; i < n; ++i)
        for(auto& x : edges[i])
            rev[x].emplace_back(i);

    vector<i64> dfs_num(n, -1);
    vector<i64> flag(n, 0);
    int num = 0;
    function<void(int)> dfs = [&](int pos){
        flag[pos] = 1;
        for(auto& xx : edges[pos])
            if(!flag[xx]){
                dfs(xx);
        }
        dfs_num[pos] = num++;
    };

    for(int i = 0; i < n; ++i)
        if(!flag[i])
            dfs(i);

    vector<int> dfs_inv(n);
    for(int i = 0; i < n; ++i)
        dfs_inv[n - 1 - dfs_num[i]] = i;

    num = 0;

    vector<int> scc_vec(n, -1);

    function<void(int)> rdfs = [&](int pos){
        scc_vec[pos] = num;
        for(auto t : rev[pos])
            if(scc_vec[t] == -1)
                rdfs(t);
    };

    for(int i = 0; i < n; ++i)
        if(scc_vec[dfs_inv[i]] == -1){
            rdfs(dfs_inv[i]);
            ++num;
        }

    return scc_vec;
}

struct Result{
    int dag_size;
    vector<vector<int>> dag_graph;
    // 元のグラフでi番目の頂点が何番目の強連結成分に含まれるか
    vector<int> elements;
    // i番目の強連結成分に含まれる頂点のリスト
    vector<vector<int>> tps_list;
    // トポソしてi番目にくる頂点のindex
    vector<int> tps_order;
    // DAGのi番目の頂点をトポソした時の番号
    vector<int> tps_index;
};

Result scc_dag(vector<vector<int>>& edges){
    int n = edges.size();
    vector<int> scc_vec = scc(edges);
    int m = *max_element(scc_vec.begin(), scc_vec.end()) + 1;
    vector<vector<int>> dag_graph(m);

    queue<int> tps_que;
    vector<int> in_count(m, 0);
    vector<int> tps(m, -1);
    vector<int> tps_idx(m);
    for(int i = 0; i < n; ++i){
        for(auto j : edges[i]){
            if(scc_vec[i] == scc_vec[j])
                continue;
            dag_graph[scc_vec[i]].push_back(scc_vec[j]);
            ++in_count[scc_vec[j]];
        }
    }
    for(int i = 0; i < m; ++i)
        if(in_count[i] == 0)
            tps_que.push(i);
    int cnt = 0;
    while(!tps_que.empty()){
        int x = tps_que.front();
        tps_idx[x] = cnt;
        tps[cnt++] = x;
        tps_que.pop();
        for(auto y : dag_graph[x])
            if(--in_count[y] == 0)
                tps_que.push(y);
    }
    assert(cnt == m);

    vector<vector<int>> tps_list(m);
    for(int i = 0; i < n; ++i)
        tps_list[scc_vec[i]].push_back(i);

    Result res;
    res.dag_size = m;
    res.elements = move(scc_vec);
    res.tps_index = move(tps_idx);
    res.tps_order = move(tps);
    res.tps_list = move(tps_list);
    res.dag_graph = move(dag_graph);
    return res;
}




signed main(){
    int n, m;
    cin >> n >> m;
    vector<int> x(n), y(n), z(n);
    vector<vector<int>> v(n, vector<int>(8, 0));
    for(int i = 0; i < n; ++i){
        cin >> x[i] >> y[i] >> z[i];
        for(int j = 0; j < 8; ++j){
            v[i][j] += x[i] * (j & 1 ? -1 : 1);
            v[i][j] += y[i] * (j & 2 ? -1 : 1);
            v[i][j] += z[i] * (j & 4 ? -1 : 1);
        }
    }
    vector<vector<int>> edges(n);
    for(int i = 0; i < m; ++i){
        int p, q;
        cin >> p >> q;
        --p, --q;
        edges[q].push_back(p);
    }
    Result res = scc_dag(edges);
    int k = res.dag_size;
    vector<vector<int>> dp(k, vector<int>(8, -MOD));
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < 8; ++j)
            chmax(dp[res.elements[i]][j], v[i][j]);

    for(auto i : res.tps_order){
        for(int j = 0; j < 8; ++j){
            for(auto nex : res.dag_graph[i]){
                chmax(dp[nex][j], dp[i][j]);
            }
        }
    }

    for(int i = 0; i < n; ++i){
        int dist = 0;
        for(int j = 0; j < 8; ++j){
            chmax(dist, dp[res.elements[i]][j] - v[i][j]);
        }
        cout << dist << endl;
    }
}
accuracy: 1
time_ms: 660
memory_kb: 74776
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
ll inf = 1e18;

//1-indexed!
class strong_components{
private:
    vector<vector<int>> v,rv,nv,cmp_node;
    vector<int> rs,visited,cmp,cmp_size;
    int num_cmp;
    void dfs(int n){
        visited[n] = 1;
        for(auto x:v[n]) if(!visited[x]) dfs(x);
        rs.push_back(n);
    }
    void rdfs(int n,int cnt){
        visited[n] = 1;
        cmp[n] = cnt;
        for(auto x:rv[n]) if(!visited[x]) rdfs(x,cnt);
    }
public:
    strong_components(int N,vector<vector<int>>& graph){
        v = graph;
        rv = cmp_node = vector<vector<int>>(N+1);
        visited = cmp = cmp_size = vector<int>(N+1,0);
        for(int i=1;i<=N;i++) for(auto x:v[i]) rv[x].push_back(i);
        for(int i=1;i<=N;i++) if(!visited[i]) dfs(i);
        for(int i=1;i<=N;i++) visited[i] = 0;
        int now = 1;
        for(int i=rs.size()-1;i>=0;i--) if(!visited[rs[i]]) rdfs(rs[i],now++);
        nv = vector<vector<int>>(now+1);
        for(int i=1;i<=N;i++){
            cmp_node[cmp[i]].push_back(i);
            for(auto x:v[i]){
                if(cmp[i]!=cmp[x]){
                    nv[cmp[x]].push_back(cmp[i]);
                    cmp_size[cmp[x]]++;
                }
            }
        }
        num_cmp = now-1;
    }
    int find(int n){return cmp[n];}
    vector<int> get_cmp_node(int n){return cmp_node[n];}
    vector<vector<int>> get_cmp_graph(){return nv;}
    int get_num_cmp() {return num_cmp;}
    bool is_same_group(int a,int b){return cmp[a]==cmp[b];}
};

int dx[8] = {1,-1,1,-1,1,1,-1-1},dy[8] = {1,1,-1,-1,1,-1,1,-1},dz[8] = {1,1,1,1,-1,-1,-1,-1};

int N,M;
vector<ll> X(200010),Y(200010),Z(200010);
vector<ll> ans(200010);
vector<vector<int>> graph(200010);
vector<vector<int>> cmp_graph(200010);
vector<vector<int>> cmp_node(200010);
vector<vector<int>> inv_graph(200010);

ll ma[200010][8] = {};
ll mi[200010][8] = {};
ll maid[200010][8] = {};
ll miid[200010][8] = {};


//全体管理
int main(){
    cin >> N >> M;
    for(int i=1;i<=N;i++){
        cin >> X[i] >> Y[i] >> Z[i];
    }
    for(int i=1;i<=M;i++){
        int a,b;
        cin >> a >> b;
        graph[a].push_back(b);
    }
    strong_components scc(N,graph);
    cmp_graph = scc.get_cmp_graph();
    for(int i=1;i<=N;i++) cmp_node[i] = scc.get_cmp_node(i);
    int num_node = scc.get_num_cmp();
    for(int i=0;i<=N;i++) for(int j=0;j<8;j++){
        ma[i][j] = -inf;
        mi[i][j] = inf;
        maid[i][j] = -1;
        miid[i][j] = -1;
    }
    vector<int> cnt(N+1);
    for(int i=1;i<=num_node;i++) cmp_graph[0].push_back(i);
    for(int i=0;i<=num_node;i++) for(auto x:cmp_graph[i]) cnt[x]++;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int now = Q.front(); Q.pop();
        //強連結成分内の点を追加
        for(auto x:cmp_node[now]){
            for(int j=0;j<8;j++){
                ll val = X[x]*dx[j]+Y[x]*dy[j]+Z[x]*dz[j];
                if(ma[now][j]<val){
                    ma[now][j] = val;
                    maid[now][j] = x;
                }
                if(mi[now][j]>val){
                    mi[now][j] = val;
                    miid[now][j] = x;
                }
            }
        }
        //強連結成分内の各点の答えを求める
        for(auto x:cmp_node[now]){
            //cerr << "now: " << now << " x: " << x << endl;
            for(int j=0;j<8;j++){
                if(maid[now][j]!=-1){
                    int id = maid[now][j];
                    ans[x] = max(ans[x],abs(X[x]-X[id])+abs(Y[x]-Y[id])+abs(Z[x]-Z[id]));
                }
                if(miid[now][j]!=-1){
                    int id = miid[now][j];
                    ans[x] = max(ans[x],abs(X[x]-X[id])+abs(Y[x]-Y[id])+abs(Z[x]-Z[id]));
                }
            }
        }
        //行き先に自分の情報を伝える
        for(auto x:cmp_graph[now]){
            for(int j=0;j<8;j++){
                if(ma[x][j]<ma[now][j]){
                    ma[x][j] = ma[now][j];
                    maid[x][j] = maid[now][j];
                }
                if(mi[x][j]>mi[now][j]){
                    mi[x][j] = mi[now][j];
                    miid[x][j] = miid[now][j];
                }               
            }
            cnt[x]--;
            if(!cnt[x]) Q.push(x);
        }
    }
    for(int i=1;i<=N;i++) cout << ans[i] << endl;
}
accuracy: 1
time_ms: 730
memory_kb: 148212
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

struct SCC{
  vector<vector<Int> > G,R,T,C;
  vector<Int> vs,used,blg;
  SCC(){}
  SCC(Int n):G(n),R(n),used(n),blg(n){}

  void add_edge(Int u,Int v){
    G[u].emplace_back(v);
    R[v].emplace_back(u);
  }

  void dfs(Int v){
    used[v]=1;
    for(Int u:G[v])
      if(!used[u]) dfs(u);
    vs.emplace_back(v);
  }

  void rdfs(Int v,Int k){
    used[v]=1;
    blg[v]=k;
    C[k].emplace_back(v);
    for(Int u:R[v])
      if(!used[u]) rdfs(u,k);
  }

  Int build(){
    Int n=G.size();
    for(Int v=0;v<n;v++)
      if(!used[v]) dfs(v);

    fill(used.begin(),used.end(),0);
    Int k=0;
    for(Int i=n-1;i>=0;i--){
      if(!used[vs[i]]){
        T.emplace_back();
        C.emplace_back();
        rdfs(vs[i],k++);
      }
    }
    for(Int v=0;v<n;v++)
      for(Int u:G[v])
        if(blg[v]!=blg[u])
          T[blg[v]].push_back(blg[u]);

    for(Int i=0;i<k;i++){
      sort(T[i].begin(),T[i].end());
      T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());
    }
    return k;
  }
  Int operator[](Int k) const{return blg[k];};
};


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  Int n,m;
  cin>>n>>m;
  vector<Int> xs(n),ys(n),zs(n);
  for(Int i=0;i<n;i++) cin>>xs[i]>>ys[i]>>zs[i];

  SCC scc(n);
  for(Int i=0;i<m;i++){
    Int u,v;
    cin>>u>>v;
    u--;v--;
    scc.add_edge(v,u);
  }
  Int k=scc.build();

  vector<Int> ans(n,0);
  for(Int bit=0;bit<8;bit++){

    vector<Int> vs(n,0);
    for(Int i=0;i<n;i++){
      vs[i]+=(bit&1)?xs[i]:-xs[i];
      vs[i]+=(bit&2)?ys[i]:-ys[i];
      vs[i]+=(bit&4)?zs[i]:-zs[i];
    }

    const Int INF = 1e18;
    vector<Int> dp(k,-INF);
    for(Int i=0;i<n;i++)
      chmax(dp[scc.blg[i]],vs[i]);

    for(Int v=0;v<k;v++)
      for(Int u:scc.T[v])
        chmax(dp[u],dp[v]);

    for(Int i=0;i<n;i++)
      chmax(ans[i],dp[scc.blg[i]]-vs[i]);
  }

  for(Int i=0;i<n;i++) cout<<ans[i]<<"\n";
  cout<<flush;
  return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 62764
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
#define dmp(x) cerr << __LINE__ << " " << #x << " " << x << endl;
template<class T> void chmin(T& a,const T& b){if(a>b)a=b;}
template<class T> void chmax(T& a,const T& b){if(a<b)a=b;}
template<class T,class U>
ostream& operator << (ostream& os,pair<T,U>& p){
	os << p.fi << ',' << p.sec; return os;
}
template<class T,class U>
istream& operator >> (istream& is,pair<T,U>& p){
	is >> p.fi >> p.sec; return is;
}
template<class T>
ostream& operator << (ostream &os,const vector<T> &vec){
	for(int i=0;i<vec.size();i++){
		os << vec[i];
		if(i+1<vec.size())os << ' ';
	}
	return os;
}
template<class T>
istream& operator >> (istream &is,vector<T>& vec){
	for(int i=0;i<vec.size();i++)is >> vec[i];
	return is;
}
vector<int> G[200100],rG[200100];
set<int> cG[200100];
vector<int> vs;
int V,E,Q;
bool used[200100];
int cmp[200100];
vector<int> cmpv[200100];
void add_edge(int from,int to){
    G[from].pb(to);
    rG[to].pb(from);
    return;
}
void dfs(int s){
    used[s]=true;
    for(int i=0;i<G[s].size();i++){
        if(!used[G[s][i]])dfs(G[s][i]);
    }
    vs.pb(s);
}
void rdfs(int s,int k){
    used[s]=true;
    cmp[s]=k;
    cmpv[k].pb(s);
    for(int i=0;i<rG[s].size();i++){
      int u = rG[s][i];
      if(!used[rG[s][i]]){
        rdfs(rG[s][i],k);
      }else{
        if(cmp[u]!=k){
          cG[cmp[u]].insert(k);
        }
      }
    }
}
int scc(){
    memset(used,0,sizeof(used));
    vs.clear();
    for(int v=0;v<V;v++){
        if(!used[v])dfs(v);
    }
    memset(used,0,sizeof(used));
    int k=0;
    for(int i=vs.size()-1;i>=0;i--){
        if(!used[vs[i]])rdfs(vs[i],k++);
    }
    return k;
}
int same(int u,int v){
    if(cmp[u]==cmp[v])return 1;
    else return 0;
}
int x[200100],y[200100],z[200100];
int w[200100][4];
int mx[200100][4],mi[200100][4];
int ans[200100];
int main(){
  cin >> V >> E;
  for(int i=0;i<V;i++){
    cin >> x[i] >> y[i] >> z[i];
    w[i][0] = x[i]+y[i]+z[i];
    w[i][1] = x[i]-y[i]+z[i];
    w[i][2] = x[i]+y[i]-z[i];
    w[i][3] = x[i]-y[i]-z[i];
  }
  for(int i=0;i<V;i++){
    ans[i] = -INF;
  }
  for(int i=0;i<V;i++){
    for(int j=0;j<4;j++){
      mx[i][j] = -INF;
      mi[i][j] = INF;
    }
  }
  for(int i=0;i<E;i++){
      int s,t;
      cin >> s >> t;
      s--;t--;
      add_edge(s,t);
  }
  int k = scc();
  // for(int i=0;i<k;i++){
  //   cout << cmpv[i] << endl;
  // }      
  for(int i=k-1;i>=0;i--){
    // cout << i << ',' << endl;
    for(int j=0;j<cmpv[i].size();j++){
      int u = cmpv[i][j];
      for(int idx=0;idx<4;idx++){
        chmin(mi[i][idx],w[u][idx]);
        chmax(mx[i][idx],w[u][idx]);
      }
    }
    for(auto it = cG[i].begin();it!=cG[i].end();it++){
      int to = *it;
      for(int idx=0;idx<4;idx++){
        chmin(mi[i][idx],mi[to][idx]);
        chmax(mx[i][idx],mx[to][idx]);
      }
    }
    for(int j=0;j<cmpv[i].size();j++){
      int v = cmpv[i][j];
      for(int idx=0;idx<4;idx++){
        chmax(ans[v],max(abs(mx[i][idx]-w[v][idx]),abs(mi[i][idx]-w[v][idx])));
      }
    }
  }
  for(int i=0;i<V;i++){
    cout << ans[i] << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 660
memory_kb: 73740
final_rank: 9

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int N,M;
ll loc[200020][3];

vector<int> g[200020];
int far[200010][8];
bool visited[200020][8];
ll ans[200020];

ll calc(int id,int t){
    ll res=0;
    for(int j=0;j<3;j++){
        if(t&(1<<j)) res+=loc[id][j];
        else res-=loc[id][j];
    }
    return res;
}

void dfs(int now,int t,int tar){
    visited[now][t]=true;
    far[now][t]=tar;
    for(auto nex:g[now]){
        if(visited[nex][t]) continue;
        dfs(nex,t,tar);
    }
    return;
}

int main(){
  cin>>N>>M;
  rep(i,N) cin>>loc[i][0]>>loc[i][1]>>loc[i][2];
  rep(i,M){
      int a,b;
      cin>>a>>b;
      a--;b--;
      g[b].push_back(a);
  }

  for(int k=0;k<8;k++){
      vector<pair<ll,int>> v;
      for(int i=0;i<N;i++){
          ll r=calc(i,k);
          v.push_back(mkp(r,i));
      }
      sort(v.begin(),v.end());
      reverse(v.begin(),v.end());

      for(int i=0;i<v.size();i++){
          int now=v[i].second;
          if(visited[now][k]) continue;
          dfs(now,k,now);
      }
  }

  for(int i=0;i<N;i++){
      for(int k=0;k<8;k++){
          ans[i]=max(ans[i],calc(i,~k)+calc(far[i][k],k));
      }
      cout<<ans[i]<<endl;
  }

  return 0;
}
accuracy: 1
time_ms: 810
memory_kb: 42420
final_rank: 7

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Strongly Connected Component */

/*
  1. dfsをして、戻るときに1から順に番号を付ける
  2. 数値が大きいノードから逆辺を使ってdfsをする。
  すでに訪れているノードには行かない。
  たどり着けるノードが同じ連結成分に属する。
*/

struct SCC{
  int n;
  vector<vector<int> > G, rG;
  vector<int> vs, cmp;
  vector<bool> used;

  SCC(int n):n(n), G(n), rG(n), cmp(n){}

  void add_edge(int from, int to){
    G[from].push_back(to);
    rG[to].push_back(from);
  }

  void dfs(int v){
    used[v] = true;
    for(int to : G[v])
      if(!used[to]) dfs(to);
    vs.push_back(v);
  }

  void rdfs(int v, int k){
    used[v] = true;
    cmp[v] = k;
    for(int to : rG[v])
      if(!used[to]) rdfs(to, k);
  }

  int solve(){
    used.assign(n, false);
    vs.clear();
    for(int i=0; i<n; i++)
      if(!used[i]) dfs(i);
    used.assign(n, false);
    int k = 0;
    for(int i=(int)vs.size()-1; i>=0; i--)
      if(!used[vs[i]]) rdfs(vs[i], k++);
    return k; //強連結成分数
  }

  //属する強連結成分番号(トポロジカル順)
  int operator[](int k) const {
    return cmp[k];
  }
};


vector<int> groups[SIZE];
vector<int> G[SIZE];
vector<int> vecMax[SIZE], vecMin[SIZE];
int ans[SIZE];


int main(){
  int N, M;
  vector<vector<int> > points;


  scanf("%d%d", &N, &M);

  SCC scc(N);

  for(int i=0; i<N; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);

    vector<int> vec;

    for(int s1=-1; s1<=1; s1+=2)
      for(int s2=-1; s2<=1; s2+=2)
        for(int s3=-1; s3<=1; s3+=2)
          vec.push_back(x * s1 + y * s2 + z * s3);

    points.push_back(vec);
  }

  for(int i=0; i<M; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--; v--;

    scc.add_edge(u, v);
    G[u].push_back(v);
  }


  scc.solve();

  for(int i=0; i<N; i++) {
    groups[scc[i]].push_back(i);
  }


  for(int i=N-1; i>=0; i--) {
    vecMin[i].assign(8, INF);
    vecMax[i].assign(8, -INF);

    for(int p : groups[i]) {
      for(int q : G[p]) {

        debug(q);
        debug(scc[q]);

        for(int j=0; j<8; j++) {
          vecMin[i][j] = min(vecMin[i][j], vecMin[scc[q]][j]);
          vecMax[i][j] = max(vecMax[i][j], vecMax[scc[q]][j]);
        }
      }

      for(int j=0; j<8; j++) {
        vecMin[i][j] = min(vecMin[i][j], points[p][j]);
        vecMax[i][j] = max(vecMax[i][j], points[p][j]);
      }

      debug(vecMin[i]);
      debug(vecMax[i]);
    }

    for(int p : groups[i]) {
      for(int j=0; j<8; j++) {
        ans[p] = max(ans[p], abs(vecMin[i][j] - points[p][j]));
        ans[p] = max(ans[p], abs(vecMax[i][j] - points[p][j]));
      }
    }
  }

  for(int i=0; i<N; i++) {
    printf("%d\n", ans[i]);
  }

  return 0;
}
accuracy: 1
time_ms: 460
memory_kb: 93920
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}
const int32 DIRECTED = 0;
const int32 UNDIRECTED = 1;

template<int32 isUNDIRECTED=0>
class Graph{
    struct Edge{
        int32 u, v, id;
        int64 c;
        Edge(int32 u, int32 v, int64 c=0, int32 id=0):u(u), v(v), c(c), id(id){}
    };

    int32 V, E;
    vector<vector<Edge>> G;
    vector<Edge> Es;
public:
    Graph(){}
    Graph(int32 V):V(V){G.resize(V);}
    Graph(const Graph<isUNDIRECTED>& g):V(g.V), E(g.E), G(g.G), Es(g.Es){}

    void add_edge(int32 u, int32 v, int64 c=0, int32 id=0){
        G[u].emplace_back(u, v, c, id);
        if(isUNDIRECTED) G[v].emplace_back(v, u, c, id);
        Es.emplace_back(u, v, c, id);
        E++;
    }

    const vector<Edge>& operator[](int32 k){
        return G[k];
    }

    int64 size() {
        return G.size();
    }
};

class SCC{
private:
    Graph<DIRECTED> orgG, revG, newG;
    vector<int32> ord, comp;
    vector<bool> used;
    vector<vector<int32>> vs;

    int32 V, nV;
public:
    SCC(){}
    SCC(int32 V):orgG(V), revG(V), comp(V, -1), used(V, 0), V(V){}

    void add_edge(int32 u, int32 v){
        orgG.add_edge(u, v);
        revG.add_edge(v, u);
    }

    void dfs(int32 v){
        used[v] = true;
        for(auto e : orgG[v]){
            if(!used[e.v]) dfs(e.v);
        }
        ord.push_back(v);
    }

    void rdfs(int32 v, int32 k){
        used[v] = true;
        comp[v] = k;
        for(auto e : revG[v]){
            if(!used[e.v]) rdfs(e.v, k);
        }
    }

    int32 build(){
        for(int32 i = 0;i < V;i++){
            if(!used[i]) dfs(i);
        }
        fill(used.begin(), used.end(), 0);
        int32 k = 0;
        for(int32 i = ord.size()-1;i >= 0;i--){
            if(!used[ord[i]]) rdfs(ord[i], k++);
        }
        nV = k;

        vs.resize(k, vector<int32>());
        for(int32 i = 0;i < V;i++)
            vs[comp[i]].push_back(i);

        newG = Graph<DIRECTED>(k);
        for(int32 i = 0;i < V;i++){
            for(auto e : orgG[i]){
                if(comp[i] != comp[e.v])
                    newG.add_edge(comp[i], comp[e.v], e.c);
            }
        }
        return k;
    }

    int32 size(){
        return nV;
    }
    int64 size(int64 v) {
        return vs[v].size();
    }

    const Graph<DIRECTED>& graph(){
        return newG;
    }

    const vector<int32>& vertices(int32 v){
        return vs[v];
    }

    int32 operator[](int32 k){
        return comp[k];
    }
};

int64 N, M;
vector<int64> x, y, z;
SCC scc;
Graph<DIRECTED> G;
vector<int64> res, used;
vector<vector<PLL>> D;

int64 dist(int64 a, int64 b) {
    return abs(x[a]-x[b]) + abs(y[a]-y[b]) + abs(z[a]-z[b]);
}

void dfs(int64 v) {
    if (used[v]) return;
    used[v] = 1;
    REP(i, G[v].size()) {
        dfs(G[v][i].v);
        REP(j, 8) {
            chmin(D[v][j], D[G[v][i].v][j]);
        }
    }
    auto vs = scc.vertices(v);
    REP(i, vs.size()) {
        REP(j, 8) {
            int64 nx = ((j & 1) ? -1 : 1) * x[vs[i]];
            int64 ny = ((j & 2) ? -1 : 1) * y[vs[i]];
            int64 nz = ((j & 4) ? -1 : 1) * z[vs[i]];
            chmin(D[v][j], PLL(nx+ny+nz, vs[i]));
        }
    }
    REP(i, vs.size()) {
        int64 vv = vs[i];
        REP(j, 8) {
            chmax(res[vv], dist(vv, D[v][j].sc));
        }
    }
}

int main(void) {
    cin >> N >> M;
    x.resize(N); y.resize(N); z.resize(N);
    REP(i, N) {
        cin >> x[i] >> y[i] >> z[i];
    }
    scc = SCC(N);
    REP(i, M) {
        int64 u, v;
        cin >> u >> v; u--; v--;
        scc.add_edge(u, v);
    }
    scc.build();
    res.resize(N, -1);
    used.resize(N, 0);
    G = scc.graph();
    D.resize(N, vector<PLL>(8, PLL(INF_LL, -1)));
    REP(i, N) {
        REP(j, 8) {
            int64 nx = ((j & 1) ? -1 : 1) * x[i];
            int64 ny = ((j & 2) ? -1 : 1) * y[i];
            int64 nz = ((j & 4) ? -1 : 1) * z[i];
        }
    }
    REP(i, scc.size()) {
        dfs(i);
    }
    REP(i, res.size()) {
        cout << res[i] << endl;
    }
}
accuracy: 1
time_ms: 780
memory_kb: 152804
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;

using i64 = long;

const i64 INF = 1e18 + 7;

vector<i64> scc(vector<vector<i64>>& edges){
    i64 n = edges.size();
    vector<vector<i64>> rev(n);

    for(i64 i = 0; i < n; ++i)
        for(auto& x : edges[i])
            rev[x].emplace_back(i);

    vector<i64> dfs_num(n, -1);
    vector<i64> flag(n, 0);
    i64 num = 0;
    function<void(i64)> dfs = [&](i64 pos){
        flag[pos] = 1;
        for(auto& xx : edges[pos])
            if(!flag[xx]){
                dfs(xx);
        }
        dfs_num[pos] = num++;
    };

    for(i64 i = 0; i < n; ++i)
        if(!flag[i])
            dfs(i);

    vector<i64> dfs_inv(n);
    for(i64 i = 0; i < n; ++i)
        dfs_inv[n - 1 - dfs_num[i]] = i;

    num = 0;

    vector<i64> scc_vec(n, -1);

    function<void(i64)> rdfs = [&](i64 pos){
        scc_vec[pos] = num;
        for(auto t : rev[pos])
            if(scc_vec[t] == -1)
                rdfs(t);
    };

    for(i64 i = 0; i < n; ++i)
        if(scc_vec[dfs_inv[i]] == -1){
            rdfs(dfs_inv[i]);
            ++num;
        }

    return scc_vec;
}

signed main(){

    i64 n, m;
    cin >> n >> m;
    vector<vector<i64>> pos_(n, vector<i64>(8, 0));
    for(i64 i = 0; i < n; ++i){
        vector<i64> pos(3);
        for(auto& x : pos)
            cin >> x;
        for(i64 j = 0; j < 8; ++j){
            for(i64 k = 0; k < 3; ++k){
                i64 mul = (j & (1 << k)) ? 1 : -1;
                pos_[i][j] += mul * pos[k];
            }
        }
    }

    vector<vector<i64>> edges(n);
    for(i64 i = 0; i < m; ++i){
        i64 a, b;
        cin >> a >> b;
        edges[--a].emplace_back(--b);
    }
    auto ret = scc(edges);
    i64 n_ = *max_element(ret.begin(), ret.end()) + 1;
    vector<vector<i64>> edg(n_);
    for(i64 i = 0; i < n; ++i){
        for(auto& j : edges[i])
            edg[ret[i]].emplace_back(ret[j]);
    }
    vector<i64> cnt(n_, 0);
    for(i64 i = 0; i < n_; ++i){
        sort(edg[i].begin(), edg[i].end());
        edg[i].erase(unique(edg[i].begin(), edg[i].end()), edg[i].end());
    }
    vector<i64> tps;
    queue<i64> que;
    for(i64 i = 0; i < n_; ++i)
        if(!cnt[i])
            que.emplace(i);
    while(!que.empty()){
        i64 i = que.front();
        tps.emplace_back(i);
        que.pop();
        for(auto& e : edg[i]){
            if(--cnt[e] == 0)
                que.emplace(e);
        }
    }
    vector<vector<i64>> elm(n_, vector<i64>(8, -INF));
    for(i64 i = 0; i < n; ++i)
        for(i64 j = 0; j < 8; ++j)
            elm[ret[i]][j] = max(elm[ret[i]][j], pos_[i][j]);

    reverse(tps.begin(), tps.end());
    for(auto& pos : tps){
        for(auto& e : edg[pos]){
            for(i64 i = 0; i < 8; ++i)
                elm[pos][i] = max(elm[pos][i], elm[e][i]);
        }
    }

    for(i64 i = 0; i < n; ++i){
        i64 r = 0;
        for(i64 j = 0; j < 8; ++j)
            r = max(r, elm[ret[i]][j] - pos_[i][j]);
        cout << r << endl;
    }
}
accuracy: 1
time_ms: 680
memory_kb: 80204
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}
const int32 DIRECTED = 0;
const int32 UNDIRECTED = 1;

template<int32 isUNDIRECTED=0>
class Graph{
    struct Edge{
        int32 u, v, id;
        int64 c;
        Edge(int32 u, int32 v, int64 c=0, int32 id=0):u(u), v(v), c(c), id(id){}
    };

    int32 V, E;
    vector<vector<Edge>> G;
    vector<Edge> Es;
public:
    Graph(){}
    Graph(int32 V):V(V){G.resize(V);}
    Graph(const Graph<isUNDIRECTED>& g):V(g.V), E(g.E), G(g.G), Es(g.Es){}

    void add_edge(int32 u, int32 v, int64 c=0, int32 id=0){
        G[u].emplace_back(u, v, c, id);
        if(isUNDIRECTED) G[v].emplace_back(v, u, c, id);
        Es.emplace_back(u, v, c, id);
        E++;
    }

    const vector<Edge>& operator[](int32 k){
        return G[k];
    }

    int64 size() {
        return G.size();
    }
};

class SCC{
private:
    Graph<DIRECTED> orgG, revG, newG;
    vector<int32> ord, comp;
    vector<bool> used;
    vector<vector<int32>> vs;

    int32 V, nV;
public:
    SCC(){}
    SCC(int32 V):orgG(V), revG(V), comp(V, -1), used(V, 0), V(V){}

    void add_edge(int32 u, int32 v){
        orgG.add_edge(u, v);
        revG.add_edge(v, u);
    }

    void dfs(int32 v){
        used[v] = true;
        for(auto e : orgG[v]){
            if(!used[e.v]) dfs(e.v);
        }
        ord.push_back(v);
    }

    void rdfs(int32 v, int32 k){
        used[v] = true;
        comp[v] = k;
        for(auto e : revG[v]){
            if(!used[e.v]) rdfs(e.v, k);
        }
    }

    int32 build(){
        for(int32 i = 0;i < V;i++){
            if(!used[i]) dfs(i);
        }
        fill(used.begin(), used.end(), 0);
        int32 k = 0;
        for(int32 i = ord.size()-1;i >= 0;i--){
            if(!used[ord[i]]) rdfs(ord[i], k++);
        }
        nV = k;

        vs.resize(k, vector<int32>());
        for(int32 i = 0;i < V;i++)
            vs[comp[i]].push_back(i);

        newG = Graph<DIRECTED>(k);
        for(int32 i = 0;i < V;i++){
            for(auto e : orgG[i]){
                if(comp[i] != comp[e.v])
                    newG.add_edge(comp[i], comp[e.v], e.c);
            }
        }
        return k;
    }

    int32 size(){
        return nV;
    }
    int64 size(int64 v) {
        return vs[v].size();
    }

    const Graph<DIRECTED>& graph(){
        return newG;
    }

    const vector<int32>& vertices(int32 v){
        return vs[v];
    }

    int32 operator[](int32 k){
        return comp[k];
    }
};

vector<int64> x, y, z;
struct Data {
    PLL dat[8];
    Data() { REP(i, 8) { dat[i] = PLL(INF_LL, -1); }}
    void add(int64 idx) {
        REP(i, 8) {
            int64 nx = ((i & 1) ? -1 : 1) * x[idx];
            int64 ny = (((i >> 1) & 1) ? -1 : 1) * y[idx];
            int64 nz = (((i >> 2) & 1) ? -1 : 1) * z[idx];
            chmin(dat[i], PLL(nx+ny+nz, idx));
        }
    }

    vector<int> get() {
        vector<int> res;
        REP(i, 8) {
            if (dat[i].sc != -1)
                res.push_back(dat[i].sc);
        }
        return res;
    }

    void merge(const Data& rhs) {
        REP(i, 8) {
            chmin(dat[i], rhs.dat[i]);
        }
    }
};

int64 N, M;
SCC scc(N);
Graph<DIRECTED> G;
vector<int64> res;
vector<Data> D;
vector<int64> used;

int64 dist(int64 id1, int64 id2) {
    return abs(x[id1]-x[id2])+abs(y[id1]-y[id2])+abs(z[id1]-z[id2]);
}

int64 dfs(int64 v) {
    if (used[v]) return 0;
    used[v] = 1;
    PLL mx_sz = PLL(0, -1);
    int64 ret = scc.size(v);
    REP(i, G[v].size()) {
        int64 vv, sz;
        sz = dfs(G[v][i].v);
        vv = G[v][i].v;
        ret += sz;
        if (mx_sz.fs < sz) {
            mx_sz = PLL(sz, vv);
        }
    }
    if (mx_sz.sc != -1) {
        REP(i, G[v].size()) {
//            REP(j, 8) {
//                for (auto &x : D[G[v][i].v].dat[j]) {
//                    D[v].dat[j].insert(x);
//                }
//            }
            D[v].merge(D[G[v][i].v]);
        }
    }
    auto vs = scc.vertices(v);
    REP(i, vs.size()) {
        D[v].add(vs[i]);
    }
    vector<int> kouho = D[v].get();
    REP(i, vs.size()) {
        int64 vv = vs[i];
        REP(j, kouho.size()) {
            chmax(res[vv], dist(kouho[j], vv));
        }
    }

    return ret;
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N >> M;
	used.resize(N, 0);
	res.resize(N, 0);
	x.resize(N); y.resize(N); z.resize(N);
	REP(i, N) {
	    cin >> x[i] >> y[i] >> z[i];
	}
	scc = SCC(N);
	REP(i, M) {
	    int64 u, v;
	    cin >> u >> v; u--; v--;
	    scc.add_edge(u, v);
	}
	scc.build();

	G = scc.graph();
	vector<int64> indeg(scc.size(), 0);
	REP(i, scc.size()) {
	    REP(j, G[i].size()) {
	        indeg[G[i][j].v]++;
	    }
	}
	D.resize(scc.size());

	REP(i, scc.size()) {
//	        cout << i << endl;
//	        auto vv = scc.vertices(i);
//	        REP(j, vv.size()) {
//	            cout << vv[j] << " ";
//	        }
//	        cout << endl;
        dfs(i);
	}
	REP(i, N) {
	    cout << res[i] << endl;
	}
}
accuracy: 0.1956521739130435
time_ms: 250
memory_kb: 77276
final_rank: 20

code_snippet: #include <stack>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
#include <random>
#include <utility>
using namespace std;
constexpr int INF = 1e9;

typedef pair<int, int> P;

struct StronglyConnectedComponents {
    int N;
    vector<vector<int> > g, gr, g2i;
    vector<bool> visited;
    vector<int> i2g;
    vector<P> edges;
    stack<int> st;

    StronglyConnectedComponents(){};
    StronglyConnectedComponents(int n){init(n);}
    void init(int n){
        N = n;
        g.clear();
        g.resize(n);
        gr.clear();
        gr.resize(n);
        edges.clear();
        visited.resize(n);
        i2g.resize(n);
    }
    void add_edge(int u, int v) {
        g[u].push_back(v);
        gr[v].push_back(u);
        edges.push_back(P(u, v));
    }

    void dfs(int x) {
        if (visited[x]) return;
        visited[x] = true;
        for (int i : g[x]) dfs(i);
        st.push(x);
    }

    void rdfs(int x, int k) {
        if (i2g[x] != -1) return;
        i2g[x] = k;
        for (int i : gr[x]) rdfs(i, k);
    }

    void build(vector<vector<int> > &t) {
        fill(visited.begin(), visited.end(), false);
        fill(i2g.begin(), i2g.end(), -1);
        for (int i = 0; i < N; i++) dfs(i);
        int p = 0;
        while (!st.empty()) {
            int v = st.top();
            st.pop();
            if (i2g[v] == -1) {
                rdfs(v, p);
                p++;
            }
        }
        g2i.clear();
        g2i.resize(p);
        t.resize(p);
        for(int i=0;i<N;i++){
            g2i[i2g[i]].push_back(i);
        }
        set<P> connected;
        for (auto &e : edges) {
            int x = i2g[e.first], y = i2g[e.second];
            if (x == y) continue;
            if (connected.count({x, y})) continue;
            t[x].push_back(y);
            connected.insert({x, y});
        }
    }
};

void dfs(const vector<vector<int>> &G, StronglyConnectedComponents &scc, int v, const vector<int> &W, vector<int> &ma){
  for(auto v_ : scc.g2i[v]){
    ma[v] = max(ma[v],W[v_]);
  }
  for(auto v_ : G[v]){
    if(ma[v_] == -INF){
      dfs(G,scc,v_,W,ma);
    }
    ma[v] = max(ma[v],ma[v_]);
  }
}

vector<int> solve2(const vector<vector<int>> &G, const vector<vector<int>> &G_, StronglyConnectedComponents &scc, const vector<int> &W){
  int M = G_.size();
  vector<int> ma(M,-INF);
  for(int i = 0; i < M; ++i){
    if(ma[i] <= -INF)
      dfs(G_,scc,i,W,ma);
  }
  int N = G.size();
  vector<int> A(N);
  for(int i = 0; i < N; ++i){
    int vv = scc.i2g[i];
    A[i] = ma[vv] - W[i];
  }
  return A;
}

vector<int> solve(const vector<vector<int>> &G, const vector<int> &X, const vector<int> &Y, const vector<int> &Z){
  int N = G.size();
  StronglyConnectedComponents scc(G.size());
  for(int i = 0; i < N; ++i){
    for(auto v : G[i]){
      scc.add_edge(i,v);
    }
  }
  vector<vector<int>> G_;
  scc.build(G_);
  int M = scc.g2i.size();
  vector<int> ans(N,-INF);
  for(int i = -1; i < 2; i += 2){
    for(int j = -1; j < 2; j += 2){
      for(int k = -1; k < 2; k += 2){
        vector<int> W(N), visited(N), ma(N,-INF);
        for(int l = 0; l < N; ++l){
          W[l] = i*X[l] + j*Y[l] + k*Z[l];
        }
        vector<int> A = solve2(G,G_,scc,W);
        for(int l = 0; l < N; ++l){
          ans[l] = max(ans[l],A[l]);
        }
      }
    }
  }
  return ans;
}

void dfs_naive(const vector<vector<int>> &G, const int v, set<int> &s){
  s.insert(v);
  for(auto v_ : G[v]){
    if(s.count(v_)) continue;
    dfs_naive(G,v_,s);
  }
}

vector<int> solve_naive(const vector<vector<int>> &G, const vector<int>& X, const vector<int>& Y, const vector<int> &Z){
  int N = G.size();
  vector<int> ret(N);
  for(int i = 0; i < N; ++i){
    set<int> s;
    dfs_naive(G,i,s);
    for(auto v : s){
      ret[i] = max(ret[i],abs(X[i]-X[v])+abs(Y[i]-Y[v])+abs(Z[i]-Z[v]));
    }
  }
  return ret;
}

void test(int N, int M){
  //int N = 4, M = 4;
  random_device rnd;
  vector<int> X(N), Y(N), Z(N);
  for(int i = 0; i < N; ++i){
    X[i] = rnd()%10;
    Y[i] = rnd()%10;
    Z[i] = rnd()%10;
  }
  set<pair<int,int>> S;
  vector<vector<int>> G(N);
  for(int i = 0; i < M; ++i){
    int u = rnd()%N, v = rnd()%N;
    while(u == v or S.count({u,v})){
      u = rnd()%N;
      v = rnd()%N;
    }
    G[u].push_back(v);
    S.insert({u,v});
  }
  vector<int> A = solve(G,X,Y,Z);
  return;
  vector<int> B = solve_naive(G,X,Y,Z);
  bool f = true;
  for(int i = 0; i < N; ++i){
    if(A[i] != B[i]){
      f = false;
      break;
    }
  }
  if(!f){
    cout << "Wrong Answer" << endl;
    cout << "solve : solve_naive" << endl;
    for(int i = 0; i < N; ++i){
      cout << A[i] << " " << B[i] << endl;
    }
    cout << "Test Case" << endl;
    cout << N << " " << M << endl;
    for(int i = 0; i < N; ++i)
      cout << X[i] << " " << Y[i] << " " << Z[i] << endl;
    for(int i = 0; i < N; ++i){
      for(auto v : G[i])
        cout << i+1 << " " << v+1 << endl;
    }
  }
}

int main(){
  int N, M;
  cin >> N >> M;
  // test(N,M);
  // return 0;
  vector<int> X(N), Y(N), Z(N);
  for(int i = 0; i < N; ++i){
    cin >> X[i] >> Y[i] >> Z[i];
  }
  vector<vector<int>> G(N);
  for(int i = 0; i < M; ++i){
    int u, v;
    cin >> u >> v;
    --u,--v;
    G[u].push_back(v);
  }
  vector<int> A = solve(G,X,Y,Z);
  for(auto a : A)
    cout << a << endl;
}
accuracy: 1
time_ms: 880
memory_kb: 91060
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}
const int32 DIRECTED = 0;
const int32 UNDIRECTED = 1;

template<int32 isUNDIRECTED=0>
class Graph{
    struct Edge{
        int32 u, v, id;
        int64 c;
        Edge(int32 u, int32 v, int64 c=0, int32 id=0):u(u), v(v), c(c), id(id){}
    };

    int32 V, E;
    vector<vector<Edge>> G;
    vector<Edge> Es;
public:
    Graph(){}
    Graph(int32 V):V(V){G.resize(V);}
    Graph(const Graph<isUNDIRECTED>& g):V(g.V), E(g.E), G(g.G), Es(g.Es){}

    void add_edge(int32 u, int32 v, int64 c=0, int32 id=0){
        G[u].emplace_back(u, v, c, id);
        if(isUNDIRECTED) G[v].emplace_back(v, u, c, id);
        Es.emplace_back(u, v, c, id);
        E++;
    }

    const vector<Edge>& operator[](int32 k){
        return G[k];
    }

    int64 size() {
        return G.size();
    }
};

class SCC{
private:
    Graph<DIRECTED> orgG, revG, newG;
    vector<int32> ord, comp;
    vector<bool> used;
    vector<vector<int32>> vs;

    int32 V, nV;
public:
    SCC(){}
    SCC(int32 V):orgG(V), revG(V), comp(V, -1), used(V, 0), V(V){}

    void add_edge(int32 u, int32 v){
        orgG.add_edge(u, v);
        revG.add_edge(v, u);
    }

    void dfs(int32 v){
        used[v] = true;
        for(auto e : orgG[v]){
            if(!used[e.v]) dfs(e.v);
        }
        ord.push_back(v);
    }

    void rdfs(int32 v, int32 k){
        used[v] = true;
        comp[v] = k;
        for(auto e : revG[v]){
            if(!used[e.v]) rdfs(e.v, k);
        }
    }

    int32 build(){
        for(int32 i = 0;i < V;i++){
            if(!used[i]) dfs(i);
        }
        fill(used.begin(), used.end(), 0);
        int32 k = 0;
        for(int32 i = ord.size()-1;i >= 0;i--){
            if(!used[ord[i]]) rdfs(ord[i], k++);
        }
        nV = k;

        vs.resize(k, vector<int32>());
        for(int32 i = 0;i < V;i++)
            vs[comp[i]].push_back(i);

        newG = Graph<DIRECTED>(k);
        for(int32 i = 0;i < V;i++){
            for(auto e : orgG[i]){
                if(comp[i] != comp[e.v])
                    newG.add_edge(comp[i], comp[e.v], e.c);
            }
        }
        return k;
    }

    int32 size(){
        return nV;
    }
    int64 size(int64 v) {
        return vs[v].size();
    }

    const Graph<DIRECTED>& graph(){
        return newG;
    }

    const vector<int32>& vertices(int32 v){
        return vs[v];
    }

    int32 operator[](int32 k){
        return comp[k];
    }
};

vector<int64> x, y, z;
struct Data {
    PLL dat[8];
    Data() { REP(i, 8) { dat[i] = PLL(INF_LL, -1); }}
    void add(int64 idx) {
        REP(i, 8) {
            int64 nx = ((i & 1) ? -1 : 1) * x[idx];
            int64 ny = (((i >> 1) & 1) ? -1 : 1) * y[idx];
            int64 nz = (((i >> 2) & 1) ? -1 : 1) * z[idx];
            chmin(dat[i], PLL(nx+ny+nz, idx));
        }
    }

    vector<int> get() {
        vector<int> res;
        REP(i, 8) {
            if (dat[i].sc != -1)
                res.push_back(dat[i].sc);
        }
        return res;
    }

    void merge(const Data& rhs) {
        REP(i, 8) {
            chmin(dat[i], rhs.dat[i]);
        }
    }
};

int64 N, M;
SCC scc(N);
Graph<DIRECTED> G;
vector<int64> res;
vector<Data> D;
vector<int64> used;

int64 dist(int64 id1, int64 id2) {
    return abs(x[id1]-x[id2])+abs(y[id1]-y[id2])+abs(z[id1]-z[id2]);
}

int64 dfs(int64 v) {
    if (used[v]) return 0;
    used[v] = 1;
    PLL mx_sz = PLL(0, -1);
    int64 ret = scc.size(v);
    REP(i, G[v].size()) {
        int64 vv, sz;
        sz = dfs(G[v][i].v);
        vv = G[v][i].v;
        ret += sz;
        if (mx_sz.fs < sz) {
            mx_sz = PLL(sz, vv);
        }
    }
    if (mx_sz.sc != -1) {
        REP(i, G[v].size()) {
//            REP(j, 8) {
//                for (auto &x : D[G[v][i].v].dat[j]) {
//                    D[v].dat[j].insert(x);
//                }
//            }
            D[v].merge(D[G[v][i].v]);
        }
    }
    auto vs = scc.vertices(v);
    REP(i, vs.size()) {
        D[v].add(vs[i]);
    }
    vector<int> kouho = D[v].get();
    REP(i, vs.size()) {
        int64 vv = vs[i];
        REP(j, kouho.size()) {
            chmax(res[vv], dist(kouho[j], vv));
        }
    }

    return ret;
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N >> M;
	used.resize(N, 0);
	res.resize(N, 0);
	x.resize(N); y.resize(N); z.resize(N);
	REP(i, N) {
	    cin >> x[i] >> y[i] >> z[i];
	}
	scc = SCC(N);
	REP(i, M) {
	    int64 u, v;
	    cin >> u >> v; u--; v--;
	    scc.add_edge(u, v);
	}
	scc.build();

	G = scc.graph();
	vector<int64> indeg(scc.size(), 0);
	REP(i, scc.size()) {
	    REP(j, G[i].size()) {
	        indeg[G[i][j].v]++;
	    }
	}
	D.resize(scc.size());

	REP(i, scc.size()) {
	    if (indeg[i] == 0) {
//	        cout << i << endl;
//	        auto vv = scc.vertices(i);
//	        REP(j, vv.size()) {
//	            cout << vv[j] << " ";
//	        }
//	        cout << endl;
            dfs(i);
	    }
	}
	REP(i, N) {
	    cout << res[i] << endl;
	}
}
accuracy: 0.1956521739130435
time_ms: 240
memory_kb: 77136
final_rank: 19

code_snippet: #include "bits/stdc++.h"
#define YES "YES"
#define NO "NO"
#define YESNO OUT(three(solve(),YES,NO))
#define ECHO OUT(solve())
#define three(A,B,C) ((A)?(B):(C))
#define FOR(i,a,b)  for(LL i=(a);i< (LL)(b);i++)
#define EFOR(i,a,b) for(LL i=(a);i<=(LL)(b);i++)
#define RFOR(i,a,b) for(LL i=(b);i>=(LL)(a);i--)
#define REP(i,b) FOR(i,zero,b)
#define rep REP
#define EREP(i,b) EFOR(i,zero,b)
#define RREP(i,b) RFOR(i,b,zero)
#define ALL(c) c.begin(),c.end()
#define UNIQUE(c) sort(ALL(c));c.erase(unique(ALL(c)),c.end())
#define MAX(c) (*max_element(ALL(c)))
#define MIN(c) (*min_element(ALL(c)))
#define MP make_pair
#define FI first
#define SE second
#define SI(x) (LL(x.size()))
#define PB push_back
#define DEBUG(a) OUT(a)
#define DEBUG2(a,b) OUT2(a,b)
#define cat cout << __LINE__ << endl
#define OUT(a) cout << (a) << endl
#define OUT2(a,b) cout << (a) <<" "<<(b) << endl
#define zero 0LL
#define all ALL
#define pb emplace_back
#define eb pb
#define int long long
using namespace std;
template<typename T> inline void maximize(T &a, T b) { a = max(a, b); }
template<typename T> inline void minimize(T &a, T b) { a = min(a, b); }
template<typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; }
template<class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; }
template<class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; }
typedef int LL;
typedef double ld;
typedef int ut;
typedef vector<ut> VI;
typedef vector<VI> VII;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef tuple<int, int, int, int> tapu;
typedef vector<tapu> Vtapu;
typedef priority_queue<tapu, Vtapu, greater<tapu> > PQ;
inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; }OUT(""); }
const int SIZE1 = 3e5 + 1000;
const int SIZE2 = 5010;
const int SIZE3 = 430;
const int SIZE = SIZE1;
const int MAPSIZE = 40;
const LL p = 7 + 1e9;
const LL INF = 1LL << 60;
const long double EPS = 1e-7;
typedef pair<ld, ut> pld;
template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;
template< typename G >
struct StronglyConnectedComponents {
  const G &g;
  UnWeightedGraph gg, rg;
  vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
    for(int i = 0; i < g.size(); i++) {
      for(auto e : g[i]) {
        gg[i].emplace_back((int) e);
        rg[(int) e].emplace_back(i);
      }
    }
  }

  int operator[](int k) {
    return comp[k];
  }

  void dfs(int idx) {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(UnWeightedGraph &t) {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    for(int i = 0; i < g.size(); i++) {
      for(auto &to : g[i]) {
        int x = comp[i], y = comp[to];
        if(x == y) continue;
        t[x].push_back(y);
      }
    }
  }
};
bool finished[SIZE];
LL X[SIZE],Y[SIZE],Z[SIZE];
LL maximum[SIZE][2][2][2];
UnWeightedGraph buff;
LL N,M;
void solve(LL x){
  if(finished[x]) return ;
  for(auto next:buff[x]){
    solve(next);
    REP(i,2) REP(j,2) REP(k,2) MX(maximum[x][i][j][k],maximum[next][i][j][k]);
  }
  finished[x]=true;
}
signed main(){
  REP(i,SIZE) REP(j,2) REP(k,2) REP(l,2) maximum[i][j][k][l]=-INF;
  cin >> N >> M;
  REP(i,N){
    cin >> X[i] >> Y[i] >> Z[i];
  }

  UnWeightedGraph g(N);
  for(int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
  }
  StronglyConnectedComponents< UnWeightedGraph > scc(g);
  scc.build(buff);
  REP(cc,N){
    REP(i,2) REP(j,2) REP(k,2){
      LL ans=0;
      ans+=i?X[cc]:-X[cc];
      ans+=j?Y[cc]:-Y[cc];
      ans+=k?Z[cc]:-Z[cc];
      MX(maximum[scc[cc]][i][j][k],ans);
    }
  }
  REP(i,N){
    solve(scc[i]);
  }
  REP(cc,N){
  LL ans2=0;
    REP(i,2) REP(j,2) REP(k,2){
      LL ans=0;
      ans+=i?X[cc]:-X[cc];
      ans+=j?Y[cc]:-Y[cc];
      ans+=k?Z[cc]:-Z[cc];
      MX(ans2,abs(ans-maximum[scc[cc]][i][j][k]));
    }
    cout <<ans2 << endl;
  }
//  cout <<ans2 << endl;
  return 0;
}
accuracy: 1
time_ms: 680
memory_kb: 85528
final_rank: 12

code_snippet: //#define NDEBUG
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>

namespace n91 {

  using i8 = std::int_fast8_t;
  using i32 = std::int_fast32_t;
  using i64 = std::int_fast64_t;
  using u8 = std::uint_fast8_t;
  using u32 = std::uint_fast32_t;
  using u64 = std::uint_fast64_t;
  using isize = std::ptrdiff_t;
  using usize = std::size_t;

  constexpr usize operator"" _z(unsigned long long x) noexcept {
    return static_cast<usize>(x);
  }

  template <class T> class integral_iterator {
  public:
    using difference_type = T;
    using value_type = T;
    using pointer = const value_type*;
    using reference = value_type;
    using iterator_category = std::random_access_iterator_tag;

  private:
    using self_type = integral_iterator<value_type>;
    value_type i;

  public:
    constexpr integral_iterator() noexcept : i() {}
    explicit constexpr integral_iterator(const value_type i) noexcept : i(i) {}
    constexpr self_type operator++(int) noexcept { return self_type(i++); }
    constexpr self_type operator--(int) noexcept { return self_type(i--); }
    constexpr self_type operator[](const difference_type rhs) const noexcept {
      return self_type(i + rhs);
    }
    constexpr self_type& operator++() noexcept {
      ++i;
      return *this;
    }
    constexpr self_type& operator--() noexcept {
      --i;
      return *this;
    }
    constexpr reference operator*() const noexcept { return i; }
    constexpr self_type operator+(const difference_type rhs) const noexcept {
      return self_type(i + rhs);
    }
    constexpr self_type operator-(const difference_type rhs) const noexcept {
      return self_type(i - rhs);
    }
    constexpr difference_type operator-(const self_type rhs) const noexcept {
      return i - rhs.i;
    }
    constexpr bool operator<(const self_type rhs) const noexcept {
      return i < rhs.i;
    }
    constexpr bool operator<=(const self_type rhs) const noexcept {
      return i <= rhs.i;
    }
    constexpr bool operator>(const self_type rhs) const noexcept {
      return i > rhs.i;
    }
    constexpr bool operator>=(const self_type rhs) const noexcept {
      return i >= rhs.i;
    }
    constexpr bool operator==(const self_type rhs) const noexcept {
      return i == rhs.i;
    }
    constexpr bool operator!=(const self_type rhs) const noexcept {
      return i != rhs.i;
    }
    constexpr self_type& operator+=(const difference_type rhs) noexcept {
      i += rhs;
      return *this;
    }
    constexpr self_type& operator-=(const difference_type rhs) noexcept {
      i -= rhs;
      return *this;
    }
  };
  template <class T>
  constexpr integral_iterator<T> make_int_itr(const T i) noexcept {
    return integral_iterator<T>(i);
  }
  class rep {
    const usize f, l;

  public:
    constexpr rep(const usize f, const usize l) noexcept : f(f), l(l) {}
    constexpr auto begin() const noexcept { return make_int_itr(f); }
    constexpr auto end() const noexcept { return make_int_itr(l); }
  };
  class revrep {
    const usize f, l;

  public:
    revrep(const usize f, const usize l) noexcept : f(l), l(f) {}
    auto begin() const noexcept {
      return std::make_reverse_iterator(make_int_itr(f));
    }
    auto end() const noexcept {
      return std::make_reverse_iterator(make_int_itr(l));
    }
  };
  template <class T> auto md_vec(const usize n, const T& value) {
    return std::vector<T>(n, value);
  }
  template <class... Args> auto md_vec(const usize n, Args... args) {
    return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
  }
  template <class T> constexpr T difference(const T& a, const T& b) {
    return a < b ? b - a : a - b;
  }
  template <class T> T scan() {
    T ret;
    std::cin >> ret;
    return ret;
  }

} // namespace n91

#include <cstdint>

namespace n91 {

  constexpr std::uint_fast64_t totient(std::uint_fast64_t x) noexcept {
    using u64 = std::uint_fast64_t;
    u64 ret = x;
    for (u64 i = static_cast<u64>(2); i * i <= x; ++i) {
      if (x % i == static_cast<u64>(0)) {
        ret -= ret / i;
        x /= i;
        while (x % i == static_cast<u64>(0)) {
          x /= i;
        }
      }
    }
    if (x != static_cast<u64>(1)) {
      ret -= ret / x;
    }
    return ret;
  }

  template <std::uint_fast64_t Modulus,
    std::uint_fast64_t InverseExp =
    totient(Modulus) - static_cast<std::uint_fast64_t>(1)>
    class modint {
    using u64 = std::uint_fast64_t;

    static_assert(Modulus < static_cast<u64>(1) << static_cast<u64>(32),
      "Modulus must be less than 2**32");

    u64 a;

    constexpr modint& negate() noexcept {
      if (a != static_cast<u64>(0)) {
        a = Modulus - a;
      }
      return *this;
    }

    public:
      constexpr modint(const u64 x = static_cast<u64>(0)) noexcept
        : a(x% Modulus) {}
      constexpr u64& value() noexcept { return a; }
      constexpr const u64& value() const noexcept { return a; }
      constexpr modint operator+() const noexcept { return modint(*this); }
      constexpr modint operator-() const noexcept { return modint(*this).negate(); }
      constexpr modint operator+(const modint rhs) const noexcept {
        return modint(*this) += rhs;
      }
      constexpr modint operator-(const modint rhs) const noexcept {
        return modint(*this) -= rhs;
      }
      constexpr modint operator*(const modint rhs) const noexcept {
        return modint(*this) *= rhs;
      }
      constexpr modint operator/(const modint rhs) const noexcept {
        return modint(*this) /= rhs;
      }
      constexpr modint& operator+=(const modint rhs) noexcept {
        a += rhs.a;
        if (a >= Modulus) {
          a -= Modulus;
        }
        return *this;
      }
      constexpr modint& operator-=(const modint rhs) noexcept {
        if (a < rhs.a) {
          a += Modulus;
        }
        a -= rhs.a;
        return *this;
      }
      constexpr modint& operator*=(const modint rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
      }
      constexpr modint& operator/=(modint rhs) noexcept {
        u64 exp = InverseExp;
        while (exp) {
          if (exp % static_cast<u64>(2) != static_cast<u64>(0)) {
            *this *= rhs;
          }
          rhs *= rhs;
          exp /= static_cast<u64>(2);
        }
        return *this;
      }
      constexpr bool operator==(const modint rhs) const noexcept {
        return a == rhs.a;
      }
      constexpr bool operator!=(const modint rhs) const noexcept {
        return a != rhs.a;
      }
  };

  template <class T, std::uint_fast64_t v> class modint_constant {
  public:
    static constexpr T value = static_cast<T>(v);

    using value_type = T;
  };

} // namespace n91

#include <functional>
#include <utility>

namespace n91 {

  template <class T, class U, class Operate = std::multiplies<T>>
  constexpr T power(T base, U exp, const Operate & oper = Operate(),
    T iden = static_cast<T>(1)) {
    while (exp != static_cast<U>(0)) {
      if (exp % static_cast<U>(2) != static_cast<U>(0)) {
        iden = oper(iden, base);
      }
      exp /= static_cast<U>(2);
      base = oper(base, base);
    }
    return iden;
  }

} // namespace n91

#include <vector>

namespace n91 {

  template <class T> class fact_binom {
  public:
    using value_type = T;
    using container_type = std::vector<value_type>;
    using size_type = typename container_type::size_type;

  private:
    container_type factrial, inv_fact;

  public:
    fact_binom() : factrial(), inv_fact() {}
    explicit fact_binom(const size_type n) : factrial(n + 1), inv_fact(n + 1) {
      factrial[0] = static_cast<value_type>(1);
      for (size_type i = 0; i != n; ++i) {
        factrial[i + 1] = static_cast<value_type>(i + 1) * factrial[i];
      }
      inv_fact[n] = static_cast<value_type>(1) / factrial[n];
      for (size_type i = n; i != 0; --i) {
        inv_fact[i - 1] = inv_fact[i] * static_cast<value_type>(i);
      }
    }

    value_type operator()(const size_type n, const size_type r) const {
      return factrial[n] * inv_fact[r] * inv_fact[n - r];
    }
  };

} // namespace n91


#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <utility>
#include<array>

namespace std {

  template <typename T> struct edge {
    int src, to;
    T cost;

    edge(int to, T cost) : src(-1), to(to), cost(cost) {}

    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

    edge& operator=(const int& x) {
      to = x;
      return *this;
    }

    operator int() const { return to; }
  };

  template <typename T> using Edges = vector<edge<T>>;
  template <typename T> using WeightedGraph = vector<Edges<T>>;
  using UnWeightedGraph = vector<vector<int>>;
  template <typename T> using Matrix = vector<vector<T>>;

  template <typename G> struct StronglyConnectedComponents {
    const G& g;
    UnWeightedGraph gg, rg;
    vector<int> comp, order, used;

    StronglyConnectedComponents(G& g)
      : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
      for (int i = 0; i < g.size(); i++) {
        for (auto e : g[i]) {
          gg[i].emplace_back((int)e);
          rg[(int)e].emplace_back(i);
        }
      }
    }

    int operator[](int k) { return comp[k]; }

    void dfs(int idx) {
      if (used[idx])
        return;
      used[idx] = true;
      for (int to : gg[idx])
        dfs(to);
      order.push_back(idx);
    }

    void rdfs(int idx, int cnt) {
      if (comp[idx] != -1)
        return;
      comp[idx] = cnt;
      for (int to : rg[idx])
        rdfs(to, cnt);
    }

    void build(UnWeightedGraph& t) {
      for (int i = 0; i < gg.size(); i++)
        dfs(i);
      reverse(begin(order), end(order));
      int ptr = 0;
      for (int i : order)
        if (comp[i] == -1)
          rdfs(i, ptr), ptr++;

      t.resize(ptr);
      for (int i = 0; i < g.size(); i++) {
        for (auto& to : g[i]) {
          int x = comp[i], y = comp[to];
          if (x == y)
            continue;
          t[x].push_back(y);
        }
      }
    }
  };

} // namespace std


namespace n91 {

  void main_() {
    const usize n = scan<usize>();
    const usize m = scan<usize>();
    struct planet {
      i64 x, y, z;
      i64 calc(usize i) const {
        return x * (i & 1 ? -1 : 1) + y * (i & 2 ? -1 : 1) + z * (i & 4 ? -1 : 1);
      }
      i64 dist(const planet& r) const {
        return std::abs(x - r.x) + std::abs(y - r.y) + std::abs(z - r.z);
      }
    };
    struct node {
      std::array<planet, 8> ps;
      void add(const node& r) {
        for (const auto i : rep(0_z, 8_z)) {
          if (ps[i].calc(i) < r.ps[i].calc(i)) {
            ps[i] = r.ps[i];
          }
        }
      }
      i64 dist(const planet& r) const {
        i64 ans = 0;
        for (const auto& e : ps) {
          ans = std::max(ans, r.dist(e));
        }
        return ans;
      }
      node() {}
      node(const planet& r) {
        for (auto& e : ps) {
          e = r;
        }
      }
    };
    std::vector<planet> p(n);
    for (auto& e : p) {
      std::cin >> e.x >> e.y >> e.z;
    }
    std::UnWeightedGraph g(n), buf;
    for (const auto i : rep(0_z, m)) {
      const usize u = scan<usize>() - 1_z;
      const usize v = scan<usize>() - 1_z;
      g[u].push_back(v);
    }
    std::StronglyConnectedComponents<std::UnWeightedGraph> scc(g);
    scc.build(buf);
    const usize k = buf.size();
    std::vector<std::vector<usize>> cs(k);
    for (const auto i : rep(0_z, n)) {
      cs[scc[i]].push_back(i);
    }
    std::vector<node> dp(k);
    std::vector<i64> ans(n);
    for (const auto ci : revrep(0_z, k)) {
      dp[ci] = node(p[cs[ci][0]]);
      for (const auto i : cs[ci]) {
        dp[ci].add(node(p[i]));
      }
      std::set<usize> set(buf[ci].begin(), buf[ci].end());
      for (const auto i : set) {
        dp[ci].add(dp[i]);
      }
      for (const auto i : cs[ci]) {
        ans[i] = dp[ci].dist(p[i]);
      }
    }
    for (const auto e : ans) {
      std::cout << e << std::endl;
    }
  }

} // namespace n91

int main() {
  n91::main_();
  return 0;
}
accuracy: 1
time_ms: 680
memory_kb: 105952
final_rank: 14

code_snippet: // う？笑
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename G >
struct StronglyConnectedComponents {
  const G &g;
  UnWeightedGraph gg, rg;
  vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
    for(int i = 0; i < g.size(); i++) {
      for(auto e : g[i]) {
        gg[i].emplace_back((int) e);
        rg[(int) e].emplace_back(i);
      }
    }
  }

  int operator[](int k) {
    return comp[k];
  }

  void dfs(int idx) {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(UnWeightedGraph &t) {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    for(int i = 0; i < g.size(); i++) {
      for(auto &to : g[i]) {
        int x = comp[i], y = comp[to];
        if(x == y) continue;
        t[x].push_back(y);
      }
    }
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  auto X = make_v< int >(N, 3);
  cin >> X;
  UnWeightedGraph g(N);
  for(int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].emplace_back(b);
  }
  StronglyConnectedComponents< UnWeightedGraph > scc(g);
  UnWeightedGraph t;
  scc.build(t);
  auto small = make_v< int >(t.size(), 1 << 3);
  fill_v(small, inf);
  auto large = make_v< int >(t.size(), 1 << 3);
  fill_v(large, -inf);


  for(int i = 0; i < N; i++) {
    for(int j = 0; j < 1 << 3; j++) {
      int add = 0;
      for(int k = 0; k < 3; k++) {
        if((j >> k) & 1) add += X[i][k];
        else add -= X[i][k];
      }
      chmin(small[scc[i]][j], add);
      chmax(large[scc[i]][j], add);
    }
  }
  auto latte = make_v< int >(t.size(), 1 << 3);
  fill_v(latte, inf);

  auto malta = make_v< int >(t.size(), 1 << 3);
  fill_v(malta, inf);

  auto rec = MFP([&](auto rec, int idx) -> void {
    if(latte[idx][0] != inf) return;
    latte[idx] = small[idx];
    malta[idx] = large[idx];
    for(auto to : t[idx]) {
      rec(to);
      for(int j = 0; j < (1 << 3); j++) chmin(latte[idx][j], latte[to][j]);
      for(int j = 0; j < (1 << 3); j++) chmax(malta[idx][j], malta[to][j]);
    }
  });

  for(int i = 0; i < N; i++) {
    int tap = 0;
    rec(scc[i]);

    for(int j = 0; j < (1 << 3); j++) {
      int add = 0;
      for(int k = 0; k < 3; k++) {
        if((j >> k) & 1) add -= X[i][k];
        else add += X[i][k];
      }
      chmax(tap, add + latte[scc[i]][j]);
      chmax(tap, malta[scc[i]][j] + add);
    }
    cout << tap << endl;
  }
}
accuracy: 1
time_ms: 510
memory_kb: 124764
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#define whlie while
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define rep(i,N) for(int i = 0; i < (N); i++)
#define repr(i,N) for(int i = (N) - 1; i >= 0; i--)
#define rep1(i,N) for(int i = 1; i <= (N) ; i++)
#define repr1(i,N) for(int i = (N) ; i > 0 ; i--)
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define vrep(v,it) for(auto it = v.begin(); it != v.end(); it++)
#define vrepr(v,it) for(auto it = v.rbegin(); it != v.rend(); it++)
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
using namespace std; void solve();
using ll = long long; using vl = vector<ll>;
using vi = vector<int>; using vvi = vector< vector<int> >;
constexpr int inf = 1001001001;
constexpr ll infLL = (1LL << 61) - 1;
struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya;
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; }
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#ifdef NyaanDebug
  #include "NyaanDebug.h"
  #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0)
  #define trca(v,...) do { cerr << #v << " = "; array_out(v , __VA_ARGS__ );} while(0)
#else
  #define trc(...)
  #define trca(...)
  int main(){solve();}
#endif

constexpr int MOD = /** 1000000007; //*/ 998244353;
/////////////////

template< typename G >
struct StronglyConnectedComponents {
  using UnWeightedGraph = vvi;
  const G &g;
  UnWeightedGraph gg, rg;
  vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
    for(int i = 0; i < (int)g.size(); i++) {
      for(auto e : g[i]) {
        gg[i].emplace_back((int) e);
        rg[(int) e].emplace_back(i);
      }
    }
  }

  int operator[](int k) {
    return comp[k];
  }

  void dfs(int idx) {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(UnWeightedGraph &t) {
    for(int i = 0; i < (int)gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    for(int i = 0; i < (int)g.size(); i++) {
      for(auto &to : g[i]) {
        int x = comp[i], y = comp[to];
        if(x == y) continue;
        t[x].push_back(y);
      }
    }
  }
};

using P = pair<int,int>;
using vp= vector<P>;

vp data(int x,int y,int z){
  vp ret = {
    P(x+y+z , x+y+z) ,
    P(x+y-z , x+y-z) ,
    P(x-y+z , x-y+z) ,
    P(x-y-z , x-y-z)
  };
  return ret;
}

vp dataini(){
  vp ret;
  rep(i,4) ret.eb(-inf , inf);
  return ret;
}

ll evaldata(vp data , int x,int y,int z){
  ll ret = 0;
  amax(ret, abs(x+y+z - data[0].fi));
  amax(ret, abs(x+y+z - data[0].se));
  amax(ret, abs(x+y-z - data[1].fi));
  amax(ret, abs(x+y-z - data[1].se));
  amax(ret, abs(x-y+z - data[2].fi));
  amax(ret, abs(x-y+z - data[2].se));
  amax(ret, abs(x-y-z - data[3].fi));
  amax(ret, abs(x-y-z - data[3].se));
  return ret;
}

void merge(vp &cur , vp &add){
  rep(i,4){
    cur[i].fi = max(cur[i].fi , add[i].fi );
    cur[i].se = min(cur[i].se , add[i].se );
  }
}

void solve(){
  int N,M; in(N , M);
  vi x(N) , y(N) , z(N);
  rep(i,N) in(x[i] , y[i] , z[i]);
  vvi g(N);
  rep(i,M){
    ini(x,y); x--; y--;
    g[x].pb(y);
  }
  StronglyConnectedComponents<vvi> scc(g);
  vvi t;
  scc.build(t);
  int T = sz(t);
  vector<vp>memo(T);
  rep(i,T){ memo[i] = dataini();}
  rep(i,N){
    vp cur = data(x[i],y[i],z[i]);
    merge( memo[scc[i]] , cur );
  }
  

  vvi inv(T);
  rep(i,T) each(x , t[i]) inv[x].pb(i);
  repr(i , T){
    each(x , inv[i]) merge(memo[x] , memo[i]);
  }
  rep(i,T) trc(memo[i]);
  vl ans(T , 0);

  rep(i,N) out( evaldata(memo[scc[i]] , x[i],y[i],z[i]) );

  

}
accuracy: 1
time_ms: 310
memory_kb: 80608
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
ll inf = 1e18;

//1-indexed!
class strong_components{
private:
    vector<vector<int>> v,rv,nv,cmp_node;
    vector<int> rs,visited,cmp,cmp_size;
    int num_cmp;
    void dfs(int n){
        visited[n] = 1;
        for(auto x:v[n]) if(!visited[x]) dfs(x);
        rs.push_back(n);
    }
    void rdfs(int n,int cnt){
        visited[n] = 1;
        cmp[n] = cnt;
        for(auto x:rv[n]) if(!visited[x]) rdfs(x,cnt);
    }
public:
    strong_components(int N,vector<vector<int>>& graph){
        v = graph;
        rv = cmp_node = vector<vector<int>>(N+1);
        visited = cmp = cmp_size = vector<int>(N+1,0);
        for(int i=1;i<=N;i++) for(auto x:v[i]) rv[x].push_back(i);
        for(int i=1;i<=N;i++) if(!visited[i]) dfs(i);
        for(int i=1;i<=N;i++) visited[i] = 0;
        int now = 1;
        for(int i=rs.size()-1;i>=0;i--) if(!visited[rs[i]]) rdfs(rs[i],now++);
        nv = vector<vector<int>>(now+1);
        for(int i=1;i<=N;i++){
            cmp_node[cmp[i]].push_back(i);
            for(auto x:v[i]){
                if(cmp[i]!=cmp[x]){
                    nv[cmp[x]].push_back(cmp[i]);
                    cmp_size[cmp[x]]++;
                }
            }
        }
        num_cmp = now-1;
    }
    int find(int n){return cmp[n];}
    vector<int> get_cmp_node(int n){return cmp_node[n];}
    vector<vector<int>> get_cmp_graph(){return nv;}
    int get_num_cmp() {return num_cmp;}
    bool is_same_group(int a,int b){return cmp[a]==cmp[b];}
};

int dx[8] = {1,-1,1,-1,1,1,-1-1},dy[8] = {1,1,-1,-1,1,-1,1,-1},dz[8] = {1,1,1,1,-1,-1,-1,-1};

int N,M;
vector<ll> X(200010),Y(200010),Z(200010);
vector<ll> ans(200010);
vector<vector<int>> graph(200010);
vector<vector<int>> cmp_graph(200010);
vector<vector<int>> cmp_node(200010);
vector<vector<int>> inv_graph(200010);

ll ma[200010][8] = {};
ll mi[200010][8] = {};
ll maid[200010][8] = {};
ll miid[200010][8] = {};


//全体管理
int main(){
    cin >> N >> M;
    for(int i=1;i<=N;i++){
        cin >> X[i] >> Y[i] >> Z[i];
    }
    for(int i=1;i<=M;i++){
        int a,b;
        cin >> a >> b;
        graph[a].push_back(b);
    }
    strong_components scc(N,graph);
    cmp_graph = scc.get_cmp_graph();
    for(int i=1;i<=N;i++) cmp_node[i] = scc.get_cmp_node(i);
    int num_node = scc.get_num_cmp();
    for(int i=0;i<=N;i++) for(int j=0;j<8;j++){
        ma[i][j] = -inf;
        mi[i][j] = inf;
        maid[i][j] = -1;
        miid[i][j] = -1;
    }
    vector<int> cnt(N+1);
    for(int i=1;i<=num_node;i++) cmp_graph[0].push_back(i);
    for(int i=0;i<=num_node;i++) for(auto x:cmp_graph[i]) cnt[x]++;
    queue<int> Q;
    Q.push(0);
    /* cerr << "graph_size: " << num_node << endl;
    for(int i=1;i<=num_node;i++){
        cerr << "cmp " << i << ": ";
        for(auto x:cmp_node[i]) cerr << x << " ";
        cerr << endl;
    }*/
    while(!Q.empty()){
        int now = Q.front(); Q.pop();
//        cerr << now << endl;
        //強連結成分内の点を追加
        for(auto x:cmp_node[now]){
            for(int j=0;j<8;j++){
                ll val = X[x]*dx[j]+Y[x]*dy[j]+Z[x]*dz[j];
                if(ma[now][j]<val){
                    ma[now][j] = val;
                    maid[now][j] = x;
                }
                if(mi[now][j]>val){
                    mi[now][j] = val;
                    miid[now][j] = x;
                }
            }
        }
        //強連結成分内の各点の答えを求める
        for(auto x:cmp_node[now]){
            //cerr << "now: " << now << " x: " << x << endl;
            for(int j=0;j<8;j++){
                if(maid[now][j]!=-1){
                    int id = maid[now][j];
                    ans[x] = max(ans[x],abs(X[x]-X[id])+abs(Y[x]-Y[id])+abs(Z[x]-Z[id]));
                }
                if(miid[now][j]!=-1){
                    int id = miid[now][j];
                    ans[x] = max(ans[x],abs(X[x]-X[id])+abs(Y[x]-Y[id])+abs(Z[x]-Z[id]));
                }
            }
        }
        //行き先に自分の情報を伝える
        for(auto x:cmp_graph[now]){
            for(int j=0;j<8;j++){
                if(ma[x][j]<ma[now][j]){
                    ma[x][j] = ma[now][j];
                    maid[x][j] = maid[now][j];
                }
                if(mi[x][j]>mi[now][j]){
                    mi[x][j] = mi[now][j];
                    miid[x][j] = miid[now][j];
                }               
            }
            cnt[x]--;
            if(!cnt[x]) Q.push(x);
        }
    }
    for(int i=1;i<=N;i++) cout << ans[i] << endl;
}

/* 
192657310
138809442
0
138809442
0
270544279
96766390
0
96664294
0


*/
accuracy: 1
time_ms: 720
memory_kb: 148292
final_rank: 16

code_snippet: #include <iostream>
#include <utility>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stdlib.h>
#define llint long long
#define mod 998244353
#define eps 1e-8
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

llint n, m;
vector<llint> G[200005], revG[200005];
vector<llint> g[200005];
int x[200005], y[200005], z[200005];
vector<llint> vec[200005];
P cand[200005][8];
llint ans[200005];

vector<int> topo;
bool used[200005];
int scc[200005];

void tpsort(int v)
{
	used[v] = true;
	for(int i = 0; i < G[v].size(); i++){
		if(!used[G[v][i]]) tpsort(G[v][i]);
	}
	topo.push_back(v);
}
void sccdfs(int v, int id)
{
	used[v] = true;
	scc[v] = id;
	for(int i = 0; i < revG[v].size(); i++){
		if(!used[revG[v][i]]) sccdfs(revG[v][i], id);
	}
}

llint dist(llint i, llint j)
{
	return abs(x[i]-x[j]) + abs(y[i]-y[j]) + abs(z[i]-z[j]);
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		cin >> x[i] >> y[i] >> z[i];
	}
	llint u, v;
	for(int i = 0; i < m; i++){
		cin >> u >> v;
		G[u].push_back(v);
		revG[v].push_back(u);
	}
	
	for(int i = 1; i <= n; i++) if(!used[i]) tpsort(i);
	reverse(topo.begin(), topo.end());
	
	int id = 0;
	for(int i = 1; i <= n; i++) used[i] = false;
	for(int i = 0; i < topo.size(); i++) if(!used[topo[i]]) sccdfs(topo[i], id++);
	for(int i = 1; i <= n; i++) vec[scc[i]].push_back(i);
	
	set<llint> S;
	for(int i = 0; i < id; i++){
		S.clear();
		for(int j = 0; j < vec[i].size(); j++){
			for(int k = 0; k < G[vec[i][j]].size(); k++){
				S.insert(scc[G[vec[i][j]][k]]);
			}
		}
		for(auto it = S.begin(); it != S.end(); it++){
			if(*it == i) continue;
			g[i].push_back(*it);
		}
	}
	
	/*for(int i = 0; i < id; i++){
		for(int j = 0; j < g[i].size(); j++){
			cout << g[i][j] << " ";
		}
		cout<< endl;
	}*/
	
	for(int k = id-1; k >= 0; k--){
		for(int i = 0; i < 8; i++) cand[k][i] = make_pair(-inf, -inf);
		for(int i = 0; i < 8; i++){
			for(llint j = 0; j < vec[k].size(); j++){
				llint v = vec[k][j];
				llint f = 0;
				if(i&1) f += x[v]; else f -= x[v];
				if(i&2) f += y[v]; else f -= y[v];
				if(i&4) f += z[v]; else f -= z[v];
				cand[k][i] = max(cand[k][i], make_pair(f, v));
			}
		}
	}
	
	for(int k = id-1; k >= 0; k--){
		for(int j = 0; j < g[k].size(); j++){
			int nk = g[k][j];
			for(int i = 0; i < 8; i++) cand[k][i] = max(cand[k][i], cand[nk][i]);
		}
		for(int i = 0; i < 8; i++){
			for(llint j = 0; j < vec[k].size(); j++){
				int v = vec[k][j];
				//cout << v << " " << cand[i].second << endl;
				ans[v] = max(ans[v], dist(v, cand[k][i].second));
			}
		}
	}
	
	for(int i = 1; i <= n; i++){
		cout << ans[i] << "\n";
	}
	flush(cout);
	
	return 0;
}
accuracy: 1
time_ms: 320
memory_kb: 81880
final_rank: 6

code_snippet: #include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <array>
#include <set>
#include <queue>

template <typename F>
class fix_point: F {
public:
  explicit constexpr fix_point(F&& f) noexcept: F(std::forward<F>(f)) {}

  template <typename... Args>
  constexpr decltype(auto) operator ()(Args&&... args) const {
    return F::operator ()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
static inline constexpr decltype(auto) make_fix_point(F&& f) noexcept {
  return fix_point<F>{std::forward<F>(f)};
}

template <typename Weight>
struct edge {
  using value_type = Weight;
  size_t src, dst;
  value_type cost;

  edge(size_t src, size_t dst, value_type cost = 1):
    src(src), dst(dst), cost(cost)
  {}

  bool operator <(edge<value_type> const& rhs) const {
    if (cost != rhs.cost) return cost < rhs.cost;
    if (src != rhs.src) return src < rhs.src;
    return dst < rhs.dst;
  }
};

template <typename Weight>
struct graph: public std::vector<std::vector<edge<Weight>>> {
  using value_type = Weight;
  graph(size_t n): std::vector<std::vector<edge<value_type>>>(n) {}

  void connect_to(size_t src, size_t dst, value_type cost = 1) {
    (*this)[src].emplace_back(src, dst, cost);
  }

  void connect_with(size_t src, size_t dst, value_type cost = 1) {
    connect_to(src, dst, cost);
    connect_to(dst, src, cost);
  }
};

template <class Weight>
std::vector<size_t> strongly_connected_components(const graph<Weight>& g) {
  size_t n = g.size();
  graph<int> rev(n);
  for (const auto& v: g)
    for (const auto& e: v)
      rev.connect_to(e.dst, e.src);

  std::vector<bool> used(n);
  std::vector<size_t> vs;
  auto dfs = make_fix_point([&](auto f, size_t v) -> void {
      used[v] = true;
      for (size_t i = 0; i < g[v].size(); ++i)
        if (!used[g[v][i].dst]) f(g[v][i].dst);
      vs.push_back(v);
  });
  for (size_t i = 0; i < n; ++i)
    if (!used[i]) dfs(i);

  used.assign(n, false);
  std::vector<size_t> cmp(n);
  size_t num = 0;
  auto rdfs = make_fix_point([&](auto f, size_t v) -> void {
      used[v] = true;
      cmp[v] = num;
      for (size_t i = 0; i < rev[v].size(); ++i)
        if (!used[rev[v][i].dst]) f(rev[v][i].dst);
  });

  for (size_t i = vs.size(); i--;)
    if (!used[vs[i]]) {
      rdfs(vs[i]);
      ++num;
    }

  return cmp;
}

class neko {
  std::array<intmax_t, 8> a = {};

public:
  neko() {
    for (intmax_t i = 0; i < 8; ++i) a[i] = -1e14;
  }
  neko(intmax_t x, intmax_t y, intmax_t z) {
    for (int i = 0; i < 8; ++i) {
      a[i] += ((i & 1)? +x: -x);
      a[i] += ((i & 2)? +y: -y);
      a[i] += ((i & 4)? +z: -z);
    }
  }
  neko(neko const& other) = default;

  neko max(neko const& other) const {
    neko res;
    for (size_t i = 0; i < 8; ++i)
      res.a[i] = std::max(a[i], other.a[i]);
    return res;
  }

  intmax_t tsurai(neko const& other) const {
    intmax_t res = 0;
    for (size_t i = 0; i < 8; ++i)
      res = std::max(res, a[i] - other.a[i]);
    return res;
  }

  void inspect() const {
    for (size_t i = 0; i < 8; ++i) fprintf(stderr, "%jd%c", a[i], i<7? ' ': '\n');
  }
};

int main() {
  size_t n, m;
  scanf("%zu %zu", &n, &m);

  std::vector<intmax_t> x(n), y(n), z(n);
  for (size_t i = 0; i < n; ++i)
    scanf("%jd %jd %jd", &x[i], &y[i], &z[i]);

  graph<int> g(n);
  std::vector<std::pair<size_t, size_t>> es;
  for (size_t i = 0; i < m; ++i) {
    size_t u, v;
    scanf("%zu %zu", &u, &v);
    --u, --v;
    g.connect_to(u, v);
    es.emplace_back(u, v);
  }

  auto scc = strongly_connected_components(g);
  size_t n1 = *std::max_element(scc.begin(), scc.end()) + 1;

  std::vector<neko> dp(n1);
  for (size_t i = 0; i < n; ++i) {
    dp[scc[i]] = dp[scc[i]].max(neko(x[i], y[i], z[i]));
  }
  // for (size_t i = 0; i < n1; ++i)
  //   dp[i].inspect();

  graph<size_t> h(n1);
  std::vector<size_t> indeg(n1);
  {
    std::set<std::pair<size_t, size_t>> es0;
    for (size_t i = 0; i < m; ++i) {
      size_t u0 = scc[es[i].first];
      size_t v0 = scc[es[i].second];
      if (u0 == v0) continue;
      if (es0.count(std::make_pair(v0, u0))) continue;
      h.connect_to(v0, u0);
      es0.insert(std::make_pair(v0, u0));
      ++indeg[u0];
    }
  }

  std::queue<size_t> q;
  for (size_t i = 0; i < n1; ++i)
    if (indeg[i] == 0) q.push(i);

  while (!q.empty()) {
    size_t v = q.front();
    // fprintf(stderr, "v: %zu\n", v);
    q.pop();
    for (auto const& e: h[v]) {
      dp[e.dst] = dp[e.dst].max(dp[e.src]);
      if (--indeg[e.dst] != 0) continue;
      q.push(e.dst);
    }
  }

  // for (size_t i = 0; i < n1; ++i)
  //   dp[i].inspect();
  for (size_t i = 0; i < n; ++i)
    printf("%jd\n", dp[scc[i]].tsurai(neko(x[i], y[i], z[i])));
}
accuracy: 1
time_ms: 340
memory_kb: 66616
final_rank: 3

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-5;
const ld pi = acos(-1.0);


int x[1 << 19], y[1 << 19], z[1 << 19];

int ans[1 << 19][8];

int calc(int id, int t) {
	int ret = 0;
	if (t % 2) {
		ret += x[id];
	}
	else {
		ret -= x[id];
	}
	t /= 2;
	if (t % 2) {
		ret += y[id];
	}
	else {
		ret -= y[id];
	}
	t /= 2;
	if (t % 2) {
		ret += z[id];
	}
	else {
		ret -= z[id];
	}
	return ret;
}
int calcinv(int id, int t) {
	return calc(id, (7 ^ t));
}

struct graph {
private:
	int n;
	vector<vector<int>> G, rG;
	vector<bool> used;
	vector<int> vs;

	int mk;
	vector<vector<int>> fG;
	vector<vector<int>> ori;
	vector<int> trans;
public:
	graph(int sz) {
		n = sz;
		G.resize(n);
		rG.resize(n);
		used.resize(n);

		fG.resize(n);
		trans.resize(n, -1);
		ori.resize(n);
	}
	void add_edge(int a, int b) {
		G[a].push_back(b);
		rG[b].push_back(a);
	}
	void dfs(int v) {
		used[v] = true;
		rep(i, G[v].size()) {
			if (!used[G[v][i]])dfs(G[v][i]);
		}
		vs.push_back(v);
	}
	void rdfs(int v, int k) {
		used[v] = true;
		queue<int> q; q.push(v);
		vector<int> c;
		while (!q.empty()) {
			int id = q.front(); q.pop();
			ori[k].push_back(id);
			rep(j, rG[id].size()) {
				int to = rG[id][j];
				if (used[to]) {
					if (trans[to] >= 0)c.push_back(trans[to]);
					continue;
				}
				used[to] = true; q.push(to);
			}
		}
		sort(c.begin(), c.end());
		int len = unique(c.begin(), c.end()) - c.begin();
		rep(i, len) {
			fG[c[i]].push_back(k);
		}
		rep(i, ori[k].size()) {
			trans[ori[k][i]] = k;
		}
	}
	void scc() {
		fill(used.begin(), used.end(), false);
		rep(i, n) {
			if (!used[i])dfs(i);
		}
		fill(used.begin(), used.end(), false);
		int k = 0;
		per(i, (int)vs.size()) {
			if (!used[vs[i]]) {
				rdfs(vs[i], k); k++;
			}
		}
		mk = k;
	}
	void query() {
		per(i, mk) {
			rep(j, ori[i].size()) {
				int id = ori[i][j];
				rep(k, 8) {
					ans[i][k] = max(ans[i][k], calc(id, k));
				}
			}
			rep(j, fG[i].size()) {
				int to = fG[i][j];
				rep(k, 8) {
					ans[i][k] = max(ans[i][k], ans[to][k]);
				}
			}
		}
	}
	void solve() {
		rep(i, n) {
			int id = trans[i];
			int ma = 0;
			rep(j, 8) {
				ma = max(ma, ans[id][j] + calcinv(i,j));
			}
			cout << ma << endl;
		}
	}
};

void solve() {
	int n, m; cin >> n >> m;
	rep(i, n) {
		cin >> x[i] >> y[i] >> z[i];
	}
	graph g(n);
	rep(i, m) {
		int a, b; cin >> a >> b; a--; b--;
		g.add_edge(a, b);
	}
	g.scc();
	rep(i, n)rep(j, 8)ans[i][j] = -mod;
	g.query(); g.solve();
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 58688
final_rank: 4