답안 #626681

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
626681 2022-08-11T16:01:55 Z codr0 Construction of Highway (JOI18_construction) C++14
100 / 100
1014 ms 23500 KB
// Code by Parsa Eslami

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>

#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
#define bit(i,j) ((j>>i)&1)
#define pb push_back
#define lc 2*id
#define rc 2*id+1
#define dmid int mid=(r+l)/2
#define err(x) cerr<#x<<" : "<<x<<'\n'
#define F first
#define S second

using namespace std;
const int N=1e5+4;
const int lg=20;
vector<int> adj[N];
int val[N];
int h[N],par[lg][N],st[N],en[N],tme;
int ver[N];
int seg[4*N];
int n;
struct fenwick{
	int fen[N];
	void add(int k,int x){
		while(k>0){
			fen[k]+=x;
			k-=(k&(-k));
		}
	}
	int get(int k){
		int rt=0;
		while(k<N){
			rt+=fen[k];
			k+=(k&(-k));
		}
		return rt;
	}
};
fenwick fen;

	int Get(){
		int x; cin>>x; return x;
	}	

	int PAR(int v,int k){
		while(k>0){
			v=par[__builtin_ctz(k)][v];
			k-=(k&(-k));
		}
		return v;
	}

	int LCA(int u,int v){
		if(h[u]<h[v]) swap(u,v);
		u=PAR(u,h[u]-h[v]);
		if(u==v) return u;
		FORR(j,lg-1,0) if(par[j][u]!=par[j][v]){
			u=par[j][u];
			v=par[j][v];
		}
		return par[0][v];
	}

	void upd(int id,int l,int r,int ind,int x){
		if(ind<l||ind>=r) return;
		seg[id]=x;
		if(l+1==r) return;
		dmid;
		upd(lc,l,mid,ind,x);
		upd(rc,mid,r,ind,x);
	}

	int get(int id,int l,int r,int s,int e){
		if(s>=r||e<=l) return 0;
		if(s<=l&&e>=r) return seg[id];
		dmid;
		return max(get(lc,l,mid,s,e),get(rc,mid,r,s,e));
	}

	ll GET(int u){
		vector<pii> vc;
		int root=1;
		while(1){	
			int w=ver[get(1,1,n+1,st[root],en[root]+1)];
			int y=val[w];
			w=LCA(u,w);
			vc.pb({y,h[w]-h[root]+1});
			if(w==u) break;
			root=PAR(u,h[u]-h[w]-1);
		}
		//for(pii x:vc) cout<<x.F<<"."<<x.S<<" ";
		//cout<<'\n';
		ll rt=0;
		for(pii x:vc){
			rt+=fen.get(x.F+1)*x.S;
			fen.add(x.F,x.S);
		}
		for(pii x:vc) fen.add(x.F,-x.S);
		return rt;
	}

	void dfs(int v){
		st[v]=++tme;
		for(int u:adj[v]) dfs(u);
		en[v]=tme;
	}

	int32_t main(){
	iostream::sync_with_stdio(0); cin.tie(0);

	n=Get();
	FOR(i,1,n) val[i]=Get();
	vector<pii> vc;
	FOR(i,1,n) vc.pb({val[i],i});
	sort(all(vc)); int x0=0;
	FOR(i,0,SZ(vc)-1){
		if(i==0||vc[i].F!=vc[i-1].F) x0++;
		val[vc[i].S]=x0;
	}
	int U[n],V[n];
	FOR(i,1,n-1){
		U[i]=Get(); V[i]=Get();
		int u=U[i],v=V[i];
		adj[u].pb(v);
		par[0][v]=u;
		h[v]=h[u]+1;
	}
	FOR(j,1,lg-1) FOR(i,1,n) par[j][i]=par[j-1][par[j-1][i]];
	dfs(1);
	upd(1,1,n+1,st[1],1); ver[1]=1;
	FOR(i,1,n-1){
		cout<<GET(U[i])<<'\n';
		upd(1,1,n+1,st[V[i]],i+1); ver[i+1]=V[i];
	}

	return 0;
	}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2800 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 3 ms 2900 KB Output is correct
