Submission #914039

# Submission time Handle Problem Language Result Execution time Memory
914039 2024-01-20T21:32:00 Z amirhoseinfar1385 Construction of Highway (JOI18_construction) C++17
100 / 100
343 ms 26608 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
vector<int>adj[maxn];
int all[maxn],part[maxn],parh[maxn],high[maxn],sz[maxn],n,timea=0;
pair<int,int>stf[maxn];
vector<pair<int,int>>val[maxn];
pair<int,int>alle[maxn];

bool cmp(int a,int b){
	return sz[a]>sz[b];
}

struct fen{
	int fn[maxn];
	vector<int>allv;
	void clear(){
		for(auto x:allv){
			fn[x]=0;
		}
		allv.clear();
	}
	void upd(int i,int w){
		//cout<<"wtf inf: "<<i<<" "<<w<<endl;
		i++;
		while(i<maxn){
			allv.push_back(i);
			fn[i]+=w;
			i+=((-i)&i);
		}
	}
	int pors(int i){
		//cout<<"tof pi: "<<i<<" ";
		i++;
		int ret=0;
		while(i>0){
			ret+=fn[i];
			i-=((-i)&i);
		}
		//cout<<ret<<endl;
		return ret;
	}
}fn;

void pre(int u=1,int par=1){
	if(u!=1){
		sort(adj[u].begin(),adj[u].end());
		adj[u].erase(lower_bound(adj[u].begin(),adj[u].end(),par));
	}
	part[u]=par;
	sz[u]=1;
	for(auto x:adj[u]){
		high[x]=high[u]+1;
		pre(x,u);
		sz[u]+=sz[x];
	}
	sort(adj[u].begin(),adj[u].end(),cmp);
}

void dfs(int u=1,int par=1){
	timea++;
	stf[u].first=timea;
	parh[u]=par;
	if(adj[u].size()>0){
		dfs(adj[u][0],par);
	}
	for(int i=1;i<(int)adj[u].size();i++){
		dfs(adj[u][i],adj[u][i]);
	}
	stf[u].second=timea;
}

void upd(int u,int w){
	//cout<<u<<" "<<w<<" "<<parh[u]<<endl;
	int ted=high[u]-high[parh[u]]+1;
	u=parh[u];
	int now=0;
	while((int)val[u].size()>0){
		if(val[u].back().first+now<=ted){
			now+=val[u].back().first;
			val[u].pop_back();
			continue;
		}
		val[u].back().first-=ted-now;
		break;
	}
	val[u].push_back(make_pair(ted,w));
	if(u==1){
		return ;
	}
	return upd(part[u],w);
}

