Submission #295451

# Submission time Handle Problem Language Result Execution time Memory
295451 2020-09-09T16:53:01 Z patrikpavic2 Factories (JOI14_factories) C++17
100 / 100
6063 ms 240552 KB
#include "factories.h"
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>

#define X first
#define Y second
#define PB push_back

using namespace std;

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

const int N = 5e5 + 500;
const int LOG = 20;

int ima[N][2], par[N][LOG], dep[N], L[N], R[N], tme, n;
ll dis[N][LOG];
vector < pii > v[N];
vector < pil > g[N];

void dfs(int x, int lst){
	L[x] = tme++;
	for(pii nxt : v[x]){
		if(nxt.X == lst) continue;
		dep[nxt.X] = dep[x] + 1;
		par[nxt.X][0] = x;
		dis[nxt.X][0] = nxt.Y;
		dfs(nxt.X, x);
	}
	R[x] = tme - 1;
}

inline bool predak(int x, int y){
	return L[x] <= L[y] && L[y] <= R[x];
}

void precompute(){
	for(int j = 1;j < LOG;j++){
		for(int i = 0;i < n;i++){
			par[i][j] = par[par[i][j - 1]][j - 1];
			dis[i][j] = dis[i][j - 1] + dis[par[i][j - 1]][j - 1];
		}
	}
}

int lca(int x, int y){
	if(dep[x] < dep[y]) swap(x, y);
	if(predak(y, x)) return y;
	for(int i = LOG - 1;i >= 0;i--)
		if(dep[x] - dep[y] >= (1 << i))
			x = par[x][i];
	for(int i = LOG - 1;i >= 0;i--)
		if(par[x][i] != par[y][i])
			x = par[x][i],
			y = par[y][i];
	return par[x][0];
}

bool raz(int x, int y){
	return (ima[x][0] && ima[y][1]) || (ima[x][1] && ima[y][0]);
}

ll get_dis(int x, int y){
	if(!predak(x, y)) return (ll)1e18;
	ll ret = 0;
	for(int i = LOG - 1; i >= 0;i--)
		if(dep[y] - dep[x] >= (1 << i)){
			ret += dis[y][i];
			y = par[y][i];
		}
	return ret;
}

ll sol = 1e18;

pll f(int x){
	if(ima[x][0] && ima[x][1]){
		sol = min(sol, 0LL);
		return {0, 0};
	}
	pll ret = {(ll)1e18 * (!ima[x][0]), (ll)1e18 * (!ima[x][1])};
	for(pil nxt : g[x]){
		pll nov = f(nxt.X);
		nov.X += nxt.Y, nov.Y += nxt.Y;
		sol = min(sol, min(nov.X + ret.Y, nov.Y + ret.X));
		ret.X = min(ret.X, nov.X);
		ret.Y = min(ret.Y, nov.Y);
	}
	return ret;
}

void Init(int nn, int a[], int b[], int d[]) {
	n = nn;
	for(int i = 0;i + 1 < n;i++){
		v[a[i]].PB({b[i], d[i]});
		v[b[i]].PB({a[i], d[i]});
	}
	dfs(0, 0); precompute();
}

bool cmp(int x, int y){
	return L[x] < L[y];
}

ll Query(int s, int x[], int t, int y[]) {
	//printf("kverij\n");
	vector < int > sve;	
	for(int i = 0;i < s;i++){
		ima[x[i]][0] = 1;
		sve.PB(x[i]);
	}
	for(int i = 0;i < t;i++){
		ima[y[i]][1] = 1;
		sve.PB(y[i]);
	}
	sort(sve.begin(), sve.end(), cmp);
	sve.erase(unique(sve.begin(), sve.end()), sve.end());
	int m = (int)sve.size();
	for(int i = 0;i + 1 < m;i++){
		sve.PB(lca(sve[i], sve[i + 1]));
	}
	sort(sve.begin(), sve.end(), cmp);
	sve.erase(unique(sve.begin(), sve.end()), sve.end());
	m = (int)sve.size();
	stack < int > S; S.push(sve[0]);
	for(int i = 1;i < m;i++){
		while(!predak(S.top(), sve[i]))
			S.pop();
		g[S.top()].PB({sve[i], get_dis(S.top(), sve[i])});
		//printf("%d -- %lld --> %d\n", S.top(), g[S.top()].back().Y, sve[i]);
		S.push(sve[i]);
	}
	sol = 1e18; f(sve[0]);
	for(int i = 0;i < m;i++){
		ima[sve[i]][0] = 0; ima[sve[i]][1] = 0;
		g[sve[i]].clear();
	}
	return sol;
}