8 Correct 2 ms 2812 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 2900 KB Output is correct
13 Correct 2 ms 2812 KB Output is correct
14 Correct 2 ms 2900 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 3 ms 2812 KB Output is correct
17 Correct 3 ms 2900 KB Output is correct
18 Correct 3 ms 2900 KB Output is correct
19 Correct 2 ms 2812 KB Output is correct
20 Correct 2 ms 2900 KB Output is correct
21 Correct 2 ms 2900 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 3 ms 2900 KB Output is correct
25 Correct 4 ms 2772 KB Output is correct
26 Correct 2 ms 2900 KB Output is correct
27 Correct 2 ms 2900 KB Output is correct
28 Correct 2 ms 2900 KB Output is correct
29 Correct 2 ms 2900 KB Output is correct
30 Correct 4 ms 2772 KB Output is correct
31 Correct 3 ms 2900 KB Output is correct
32 Correct 3 ms 2900 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2900 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 2 ms 2804 KB Output is correct
38 Correct 2 ms 2804 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 2 ms 2804 KB Output is correct
43 Correct 2 ms 2900 KB Output is correct
44 Correct 2 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2800 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 3 ms 2900 KB Output is correct
8 Correct 2 ms 2812 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 2900 KB Output is correct
13 Correct 2 ms 2812 KB Output is correct
14 Correct 2 ms 2900 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 3 ms 2812 KB Output is correct
17 Correct 3 ms 2900 KB Output is correct
18 Correct 3 ms 2900 KB Output is correct
19 Correct 2 ms 2812 KB Output is correct
20 Correct 2 ms 2900 KB Output is correct
21 Correct 2 ms 2900 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 3 ms 2900 KB Output is correct
25 Correct 4 ms 2772 KB Output is correct
26 Correct 2 ms 2900 KB Output is correct
27 Correct 2 ms 2900 KB Output is correct
28 Correct 2 ms 2900 KB Output is correct
29 Correct 2 ms 2900 KB Output is correct
30 Correct 4 ms 2772 KB Output is correct
31 Correct 3 ms 2900 KB Output is correct
32 Correct 3 ms 2900 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2900 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 2 ms 2804 KB Output is correct
38 Correct 2 ms 2804 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 2 ms 2804 KB Output is correct
43 Correct 2 ms 2900 KB Output is correct
44 Correct 2 ms 2900 KB Output is correct
45 Correct 3 ms 2900 KB Output is correct
46 Correct 9 ms 3456 KB Output is correct
47 Correct 9 ms 3412 KB Output is correct
48 Correct 9 ms 3412 KB Output is correct
49 Correct 5 ms 3540 KB Output is correct
50 Correct 5 ms 3540 KB Output is correct
51 Correct 5 ms 3584 KB Output is correct
52 Correct 5 ms 3412 KB Output is correct
53 Correct 5 ms 3412 KB Output is correct
54 Correct 5 ms 3456 KB Output is correct
55 Correct 5 ms 3452 KB Output is correct
56 Correct 5 ms 3412 KB Output is correct
57 Correct 14 ms 3440 KB Output is correct
58 Correct 14 ms 3412 KB Output is correct
59 Correct 14 ms 3448 KB Output is correct
60 Correct 15 ms 3456 KB Output is correct
61 Correct 5 ms 3412 KB Output is correct
62 Correct 7 ms 3412 KB Output is correct
63 Correct 6 ms 3412 KB Output is correct
64 Correct 11 ms 3328 KB Output is correct
65 Correct 11 ms 3404 KB Output is correct
66 Correct 9 ms 3412 KB Output is correct
67 Correct 10 ms 3412 KB Output is correct
68 Correct 4 ms 3540 KB Output is correct
69 Correct 5 ms 3440 KB Output is correct
70 Correct 5 ms 3412 KB Output is correct
71 Correct 5 ms 3412 KB Output is correct
72 Correct 13 ms 3328 KB Output is correct
73 Correct 14 ms 3412 KB Output is correct
74 Correct 5 ms 3412 KB Output is correct
75 Correct 6 ms 3392 KB Output is correct
76 Correct 5 ms 3412 KB Output is correct
77 Correct 5 ms 3416 KB Output is correct
78 Correct 5 ms 3336 KB Output is correct
79 Correct 7 ms 3416 KB Output is correct
80 Correct 6 ms 3416 KB Output is correct
81 Correct 6 ms 3416 KB Output is correct
82 Correct 6 ms 3416 KB Output is correct
83 Correct 5 ms 3416 KB Output is correct
84 Correct 5 ms 3388 KB Output is correct
85 Correct 6 ms 3324 KB Output is correct
86 Correct 5 ms 3412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2800 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 3 ms 2900 KB Output is correct
8 Correct 2 ms 2812 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 2900 KB Output is correct
13 Correct 2 ms 2812 KB Output is correct
14 Correct 2 ms 2900 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 3 ms 2812 KB Output is correct
17 Correct 3 ms 2900 KB Output is correct
18 Correct 3 ms 2900 KB Output is correct
19 Correct 2 ms 2812 KB Output is correct
20 Correct 2 ms 2900 KB Output is correct
21 Correct 2 ms 2900 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 3 ms 2900 KB Output is correct
25 Correct 4 ms 2772 KB Output is correct
26 Correct 2 ms 2900 KB Output is correct
27 Correct 2 ms 2900 KB Output is correct
28 Correct 2 ms 2900 KB Output is correct
29 Correct 2 ms 2900 KB Output is correct
30 Correct 4 ms 2772 KB Output is correct
31 Correct 3 ms 2900 KB Output is correct
32 Correct 3 ms 2900 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2900 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 2 ms 2804 KB Output is correct
38 Correct 2 ms 2804 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 2 ms 2804 KB Output is correct
43 Correct 2 ms 2900 KB Output is correct
44 Correct 2 ms 2900 KB Output is correct
45 Correct 3 ms 2900 KB Output is correct
46 Correct 9 ms 3456 KB Output is correct
47 Correct 9 ms 3412 KB Output is correct
48 Correct 9 ms 3412 KB Output is correct
49 Correct 5 ms 3540 KB Output is correct
50 Correct 5 ms 3540 KB Output is correct
51 Correct 5 ms 3584 KB Output is correct
52 Correct 5 ms 3412 KB Output is correct
53 Correct 5 ms 3412 KB Output is correct
54 Correct 5 ms 3456 KB Output is correct
55 Correct 5 ms 3452 KB Output is correct
56 Correct 5 ms 3412 KB Output is correct
57 Correct 14 ms 3440 KB Output is correct
58 Correct 14 ms 3412 KB Output is correct
59 Correct 14 ms 3448 KB Output is correct
60 Correct 15 ms 3456 KB Output is correct
61 Correct 5 ms 3412 KB Output is correct
62 Correct 7 ms 3412 KB Output is correct
63 Correct 6 ms 3412 KB Output is correct
64 Correct 11 ms 3328 KB Output is correct
65 Correct 11 ms 3404 KB Output is correct
66 Correct 9 ms 3412 KB Output is correct
67 Correct 10 ms 3412 KB Output is correct
68 Correct 4 ms 3540 KB Output is correct
69 Correct 5 ms 3440 KB Output is correct
70 Correct 5 ms 3412 KB Output is correct
71 Correct 5 ms 3412 KB Output is correct
72 Correct 13 ms 3328 KB Output is correct
73 Correct 14 ms 3412 KB Output is correct
74 Correct 5 ms 3412 KB Output is correct
75 Correct 6 ms 3392 KB Output is correct
76 Correct 5 ms 3412 KB Output is correct
77 Correct 5 ms 3416 KB Output is correct
78 Correct 5 ms 3336 KB Output is correct
79 Correct 7 ms 3416 KB Output is correct
80 Correct 6 ms 3416 KB Output is correct
81 Correct 6 ms 3416 KB Output is correct
82 Correct 6 ms 3416 KB Output is correct
83 Correct 5 ms 3416 KB Output is correct
84 Correct 5 ms 3388 KB Output is correct
85 Correct 6 ms 3324 KB Output is correct
86 Correct 5 ms 3412 KB Output is correct
87 Correct 23 ms 4448 KB Output is correct
88 Correct 80 ms 7628 KB Output is correct
89 Correct 528 ms 19352 KB Output is correct
90 Correct 500 ms 19440 KB Output is correct
91 Correct 451 ms 19352 KB Output is correct
92 Correct 82 ms 23500 KB Output is correct
93 Correct 78 ms 23460 KB Output is correct
94 Correct 74 ms 23488 KB Output is correct
95 Correct 124 ms 21176 KB Output is correct
96 Correct 134 ms 21428 KB Output is correct
97 Correct 136 ms 21408 KB Output is correct
98 Correct 128 ms 21524 KB Output is correct
99 Correct 116 ms 20812 KB Output is correct
100 Correct 892 ms 19696 KB Output is correct
101 Correct 1014 ms 19872 KB Output is correct
102 Correct 921 ms 19908 KB Output is correct
103 Correct 906 ms 19904 KB Output is correct
104 Correct 175 ms 20752 KB Output is correct
105 Correct 163 ms 20680 KB Output is correct
106 Correct 158 ms 20748 KB Output is correct
107 Correct 419 ms 18184 KB Output is correct
108 Correct 460 ms 18272 KB Output is correct
109 Correct 529 ms 18788 KB Output is correct
110 Correct 77 ms 22428 KB Output is correct
111 Correct 120 ms 21120 KB Output is correct
112 Correct 112 ms 20440 KB Output is correct
113 Correct 113 ms 19656 KB Output is correct
114 Correct 958 ms 19492 KB Output is correct
115 Correct 913 ms 18864 KB Output is correct
116 Correct 157 ms 19728 KB Output is correct
117 Correct 123 ms 19764 KB Output is correct
118 Correct 121 ms 19472 KB Output is correct
119 Correct 111 ms 19352 KB Output is correct
120 Correct 109 ms 18972 KB Output is correct
121 Correct 118 ms 18756 KB Output is correct
122 Correct 117 ms 18692 KB Output is correct
123 Correct 200 ms 19752 KB Output is correct
124 Correct 216 ms 19528 KB Output is correct
125 Correct 210 ms 19420 KB Output is correct
126 Correct 198 ms 19100 KB Output is correct
127 Correct 198 ms 18872 KB Output is correct
128 Correct 190 ms 18776 KB Output is correct