Submission #422349

# Submission time Handle Problem Language Result Execution time Memory
422349 2021-06-10T04:16:33 Z amunduzbaev Factories (JOI14_factories) C++14
0 / 100
8000 ms 524288 KB
#include "factories.h"

#ifndef EVAL
#include "grader.cpp"
#endif

#include "bits/stdc++.h"
using namespace std;

#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define int long long
#define sz(x) (int)x.size()
#define i32 int32_t

template<class T> bool umin(T& a, const T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool umax(T& a, const T b) { if(a < b) { a = b; return 1; } return 0; }

const int MAXN = 5e5+5;
const int mod = 1e9+7;
const int inf = 1e18;

vector<pii> edges[MAXN], ss;
int n, tin[MAXN], tout[MAXN], t, lg[MAXN], dep[MAXN];
pii st[MAXN*2][20];

void dfs(int u, int p = -1){
	ss.pb({dep[u], u});
	tin[u] = sz(ss)-1;
	for(auto x : edges[u]){
		if(x.ff == p) continue;
		dep[x.ff] = dep[u] + x.ss;
		dfs(x.ff, u), ss.pb({dep[u], u});
	} tout[u] = sz(ss)-1;
}

bool par(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); }
int find_lca(int a, int b){
	if(par(a, b)) return dep[b] - dep[a];
	if(par(b, a)) return dep[a] - dep[b];
	if(tin[a] > tout[b]) swap(a, b);
	int l = tout[a], r = tin[b], llg = lg[r-l+1];
	int dis = min(st[l][llg].ff, st[r-(1<<llg)+1][llg].ff);
	return dep[a] + dep[b] - dis * 2ll;
}

void Init(i32 n, i32 a[], i32 b[], i32 d[]) {
	for(int i=0;i<n-1;i++){ // a[i]++, b[i]++;
		edges[a[i]].pb({b[i], d[i]}), edges[b[i]].pb({a[i], d[i]}); 
	} dfs(0); 
	lg[1] = 0;
	for(int i=2;i<MAXN;i++) lg[i] = lg[i/2]+1;
	for(int i=0;i<sz(ss);i++) st[i][0] = ss[i];
	for(int j=1;j<20;j++){
		for(int i=0;i<sz(ss);i++){
			if(i + (1<<(j - 1)) < sz(ss)) st[i][j] = min(st[i][j-1], st[i+(1<<(j-1))][j-1]);
			else st[i][j] = st[i][j-1];
		}
	} 
}

int Query(i32 s, i32 x[], i32 t, i32 y[]) {
	int res = inf;
	for(int i=0;i<s;i++){
		for(int j=0;j<t;j++){
			int a = x[i], b = y[j]; 
			umin(res, find_lca(a, b));
		}
	} return res;
}
# Verdict Execution time Memory Grader output
1 Correct 28 ms 16616 KB Output is correct
2 Execution timed out 8055 ms 33924 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 16380 KB Output is correct
2 Runtime error 1642 ms 524288 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 16616 KB Output is correct
2 Execution timed out 8055 ms 33924 KB Time limit exceeded
3 Halted 0 ms 0 KB -