# Verdict Execution time Memory Grader output
1 Correct 45 ms 24440 KB Output is correct
2 Correct 1375 ms 43128 KB Output is correct
3 Correct 1413 ms 43256 KB Output is correct
4 Correct 1360 ms 43128 KB Output is correct
5 Correct 1210 ms 43452 KB Output is correct
6 Correct 966 ms 43128 KB Output is correct
7 Correct 1369 ms 43196 KB Output is correct
8 Correct 1353 ms 43264 KB Output is correct
9 Correct 1142 ms 43256 KB Output is correct
10 Correct 960 ms 43384 KB Output is correct
11 Correct 1347 ms 43384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 24192 KB Output is correct
2 Correct 2378 ms 201532 KB Output is correct
3 Correct 3550 ms 203232 KB Output is correct
4 Correct 1778 ms 201744 KB Output is correct
5 Correct 3064 ms 232148 KB Output is correct
6 Correct 3850 ms 205428 KB Output is correct
7 Correct 3235 ms 78808 KB Output is correct
8 Correct 1678 ms 78696 KB Output is correct
9 Correct 2611 ms 84040 KB Output is correct
10 Correct 3288 ms 79912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 24440 KB Output is correct
2 Correct 1375 ms 43128 KB Output is correct
3 Correct 1413 ms 43256 KB Output is correct
4 Correct 1360 ms 43128 KB Output is correct
5 Correct 1210 ms 43452 KB Output is correct
6 Correct 966 ms 43128 KB Output is correct
7 Correct 1369 ms 43196 KB Output is correct
8 Correct 1353 ms 43264 KB Output is correct
9 Correct 1142 ms 43256 KB Output is correct
10 Correct 960 ms 43384 KB Output is correct
11 Correct 1347 ms 43384 KB Output is correct
12 Correct 20 ms 24192 KB Output is correct
13 Correct 2378 ms 201532 KB Output is correct
14 Correct 3550 ms 203232 KB Output is correct
15 Correct 1778 ms 201744 KB Output is correct
16 Correct 3064 ms 232148 KB Output is correct
17 Correct 3850 ms 205428 KB Output is correct
18 Correct 3235 ms 78808 KB Output is correct
19 Correct 1678 ms 78696 KB Output is correct
20 Correct 2611 ms 84040 KB Output is correct
21 Correct 3288 ms 79912 KB Output is correct
22 Correct 4560 ms 217896 KB Output is correct
23 Correct 4125 ms 218904 KB Output is correct
24 Correct 5010 ms 221500 KB Output is correct
25 Correct 5369 ms 223656 KB Output is correct
26 Correct 5850 ms 214096 KB Output is correct
27 Correct 4643 ms 240552 KB Output is correct
28 Correct 2988 ms 216860 KB Output is correct
29 Correct 5867 ms 212756 KB Output is correct
30 Correct 6063 ms 212408 KB Output is correct
31 Correct 5867 ms 213060 KB Output is correct
32 Correct 2483 ms 86152 KB Output is correct
33 Correct 1906 ms 81624 KB Output is correct
34 Correct 2918 ms 77720 KB Output is correct
35 Correct 2808 ms 77412 KB Output is correct
36 Correct 3306 ms 78012 KB Output is correct
37 Correct 3408 ms 77840 KB Output is correct