답안 #500756

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
500756 2022-01-01T07:51:11 Z mars4 공장들 (JOI14_factories) C++17
100 / 100
4619 ms 179192 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include "factories.h"

using namespace std;
using namespace __gnu_pbds; 

#define ff              first
#define ss              second
#define ll              int64_t
#define ld              long double
#define nl              cout<<"\n"
#define all(v)          v.begin(),v.end()
#define mset(a,v)       memset((a),(v),sizeof(a))
#define forn(i,a,b)     for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b)     for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio()        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define mod         1'000'000'007
#define mod2        998'244'353 
#define inf         1'000'000'000'000'007
#define pi          3.14159265358979323846

template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;

template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p)        {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v)                {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v)                 {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s)                   {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m)         {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}

void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail> 
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
	#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
	#define dbg(...)
#endif

//---------------------------------mars4---------------------------------

vector<vector<ll>> v;
vector<vector<pair<ll,ll>>> ve;
vector<ll> len;
vector<ll> updated;

void dfs(ll cur,ll prev,ll d)
{
	len[cur]=d;
	for(auto [i,w]:ve[cur])
	{
		if(i!=prev)
		{
			dfs(i,cur,d+w);
		}
	}
}

class SegtreeMin
{
	ll N;
	
	public:
	vector<ll> segtree;

	void init(ll n,ll val=0)
	{
		N=n;
		segtree=vector<ll>(N<<1,val);
	}

	void build(vector<ll> &a)
	{
		forn(i,0,N)
			segtree[i+N]=a[i];
		forb(i,N-1,1)
			segtree[i]=min(segtree[i<<1],segtree[i<<1|1]);
	}

	void update(ll i,ll val,bool reset=false)
	{
		if(reset or segtree[i+N]>val)
		{
			for(segtree[i+=N]=val;i>>=1;)
				segtree[i]=min(segtree[i<<1],segtree[i<<1|1]);
		}
	}

	ll query(ll l,ll r)
	{
		ll res=inf;
		for(l+=N,r+=N+1;l<r;l>>=1,r>>=1)
		{
			if(l&1) res=min(res,segtree[l++]);
			if(r&1) res=min(res,segtree[--r]);
		}
		return res;
	}
};

class HLD
{
	ll N;
	ll timer;

	void dfs_sz(ll cur,ll prev,vector<vector<ll>> &v)
	{
		child[cur]=1;
		parent[cur]=prev;
		for(ll i:v[cur])
		{
			if(i!=prev)
			{
				depth[i]=depth[cur]+1;
				dfs_sz(i,cur,v);
				child[cur]+=child[i];
			}
		}
	}

	void dfs_hld(ll cur,ll prev,ll top,vector<vector<ll>> &v)
	{
		timer++;
		st[cur]=timer;
		tp[cur]=top;
		dn[top]=cur;
		ll h_ind=-1,h_sz=-1;
		for(ll i=0;i<(ll)v[cur].size();i++)
		{
			if(v[cur][i]!=prev)
			{
				if(child[v[cur][i]]>h_sz)
				{
					h_ind=i;
					h_sz=child[v[cur][i]];
				}
			}
		}
		if(h_ind==-1)
		{
			ed[cur]=timer;
			return;
		}
		swap(v[cur][0],v[cur][h_ind]);
		dfs_hld(v[cur][0],cur,top,v);
		for(ll i:v[cur])
		{
			if(i!=prev and i!=v[cur][0])
			{
				dfs_hld(i,cur,i,v);
			}
		}
		ed[cur]=timer;
	}

	public:
	vector<ll> child;
	vector<ll> parent;
	vector<ll> depth;
	vector<ll> st;
	vector<ll> ed;
	vector<ll> tp;
	vector<ll> dn;

	// use segtree class
	SegtreeMin s_up,s_down;

	void init(vector<vector<ll>> &v,ll root=0)
	{
		N=(ll)v.size();
		s_up.init(N,inf);
		s_down.init(N,inf);
		child=vector<ll>(N);
		parent=vector<ll>(N);
		depth=vector<ll>(N);
		st=vector<ll>(N);
		ed=vector<ll>(N);
		tp=vector<ll>(N);
		dn=vector<ll>(N);
		timer=-1;
		dfs_sz(root,root,v);
		dfs_hld(root,root,root,v);
	}

	void update(ll node)
	{
		ll dist=0;
		while(true)
		{
			updated.push_back(st[node]);
			s_up.update(st[node],dist-len[node]);
			s_down.update(st[node],dist+len[node]);
			ll new_node=parent[tp[node]];
			if(node==new_node)
			{
				break;
			}
			dist+=len[node]-len[new_node];
			node=new_node;
		}
	}

