Submission #1039399

#TimeUsernameProblemLanguageResultExecution timeMemory
1039399arashMLGReconstruction Project (JOI22_reconstruction)C++17
100 / 100
604 ms32180 KiB
#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...)    69
#define debugArr(...)  69
#endif
using namespace std;

typedef long long     ll;
typedef pair<int,int> pii;
typedef pair<ll,ll>   pll;

const int N = 500 + 23;
const int M = 1e5 + 23;
const ll inf = 1e18;

#define F           first
#define S           second
#define pb          push_back
#define kill(x)     cout<<x<<endl, exit(0);
#define all(x)      x.begin(),x.end()
#define sz(x)       (int)x.size()
#define lc          (v << 1)
#define rc          ((v<<1) |1)

struct Edge {
	int w,a,b;
};

struct DSU {
	int par[N];
	int rnk[N];
	
	void clear() {
		iota(par,par+N,0);
		fill(rnk,rnk+N,0);
	}
	
	int getPar(int v) {
		return (par[v] == v ? v : par[v] = getPar(par[v]));
	}
	
	void merge(int v,int u) {
		v = getPar(v),u = getPar(u);
		if(v == u) return;
		if(rnk[v] > rnk[u]) swap(v,u);
		par[v] = u;
		if(rnk[v] == rnk[u]) rnk[u] ++;
	}
} dsu;

int n,m;
Edge edges[M];
int l[M],r[M];
vector<Edge> vals;

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin>>n>>m;
    for(int i =1; i <= m ;i ++) {
    	cin>>edges[i].a>>edges[i].b>>edges[i].w;
    }
    sort(edges +1,edges+m+1,[&](const Edge &x,const Edge &y) { return x.w < y.w;});
	fill(r,r+M,m+1);
	for(int i = 1; i <= m ;i  ++) {
		l[i] = i-1;
		dsu.clear();
		while(l[i]) {
			dsu.merge(edges[l[i]].a,edges[l[i]].b);
			if(dsu.getPar(edges[i].a) == dsu.getPar(edges[i].b)) break;
			l[i] --; 
		}
		r[l[i]] = i;
	}
	for(int i = 1; i <= m ;i ++) {
		if(l[i]) vals.pb({(edges[i].w + edges[l[i]].w)/2 + 1,-1,edges[i].w});
		else     vals.pb({0,-1,edges[i].w});
		vals.pb({edges[i].w,2,-2*edges[i].w});
		if(r[i] != m+1) vals.pb({(edges[i].w + edges[r[i]].w)/2 +1,-1,edges[i].w});
	}
	sort(all(vals),[&](const Edge &x,const Edge &y) {return x.w < y.w;});
	int q; cin>>q;
	ll c1=0,c2 = 0;
	int I = 0;
	while(q--) {
		int x; cin>>x;
		while(I < sz(vals) && vals[I].w <= x) {
			c1 += vals[I].a;
			c2 += vals[I].b;
			I++;
		}
		cout << c1*x + c2 << '\n';
	}
	return 0;
}

// Jumpsuit, Jumpsuit cover me!
// Jumpsuit, Jumpsuit cover me!
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...