int get(int u){
//	//cout<<"inget: "<<u<<endl;
	int ted=high[u]-high[parh[u]]+1;
	int now=0;
	int ph=parh[u];
	if((int)val[ph].size()==0){
		if(ph==1){
			return 0;
		}
		return get(part[parh[u]]);
	}
	u=parh[u];
	vector<pair<int,int>>tof;
	for(int i=(int)val[u].size()-1;i>=0;i--){
		if(val[u][i].first+now<=ted){
			tof.push_back(val[u][i]);
			now+=val[u][i].first;
		}
		else{
			tof.push_back(make_pair(ted-now,val[u][i].second));
			break;
		}
	}
//	//cout<<"injy: "<<u<<endl;
	reverse(tof.begin(),tof.end());
	long long ret=0;
	for(auto x:tof){
		ret+=x.first*fn.pors(x.second-1);
		fn.upd(x.second,x.first);
	}
//	//cout<<"oonjy: "<<u<<endl;
	if(u==1){
		return ret;
	}
	ret+=get(part[parh[u]]);
	return ret;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	//cout.tie(0);
	cin>>n;
	vector<int>allind;
	for(int i=1;i<=n;i++){
		cin>>all[i];
		allind.push_back(all[i]);
	}	
	sort(allind.begin(),allind.end());
	allind.resize(unique(allind.begin(),allind.end())-allind.begin());
	for(int i=1;i<=n;i++){
		all[i]=lower_bound(allind.begin(),allind.end(),all[i])-allind.begin();
	}
	for(int i=0;i<n-1;i++){
		cin>>alle[i].first>>alle[i].second;
		adj[alle[i].first].push_back(alle[i].second);
		adj[alle[i].second].push_back(alle[i].first);
	}
	pre();
	dfs();
	upd(1,all[1]);
	////cout<<"sa;am"<<endl;
	for(int i=0;i<n-1;i++){
		if(high[alle[i].first]>high[alle[i].second]){
			swap(alle[i].first,alle[i].second);
		}
		cout<<get(alle[i].first)<<endl;
//		//cout<<"wtf"<<endl;
		upd(alle[i].second,all[alle[i].second]);
//		//cout<<"radshod"<<endl;
		fn.clear();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 3 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 4 ms 8796 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8796 KB Output is correct
13 Correct 3 ms 8796 KB Output is correct
14 Correct 4 ms 8948 KB Output is correct
15 Correct 3 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 3 ms 8796 KB Output is correct
18 Correct 4 ms 8796 KB Output is correct
19 Correct 3 ms 8796 KB Output is correct
20 Correct 3 ms 9304 KB Output is correct
21 Correct 3 ms 8796 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 3 ms 8796 KB Output is correct
24 Correct 4 ms 8796 KB Output is correct
25 Correct 4 ms 8884 KB Output is correct
26 Correct 3 ms 8796 KB Output is correct
27 Correct 3 ms 8972 KB Output is correct
28 Correct 3 ms 8796 KB Output is correct
29 Correct 3 ms 8828 KB Output is correct
30 Correct 4 ms 8796 KB Output is correct
31 Correct 4 ms 8904 KB Output is correct
32 Correct 3 ms 8796 KB Output is correct
33 Correct 3 ms 8796 KB Output is correct
34 Correct 3 ms 8796 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 3 ms 8796 KB Output is correct
37 Correct 3 ms 8796 KB Output is correct
38 Correct 3 ms 8796 KB Output is correct
39 Correct 3 ms 8796 KB Output is correct
40 Correct 4 ms 8796 KB Output is correct
41 Correct 3 ms 8796 KB Output is correct
42 Correct 3 ms 8796 KB Output is correct
43 Correct 4 ms 8796 KB Output is correct
44 Correct 3 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 3 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 4 ms 8796 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8796 KB Output is correct
13 Correct 3 ms 8796 KB Output is correct
14 Correct 4 ms 8948 KB Output is correct
15 Correct 3 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 3 ms 8796 KB Output is correct
18 Correct 4 ms 8796 KB Output is correct
19 Correct 3 ms 8796 KB Output is correct
20 Correct 3 ms 9304 KB Output is correct
21 Correct 3 ms 8796 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 3 ms 8796 KB Output is correct
24 Correct 4 ms 8796 KB Output is correct
25 Correct 4 ms 8884 KB Output is correct
26 Correct 3 ms 8796 KB Output is correct
27 Correct 3 ms 8972 KB Output is correct
28 Correct 3 ms 8796 KB Output is correct
29 Correct 3 ms 8828 KB Output is correct
30 Correct 4 ms 8796 KB Output is correct
31 Correct 4 ms 8904 KB Output is correct
32 Correct 3 ms 8796 KB Output is correct
33 Correct 3 ms 8796 KB Output is correct
34 Correct 3 ms 8796 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 3 ms 8796 KB Output is correct
37 Correct 3 ms 8796 KB Output is correct
38 Correct 3 ms 8796 KB Output is correct
39 Correct 3 ms 8796 KB Output is correct
40 Correct 4 ms 8796 KB Output is correct
41 Correct 3 ms 8796 KB Output is correct
42 Correct 3 ms 8796 KB Output is correct
43 Correct 4 ms 8796 KB Output is correct
44 Correct 3 ms 8796 KB Output is correct
45 Correct 4 ms 8796 KB Output is correct
46 Correct 12 ms 9132 KB Output is correct
47 Correct 11 ms 9052 KB Output is correct
48 Correct 11 ms 9052 KB Output is correct
49 Correct 10 ms 9308 KB Output is correct
50 Correct 9 ms 9420 KB Output is correct
51 Correct 11 ms 9308 KB Output is correct
52 Correct 9 ms 9564 KB Output is correct
53 Correct 9 ms 9496 KB Output is correct
54 Correct 9 ms 9564 KB Output is correct
55 Correct 9 ms 9424 KB Output is correct
56 Correct 9 ms 9308 KB Output is correct
57 Correct 13 ms 9052 KB Output is correct
58 Correct 12 ms 9052 KB Output is correct
59 Correct 12 ms 9052 KB Output is correct
60 Correct 12 ms 9052 KB Output is correct
61 Correct 13 ms 9344 KB Output is correct
62 Correct 9 ms 9308 KB Output is correct
63 Correct 9 ms 9168 KB Output is correct
64 Correct 10 ms 9168 KB Output is correct
65 Correct 11 ms 9052 KB Output is correct
66 Correct 11 ms 9052 KB Output is correct
67 Correct 12 ms 9052 KB Output is correct
68 Correct 9 ms 9308 KB Output is correct
69 Correct 10 ms 9424 KB Output is correct
70 Correct 9 ms 9564 KB Output is correct
71 Correct 9 ms 9308 KB Output is correct
72 Correct 13 ms 9052 KB Output is correct
73 Correct 12 ms 9052 KB Output is correct
74 Correct 9 ms 9168 KB Output is correct
75 Correct 9 ms 9052 KB Output is correct
76 Correct 9 ms 9296 KB Output is correct
77 Correct 9 ms 9232 KB Output is correct
78 Correct 9 ms 9140 KB Output is correct
79 Correct 10 ms 9052 KB Output is correct
80 Correct 9 ms 9052 KB Output is correct
81 Correct 10 ms 9052 KB Output is correct
82 Correct 9 ms 9052 KB Output is correct
83 Correct 9 ms 9172 KB Output is correct
84 Correct 11 ms 9052 KB Output is correct
85 Correct 13 ms 9088 KB Output is correct
86 Correct 9 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 3 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 4 ms 8796 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8796 KB Output is correct
13 Correct 3 ms 8796 KB Output is correct
14 Correct 4 ms 8948 KB Output is correct
15 Correct 3 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 3 ms 8796 KB Output is correct
18 Correct 4 ms 8796 KB Output is correct
19 Correct 3 ms 8796 KB Output is correct
20 Correct 3 ms 9304 KB Output is correct
21 Correct 3 ms 8796 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 3 ms 8796 KB Output is correct
24 Correct 4 ms 8796 KB Output is correct
25 Correct 4 ms 8884 KB Output is correct
26 Correct 3 ms 8796 KB Output is correct
27 Correct 3 ms 8972 KB Output is correct
28 Correct 3 ms 8796 KB Output is correct
29 Correct 3 ms 8828 KB Output is correct
30 Correct 4 ms 8796 KB Output is correct
31 Correct 4 ms 8904 KB Output is correct
32 Correct 3 ms 8796 KB Output is correct
33 Correct 3 ms 8796 KB Output is correct
34 Correct 3 ms 8796 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 3 ms 8796 KB Output is correct
37 Correct 3 ms 8796 KB Output is correct
38 Correct 3 ms 8796 KB Output is correct
39 Correct 3 ms 8796 KB Output is correct
40 Correct 4 ms 8796 KB Output is correct
41 Correct 3 ms 8796 KB Output is correct
42 Correct 3 ms 8796 KB Output is correct
43 Correct 4 ms 8796 KB Output is correct
44 Correct 3 ms 8796 KB Output is correct
45 Correct 4 ms 8796 KB Output is correct
46 Correct 12 ms 9132 KB Output is correct
47 Correct 11 ms 9052 KB Output is correct
48 Correct 11 ms 9052 KB Output is correct
49 Correct 10 ms 9308 KB Output is correct
50 Correct 9 ms 9420 KB Output is correct
51 Correct 11 ms 9308 KB Output is correct
52 Correct 9 ms 9564 KB Output is correct
53 Correct 9 ms 9496 KB Output is correct
54 Correct 9 ms 9564 KB Output is correct
55 Correct 9 ms 9424 KB Output is correct
56 Correct 9 ms 9308 KB Output is correct
57 Correct 13 ms 9052 KB Output is correct
58 Correct 12 ms 9052 KB Output is correct
59 Correct 12 ms 9052 KB Output is correct
60 Correct 12 ms 9052 KB Output is correct
61 Correct 13 ms 9344 KB Output is correct
62 Correct 9 ms 9308 KB Output is correct
63 Correct 9 ms 9168 KB Output is correct
64 Correct 10 ms 9168 KB Output is correct
65 Correct 11 ms 9052 KB Output is correct
66 Correct 11 ms 9052 KB Output is correct
67 Correct 12 ms 9052 KB Output is correct
68 Correct 9 ms 9308 KB Output is correct
69 Correct 10 ms 9424 KB Output is correct
70 Correct 9 ms 9564 KB Output is correct
71 Correct 9 ms 9308 KB Output is correct
72 Correct 13 ms 9052 KB Output is correct
73 Correct 12 ms 9052 KB Output is correct
74 Correct 9 ms 9168 KB Output is correct
75 Correct 9 ms 9052 KB Output is correct
76 Correct 9 ms 9296 KB Output is correct
77 Correct 9 ms 9232 KB Output is correct
78 Correct 9 ms 9140 KB Output is correct
79 Correct 10 ms 9052 KB Output is correct
80 Correct 9 ms 9052 KB Output is correct
81 Correct 10 ms 9052 KB Output is correct
82 Correct 9 ms 9052 KB Output is correct
83 Correct 9 ms 9172 KB Output is correct
84 Correct 11 ms 9052 KB Output is correct
85 Correct 13 ms 9088 KB Output is correct
86 Correct 9 ms 9052 KB Output is correct
87 Correct 25 ms 9684 KB Output is correct
88 Correct 75 ms 11092 KB Output is correct
89 Correct 261 ms 16596 KB Output is correct
90 Correct 263 ms 16592 KB Output is correct
91 Correct 270 ms 16516 KB Output is correct
92 Correct 188 ms 24484 KB Output is correct
93 Correct 187 ms 24212 KB Output is correct
94 Correct 186 ms 24176 KB Output is correct
95 Correct 193 ms 23800 KB Output is correct
96 Correct 191 ms 24024 KB Output is correct
97 Correct 193 ms 23932 KB Output is correct
98 Correct 200 ms 24236 KB Output is correct
99 Correct 195 ms 21240 KB Output is correct
100 Correct 309 ms 15824 KB Output is correct
101 Correct 333 ms 16340 KB Output is correct
102 Correct 343 ms 16364 KB Output is correct
103 Correct 335 ms 16448 KB Output is correct
104 Correct 202 ms 21280 KB Output is correct
105 Correct 198 ms 21204 KB Output is correct
106 Correct 194 ms 21204 KB Output is correct
107 Correct 252 ms 15692 KB Output is correct
108 Correct 273 ms 16084 KB Output is correct
109 Correct 259 ms 16052 KB Output is correct
110 Correct 173 ms 23508 KB Output is correct
111 Correct 187 ms 23756 KB Output is correct
112 Correct 182 ms 26608 KB Output is correct
113 Correct 188 ms 20480 KB Output is correct
114 Correct 302 ms 15920 KB Output is correct
115 Correct 323 ms 15512 KB Output is correct
116 Correct 195 ms 20388 KB Output is correct
117 Correct 195 ms 18544 KB Output is correct
118 Correct 198 ms 17832 KB Output is correct
119 Correct 197 ms 17364 KB Output is correct
120 Correct 191 ms 17872 KB Output is correct
121 Correct 198 ms 17364 KB Output is correct
122 Correct 193 ms 16812 KB Output is correct
123 Correct 202 ms 18660 KB Output is correct
124 Correct 205 ms 18272 KB Output is correct
125 Correct 204 ms 17460 KB Output is correct
126 Correct 194 ms 18004 KB Output is correct
127 Correct 193 ms 17340 KB Output is correct
128 Correct 199 ms 16952 KB Output is correct