/* Author : Mychecksdead */
#pragma GCC optimize("unroll-loops,O3")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int s[N], p[N];
struct Dsu{
Dsu(int n){
for(int j = 0; j <= n; ++j) s[j] = 1, p[j] = j;
}
int find(int v){
if(p[v] == v) return v;
return p[v] = find(p[v]);
}
bool merge(int u, int v){
u = find(u);
v = find(v);
if(u != v){
if(s[u] > s[v] )swap(u, v);
p[u] = v;
s[v] += s[u];
return true;
}
return false;
}
bool merge(array<int, 3> e){
int u = e[1], v = e[2];
u = find(u);
v = find(v);
if(u != v){
if(s[u] > s[v] )swap(u, v);
p[u] = v;
s[v] += s[u];
return true;
}
return false;
}
};
int n, m, q, maxL[N], maxR[N], lsz[N], rsz[N], L[M][600], R[M][600];
array<int, 3> E[N];
// vi L[N], R[N];
void solve(){
cin >> n >> m;
for(int j=0;j<m;++j) cin >> E[j][1] >> E[j][2] >> E[j][0];
E[m] = {-MOD, 0, 0};
E[m + 1] = {MOD+MOD, 0, 0};
sort(E, E+m+2, [&](const array<int, 3> &x, const array<int, 3> &y){
return x[0] < y[0];
});
for(int i = 1; i <= m; ++i){
maxL[i] = maxR[i] = i;
}
L[1][0] = 0;
L[1][1] = 1;
L[1][2] = m + 1;
lsz[1] = 3;
for(int i = 2; i <= m; ++i){
Dsu d(n);
d.merge(E[i]);
L[i][0] = 0;
lsz[i] = 1;
for(int j = lsz[i-1]-2; j > 0; --j){
if(d.merge(E[L[i - 1][j]])){
L[i][lsz[i]++] = L[i - 1][j];
}
}
reverse(L[i]+1, L[i]+lsz[i]);
L[i][lsz[i]++] = i;
L[i][lsz[i]++] = m + 1;
}
R[m][0] = 0;
R[m][1] = m;
R[m][2] = m + 1;
rsz[1] = 3;
for(int i = m - 1; i >= 1; --i){
Dsu d(n);
d.merge(E[i]);
R[i][0] = 0;
R[i][1] = i;
rsz[i] = 2;
for(int j = 1; j + 1 < rsz[i + 1]; ++j){
if(d.merge(E[R[i + 1][j]])){
R[i][rsz[i]++] = R[i + 1][j];
}
}
R[i][rsz[i]++] = m + 1;
}
for(int i = 1; i <= m; ++i){
for(int x = 1; x + 1 < lsz[i]; ++x) maxR[L[i][x]] = max(maxR[L[i][x]], i);
for(int x = 1; x + 1 < rsz[i]; ++x) maxL[R[i][x]] = min(maxL[R[i][x]], i);
}
cin >> q;
for(int i = 1; i <= q; ++i){
ll x; cin >> x;
int pos = upper_bound(E+1, E+m+1, array<int, 3>{(int)x, -1, -1}) - E;
--pos;
if(pos == 0){
ll ans = 0;
for(int j = 1; j + 1 < rsz[pos + 1]; ++j) ans += E[R[pos + 1][j]][0] - x;
cout << ans << '\n';
continue;
}
if(pos == m){
ll ans = 0;
for(int j = 1; j + 1 < lsz[pos]; ++j) ans += x - E[L[pos][j]][0];
cout << ans << '\n';
continue;
}
int l = lsz[pos] - 2;
int r = 1;
int sz = 1;
ll ans = 0;
for(;sz < n;){
const int lp = L[pos][l];
const int rp = R[pos + 1][r];
if(x - E[lp][0] <= E[rp][0] - x){
if(maxR[lp] >= R[pos + 1][r - 1]){
ans += x - E[lp][0];
++sz;
}
--l;
}else{
if(maxL[rp] <= L[pos][l + 1]){
ans += E[rp][0] - x;
++sz;
}
++r;
}
}
cout << ans << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |