답안 #300144

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
300144 2020-09-16T20:34:22 Z NaimSS 공장들 (JOI14_factories) C++14
100 / 100
6944 ms 261132 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<int> vi;

//#define int long long

template<class T> struct RMQ { // floor(log_2(x))
  int level(int x) { return 31-__builtin_clz(x); } 
  vector<T> v; vector<vi> jmp;
  int comb(int a, int b) { // index of min
    return v[a]==v[b]?min(a,b):(v[a]<v[b]?a:b); } 
  void init(const vector<T>& _v) {
    v = _v; jmp = {vi(sz(v))}; iota(all(jmp[0]),0);
    for (int j = 1; 1<<j <= sz(v); ++j) {
      jmp.pb(vi(sz(v)-(1<<j)+1));
      rep(i,0,sz(jmp[j])) jmp[j][i] = comb(jmp[j-1][i],
                  jmp[j-1][i+(1<<(j-1))]);
    }
  }
  int index(int l, int r) { // get index of min element
    int d = level(r-l+1);
    return comb(jmp[d][l],jmp[d][r-(1<<d)+1]); }
  T query(int l, int r) { return v[index(l,r)]; }
};
const int MAXL = 21;
const ll inf = 1e18;

struct Tree{
  int n;
  vector<vector<pii> > g;
  vi pos,tin,tout,vis,tam,del,pai,hi;
  vector<ll> H,best;
  vector<pii> temp;
  Tree(){}
  int T;
  vi ans[MAXL];
  void init(int _n){
    T=0;
    n = _n;
    g.resize(n+1);
    best = H = vector<ll>(n+1);
    hi = pai = del = vis = tam = pos = tin = tout = vi(n+1);
    rep(i,0,MAXL)ans[i].resize(n+1);
  }
  RMQ<pii> rmq;
  void addEdge(int a,int b,int c){
    g[a].pb(pii(b,c));
    g[b].pb(pii(a,c));
  }
  void dfs(int v,int p = -1){
    pos[v] = sz(temp);
    tin[v] = ++T;
    temp.pb(pii(hi[v],v));
    trav(to,g[v])if(to.ff!=p){
      
      H[to.ff] = H[v] + to.ss;
      hi[to.ff] = hi[v] + 1;
      ans[0][to.ff] = v;
      dfs(to.ff,v);
      temp.pb(pii(hi[v],v));
    }
    tout[v] = T;
  }
  void gen(int Root){
    ans[0][Root] = Root;
    temp.clear();
    dfs(Root);
    rmq.init(temp);
    
    rep(j,1,MAXL){
      rep(i,0,n+1){
        ans[j][i] = ans[j-1][ans[j-1][i]];
      }
    }

    decompose(Root);
  }
  int lca(int a,int b){
    a = pos[a],b = pos[b];
    if(a > b)swap(a,b);
    return rmq.query(a,b).ss;
  }
  ll dist(int a,int b){
    return H[a] + H[b] - 2*H[lca(a,b)];
  }
  int calc_sz(int v,int p = -1){
  	tam[v] = 1;
  	for(auto to : g[v])if(to.ff!=p and !del[to.ff]){
  		tam[v]+=calc_sz(to.ff,v);
  	}
  	return tam[v];
  }
  int find_centroid(int v,int p,int Sz){
  	for(auto to : g[v])if(to.ff!=p and !del[to.ff]){
  		if(tam[to.ff]*2 > Sz)return find_centroid(to.ff,v,Sz);
  	}
  	return v;
  }
  void decompose(int v,int p = -1){
  	int Sz = calc_sz(v,-1);
  	int cent = find_centroid(v,-1,Sz);
  	pai[cent] = p;
  	del[cent] = 1;
  	for(auto to : g[cent]){
  		if(del[to.ff])continue;
  		decompose(to.ff,cent);
  	}
  }
  int CT = 0;
  void add(int x){
  	int c = x;
  	while(c!=-1){
  		if(vis[c] != CT){
  			vis[c] = CT;
  			best[c] = inf;
  		}
  		best[c] = min(best[c],dist(x,c));
  		c = pai[c];
  	}
  }
  ll query(int x){
  	ll res = inf;
  	int c = x;
  	while(c!=-1){
  		if(vis[c] == CT){
  			res = min(res,best[c] + dist(x,c));
  		}
  		c = pai[c];
  	}
  	return res;
  }
}tree;

void Init(int32_t N, int32_t A[], int32_t B[], int32_t D[]) {
	tree.init(N);
	for(int i=0;i<N-1;i++){
		tree.addEdge(A[i],B[i],D[i]);
	}
	tree.gen(0);
}

long long Query(int32_t S, int32_t X[], int32_t T, int32_t Y[]) {
  tree.CT++;
  for(int i=0;i<S;i++)tree.add(X[i]);
  ll res = inf;
  for(int i=0;i<T;i++)res = min(res,tree.query(Y[i]));	
  assert(res!=inf);
  return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 768 KB Output is correct
2 Correct 551 ms 9960 KB Output is correct
3 Correct 709 ms 9976 KB Output is correct
4 Correct 605 ms 19576 KB Output is correct
5 Correct 715 ms 19908 KB Output is correct
6 Correct 317 ms 19448 KB Output is correct
7 Correct 705 ms 19684 KB Output is correct
8 Correct 735 ms 19576 KB Output is correct
9 Correct 715 ms 20088 KB Output is correct
10 Correct 329 ms 19496 KB Output is correct
11 Correct 704 ms 19504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 2819 ms 198236 KB Output is correct
3 Correct 3796 ms 202488 KB Output is correct
4 Correct 1344 ms 217168 KB Output is correct
5 Correct 4973 ms 261132 KB Output is correct
6 Correct 3889 ms 222180 KB Output is correct
7 Correct 2125 ms 60912 KB Output is correct
8 Correct 637 ms 61284 KB Output is correct
9 Correct 2227 ms 66540 KB Output is correct
10 Correct 2035 ms 62192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 768 KB Output is correct
2 Correct 551 ms 9960 KB Output is correct
3 Correct 709 ms 9976 KB Output is correct
4 Correct 605 ms 19576 KB Output is correct
5 Correct 715 ms 19908 KB Output is correct
6 Correct 317 ms 19448 KB Output is correct
7 Correct 705 ms 19684 KB Output is correct
8 Correct 735 ms 19576 KB Output is correct
9 Correct 715 ms 20088 KB Output is correct
10 Correct 329 ms 19496 KB Output is correct
11 Correct 704 ms 19504 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2819 ms 198236 KB Output is correct
14 Correct 3796 ms 202488 KB Output is correct
15 Correct 1344 ms 217168 KB Output is correct
16 Correct 4973 ms 261132 KB Output is correct
17 Correct 3889 ms 222180 KB Output is correct
18 Correct 2125 ms 60912 KB Output is correct
19 Correct 637 ms 61284 KB Output is correct
20 Correct 2227 ms 66540 KB Output is correct
21 Correct 2035 ms 62192 KB Output is correct
22 Correct 4282 ms 223832 KB Output is correct
23 Correct 3573 ms 226652 KB Output is correct
24 Correct 5654 ms 227824 KB Output is correct
25 Correct 5658 ms 224200 KB Output is correct
26 Correct 5548 ms 228264 KB Output is correct
27 Correct 6944 ms 252884 KB Output is correct
28 Correct 1537 ms 218212 KB Output is correct
29 Correct 5455 ms 227608 KB Output is correct
30 Correct 5483 ms 226648 KB Output is correct
31 Correct 5380 ms 227936 KB Output is correct
32 Correct 2379 ms 67836 KB Output is correct
33 Correct 613 ms 61672 KB Output is correct
34 Correct 1701 ms 58628 KB Output is correct
35 Correct 1688 ms 58400 KB Output is correct
36 Correct 2256 ms 59268 KB Output is correct
37 Correct 2114 ms 59628 KB Output is correct