	// use appropriate query operation (vertex/edge)
	ll query(ll node)
	{
		ll res=inf;
		ll dist=0;
		while(true)
		{
			ll up_val=s_up.query(st[tp[node]],st[node]);
			ll down_val=s_down.query(st[node],st[dn[tp[node]]]);
			ll nres=min(up_val+len[node],down_val-len[node])+dist;
			res=min(res,nres);
			ll new_node=parent[tp[node]];
			if(node==new_node)
			{
				break;
			}
			dist+=len[node]-len[new_node];
			node=new_node;
		}
		return res;
	}
};

HLD hld;

void Init(int N,int A[],int B[],int D[])
{
	v=vector<vector<ll>>(N);
	ve=vector<vector<pair<ll,ll>>>(N);
	len=vector<ll>(N);
	forn(i,0,N-1)
	{
		v[A[i]].push_back({B[i]});
		v[B[i]].push_back({A[i]});
		ve[A[i]].push_back({B[i],D[i]});
		ve[B[i]].push_back({A[i],D[i]});
	}
	dfs(0,-1,0);
	hld.init(v);
}

long long Query(int S,int X[],int T,int Y[])
{
	forn(i,0,S)
	{
		hld.update(X[i]);
	}
	ll res=inf;
	forn(i,0,T)
	{
		res=min(res,hld.query(Y[i]));
	}
	for(ll i:updated)
	{
		hld.s_up.update(i,inf,true);
		hld.s_down.update(i,inf,true);
	}
	updated.clear();
	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 716 KB Output is correct
2 Correct 653 ms 9556 KB Output is correct
3 Correct 698 ms 19056 KB Output is correct
4 Correct 646 ms 19204 KB Output is correct
5 Correct 478 ms 19352 KB Output is correct
6 Correct 425 ms 19064 KB Output is correct
7 Correct 687 ms 18952 KB Output is correct
8 Correct 631 ms 19012 KB Output is correct
9 Correct 467 ms 19224 KB Output is correct
10 Correct 392 ms 18992 KB Output is correct
11 Correct 651 ms 19160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2794 ms 128056 KB Output is correct
3 Correct 2135 ms 148492 KB Output is correct
4 Correct 1241 ms 144684 KB Output is correct
5 Correct 1798 ms 175320 KB Output is correct
6 Correct 2319 ms 150656 KB Output is correct
7 Correct 1466 ms 48244 KB Output is correct
8 Correct 858 ms 48372 KB Output is correct
9 Correct 931 ms 52324 KB Output is correct
10 Correct 1356 ms 49912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 716 KB Output is correct
2 Correct 653 ms 9556 KB Output is correct
3 Correct 698 ms 19056 KB Output is correct
4 Correct 646 ms 19204 KB Output is correct
5 Correct 478 ms 19352 KB Output is correct
6 Correct 425 ms 19064 KB Output is correct
7 Correct 687 ms 18952 KB Output is correct
8 Correct 631 ms 19012 KB Output is correct
9 Correct 467 ms 19224 KB Output is correct
10 Correct 392 ms 18992 KB Output is correct
11 Correct 651 ms 19160 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2794 ms 128056 KB Output is correct
14 Correct 2135 ms 148492 KB Output is correct
15 Correct 1241 ms 144684 KB Output is correct
16 Correct 1798 ms 175320 KB Output is correct
17 Correct 2319 ms 150656 KB Output is correct
18 Correct 1466 ms 48244 KB Output is correct
19 Correct 858 ms 48372 KB Output is correct
20 Correct 931 ms 52324 KB Output is correct
21 Correct 1356 ms 49912 KB Output is correct
22 Correct 4619 ms 157524 KB Output is correct
23 Correct 4280 ms 162904 KB Output is correct
24 Correct 3588 ms 158928 KB Output is correct
25 Correct 3625 ms 161976 KB Output is correct
26 Correct 3630 ms 156536 KB Output is correct
27 Correct 2398 ms 179192 KB Output is correct
28 Correct 1908 ms 157112 KB Output is correct
29 Correct 3675 ms 156312 KB Output is correct
30 Correct 3524 ms 155768 KB Output is correct
31 Correct 3475 ms 156492 KB Output is correct
32 Correct 862 ms 54396 KB Output is correct
33 Correct 790 ms 50172 KB Output is correct
34 Correct 1543 ms 46180 KB Output is correct
35 Correct 1577 ms 46212 KB Output is correct
36 Correct 1313 ms 46724 KB Output is correct
37 Correct 1330 ms 46816 KB Output is correct