답안 #693834

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
693834 2023-02-03T09:40:20 Z sunwukong123 Transport (COCI19_transport) C++14
78 / 130
1000 ms 27772 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{"  << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach 
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first 
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> bool chmax(T& a, const T b) { return a<b ? a = b, 1 : 0; }
template <typename T> bool chmin(T& a, const T b) { return a>b ? a = b, 1 : 0; }
const int MAXN = 100005;
int A[MAXN];
int n;
vector<pi> adj[MAXN];
int lvl[MAXN];
int sz[MAXN];
int par[MAXN];
int dist[MAXN][20]; // dist from the centroid of this level

int dfs_sz(int x, int p) {
	sz[x] = 1;
	for(auto v:adj[x]) {
		if(v.f == p || lvl[v.f] != -1)continue;
		sz[x]+=dfs_sz(v.f,x);
	}
	return sz[x];
}

int dfs2(int x, int p, int S) {
	//trying to find the centroid
	for(auto v:adj[x]){
		if(v.f == p || lvl[v.f] != -1)continue;
		if(dfs_sz(v.f, x) > S/2)return dfs2(v.f,x,S);
	}
	return x;
}

namespace fw {
	unordered_map<int,int> fw;
	void update(int x, int nval) {
		++x;
		for(;x<=oo;x+=x&-x){
			fw[x]+=nval;
		}
	}	
	int point(int x) {
		int t=0; 
		for(;x;x-=x&-x){
			t+=fw[x];
		}
		return t;
	}
	int query(int x, int y) {
		++x;++y;
		return point(y) - point(x-1);
	}
}
vector<pi> vec;
void dfs3(int x, int p, int l, int mx) {
	// do stuff
	for(auto v:adj[x]) {
		if(v.f == p || lvl[v.f] != -1)continue;

		dist[v.f][l] = dist[x][l] - v.s + A[v.f];
		dfs3(v.f, x, l, max(mx, dist[v.f][l]));
	}
	// this is the start point
	
	if(dist[x][l] >= mx) {
		vec.pb({dist[x][l],1});
		//fw::update(dist[x][l], 1);
	}
}
int ans;
void dfs4(int x, int p, int l, int mn){
	for(auto v:adj[x]) {
		if(v.f == p || lvl[v.f] != -1)continue;
		dist[v.f][l] = dist[x][l] - v.s + A[v.f];
		dfs4(v.f,x,l,min(mn, dist[v.f][l] - A[v.f]));
	}
	vec.pb({abs(mn),-1});
	
	//ans += fw::query(abs(mn), 1e15);

}
void build(int x, int p, int l) {
	
	int s=dfs_sz(x,p);
	int cent = dfs2(x,p,s);

	if(p == -1) p = cent;
	//fw::update(A[cent], 1);
	
	vector<pi> V;
	V.pb({A[cent],1});
	for(auto v:adj[cent]){
		if(v.f == p || lvl[v.f] != -1)continue;

		dist[v.f][l] = -v.s+A[v.f];
		dfs4(v.f,cent,l,min(0ll, -v.s)); // consider this to be the end point

		dist[v.f][l]=A[cent] - v.s + A[v.f];
		dfs3(v.f,cent,l,max(A[cent],dist[v.f][l])); // consider this to be the start point
		int c=0;
		sort(all(vec),[](pi x, pi y) {
			if(x.f!=y.f)return x.f>y.f;
			else return x.s>y.s;
		});
		for(auto i:vec) {
			if(i.s==1)++c;
			else ans-=c; // double counted
		}
		for(auto i:vec)V.pb(i);
		vec.clear();
	}

	V.pb({0,-1});
	ans--; // centroid paired with itself;
	sort(all(V),[](pi x, pi y) {
		if(x.f!=y.f)return x.f>y.f;
		else return x.s>y.s;
	});
	int cnt = 0;
	dbvp(V);
	for(auto i:V) {
		if(i.s==1)++cnt;
		else ans+=cnt;
	}
	
	/*
	reverse(all(adj[cent]));
	for(auto v:adj[cent]){
		if(v.f == p || lvl[v.f] != -1)continue;
		
		dist[v.f][l] = A[cent]-v.s+A[v.f];
		dfs4(v.f,cent,l,min(0ll, A[cent]-v.s)); // query with these ending nodes
		dist[v.f][l]= -v.s + A[v.f];
		dfs3(v.f,cent,l,max(0ll,dist[v.f][l])); // add all potential starting nodes
	}
	ans += fw::query(0,1e15);
	fw::fw.clear();*/
	par[cent] = p;
	lvl[cent] = l;
	for(auto v:adj[cent]) {
		if(lvl[v.f] != -1)continue;
		build(v.f, cent, l+1);
	}
}

int32_t main() 
{
	IAMSPEED
	memset(lvl,-1,sizeof lvl);
	cin >> n;
	FOR(i,1,n) cin >> A[i];
	FOR(i,1,n-1) {
		int a,b,c; cin >> a >> b >> c;
		adj[a].pb({b,c});
		adj[b].pb({a,c});
	}
	build(1,-1,0);

	cout << ans;


	return (0-0) ; 
}



# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4308 KB Output is correct
2 Correct 7 ms 4564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 4880 KB Output is correct
2 Correct 190 ms 5228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 8784 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1086 ms 10852 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 13644 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 11628 KB Output is correct
2 Correct 36 ms 10548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 16140 KB Output is correct
2 Correct 348 ms 15052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 20048 KB Output is correct
2 Correct 171 ms 22176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 25548 KB Output is correct
2 Correct 223 ms 27772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1056 ms 10676 KB Time limit exceeded
2 Halted 0 ms 0 KB -