Submission #360040

# Submission time Handle Problem Language Result Execution time Memory
360040 2021-01-27T12:22:26 Z keta_tsimakuridze Construction of Highway (JOI18_construction) C++14
100 / 100
1462 ms 20844 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
const int N=1e5+5;
int sz[N],par[N],lazy[4*N],ind[N],c[N],cur,idx,k,i,a[N],b[N],n,st[N],f[N],ch[N],s[N];
pair<int,int>d[N],tree[4*N];
vector<int>V[N];
void dfs(int u,int p){
	sz[u]=1; par[u]=p;
	for(int i=0;i<V[u].size();i++){
		if(V[u][i]!=p) dfs(V[u][i],u),sz[u]+=sz[V[u][i]];
	}
}
 
void update(int u,int start,int end,int l,int r,int val){
	if(lazy[u]){
		tree[u]={lazy[u],lazy[u]};
		if(l!=r){
			lazy[2*u]=lazy[u];
			lazy[2*u+1]=lazy[u];
		}
		lazy[u]=0;
	}
	if(l>end || r<start) return;
	if(start<=l && r<=end) {
		tree[u]={val,val};
		if(l!=r){
			lazy[2*u]=val;
			lazy[2*u+1]=val;
		}
		return;
	}
	int mid=(l+r)/2;
	update(2*u,start,end,l,mid,val);
	update(2*u+1,start,end,mid+1,r,val);
	tree[u].f=min(tree[2*u].f,tree[2*u+1].f);
	tree[u].s=max(tree[2*u].s,tree[2*u+1].s);
}
pair<int,int> getans(int u,int start,int end,int l,int r){
	if(lazy[u]){
		tree[u]={lazy[u],lazy[u]};
		if(l!=r){
			lazy[2*u]=lazy[u];
			lazy[2*u+1]=lazy[u];
		}
		lazy[u]=0;
	}
	if(l>end || r<start) return {n+1,0};
	if(start<=l && r<=end) {
		return tree[u];
	}
	int mid=(l+r)/2;
	pair<int,int> g1=getans(2*u,start,end,l,mid);
	pair<int,int> g2=getans(2*u+1,start,end,mid+1,r);
	return {min(g1.f,g2.f),max(g1.s,g2.s)};
}
void hld(int u,int p){
	if(!st[cur]) st[cur]=u;
	idx++;
	ind[u]=idx; s[idx]=u;
	ch[u]=cur;
	int mx=0,v=0;
	for(int i=0;i<V[u].size();i++){
		if(V[u][i]==p) continue;
		if(mx<sz[V[u][i]]) {
			mx=sz[V[u][i]];
			v=V[u][i];
		}
	}
	if(v) hld(v,u);
	for(int i=0;i<V[u].size();i++){
		if(V[u][i]==p || V[u][i]==v) continue;
		cur++;
		hld(V[u][i],u);
	}
}
void update(int ind,int val){
	for(ind;ind<=n;ind+=ind&(-ind))
	f[ind]+=val;
}
ll getans(int ind){
	ll pas=0;
	for(ind;ind>=1;ind-=ind&(-ind))
	pas+=f[ind];
	return pas;
}
ll upd(int u,int val){
	vector<pair<int,int> >v;
	long long ans = 0,U=u;
	update(1,ind[u],ind[u],1,n,val);
	u=par[u];
	while(u){
		int l=ind[st[ch[u]]],r=ind[u],col=0;
 
		while(l<=r){
			int mid=(l+r)/2;
			pair<int,int> g=getans(1,mid,ind[u],1,n);
			if(g.first==g.second) {
				idx=mid; col=g.first; r=mid-1; 
			}
			else l=mid+1;
		} 
		update(1,idx,ind[u],1,n,val);
		ans += getans(col-1) *(ll)(-idx+ind[u]+1);
		update(col,-idx+ind[u]+1);
		v.push_back({col,-idx+ind[u]+1});
		u=par[s[idx]];
	}
	for(int i=0;i<v.size();i++){
		update(v[i].f,-v[i].s);
	}
	return ans;
}
 main(){
 	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n;
	for(k=1;k<=n;k++){
		cin>>c[k]; d[k]={c[k],k};
	}
	sort(d+1,d+n+1);
	cur=0;
	for(k=1;k<=n;k++){
		if(d[k].f!=d[k-1].f) cur++;
		c[d[k].second]=cur;
	}
	cur=1;
	for(k=1;k<n;k++){
		cin>>a[k]>>b[k];
		V[a[k]].push_back(b[k]);
		V[b[k]].push_back(a[k]);
	}
	dfs(1,0);
	hld(1,0);
	update(1,ind[1],ind[1],1,n,c[1]);
	for(k=1;k<n;k++){
		cout<<upd(b[k],c[b[k]])<<" ";
		
	}
}

Compilation message

construction.cpp: In function 'void dfs(int, int)':
construction.cpp:12:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
construction.cpp: In function 'void hld(int, int)':
construction.cpp:65:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
construction.cpp:73:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
construction.cpp: In function 'void update(int, int)':
construction.cpp:80:6: warning: statement has no effect [-Wunused-value]
   80 |  for(ind;ind<=n;ind+=ind&(-ind))
      |      ^~~
construction.cpp: In function 'long long int getans(int)':
construction.cpp:85:6: warning: statement has no effect [-Wunused-value]
   85 |  for(ind;ind>=1;ind-=ind&(-ind))
      |      ^~~
construction.cpp: In function 'long long int upd(int, int)':
construction.cpp:111:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |  for(int i=0;i<v.size();i++){
      |              ~^~~~~~~~~
construction.cpp:91:20: warning: unused variable 'U' [-Wunused-variable]
   91 |  long long ans = 0,U=u;
      |                    ^
construction.cpp: At global scope:
construction.cpp:116:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  116 |  main(){
      |       ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 3 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 3 ms 2796 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 4 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 4 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2924 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 4 ms 2796 KB Output is correct
28 Correct 4 ms 2796 KB Output is correct
29 Correct 4 ms 2796 KB Output is correct
30 Correct 4 ms 2796 KB Output is correct
31 Correct 5 ms 2828 KB Output is correct
32 Correct 4 ms 2796 KB Output is correct
33 Correct 4 ms 2796 KB Output is correct
34 Correct 4 ms 2944 KB Output is correct
35 Correct 5 ms 2796 KB Output is correct
36 Correct 4 ms 2796 KB Output is correct
37 Correct 4 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 3 ms 2796 KB Output is correct
40 Correct 3 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 4 ms 2816 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 3 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 3 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 3 ms 2796 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 4 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 4 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2924 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 4 ms 2796 KB Output is correct
28 Correct 4 ms 2796 KB Output is correct
29 Correct 4 ms 2796 KB Output is correct
30 Correct 4 ms 2796 KB Output is correct
31 Correct 5 ms 2828 KB Output is correct
32 Correct 4 ms 2796 KB Output is correct
33 Correct 4 ms 2796 KB Output is correct
34 Correct 4 ms 2944 KB Output is correct
35 Correct 5 ms 2796 KB Output is correct
36 Correct 4 ms 2796 KB Output is correct
37 Correct 4 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 3 ms 2796 KB Output is correct
40 Correct 3 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 4 ms 2816 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 3 ms 2796 KB Output is correct
45 Correct 5 ms 2924 KB Output is correct
46 Correct 16 ms 3308 KB Output is correct
47 Correct 17 ms 3308 KB Output is correct
48 Correct 16 ms 3308 KB Output is correct
49 Correct 19 ms 3564 KB Output is correct
50 Correct 17 ms 3436 KB Output is correct
51 Correct 17 ms 3436 KB Output is correct
52 Correct 20 ms 3308 KB Output is correct
53 Correct 21 ms 3436 KB Output is correct
54 Correct 21 ms 3436 KB Output is correct
55 Correct 21 ms 3436 KB Output is correct
56 Correct 21 ms 3308 KB Output is correct
57 Correct 26 ms 3180 KB Output is correct
58 Correct 26 ms 3200 KB Output is correct
59 Correct 26 ms 3308 KB Output is correct
60 Correct 26 ms 3180 KB Output is correct
61 Correct 23 ms 3308 KB Output is correct
62 Correct 23 ms 3308 KB Output is correct
63 Correct 23 ms 3328 KB Output is correct
64 Correct 14 ms 3180 KB Output is correct
65 Correct 16 ms 3180 KB Output is correct
66 Correct 17 ms 3180 KB Output is correct
67 Correct 16 ms 3180 KB Output is correct
68 Correct 17 ms 3436 KB Output is correct
69 Correct 20 ms 3308 KB Output is correct
70 Correct 20 ms 3308 KB Output is correct
71 Correct 21 ms 3308 KB Output is correct
72 Correct 26 ms 3180 KB Output is correct
73 Correct 26 ms 3180 KB Output is correct
74 Correct 22 ms 3308 KB Output is correct
75 Correct 20 ms 3308 KB Output is correct
76 Correct 19 ms 3308 KB Output is correct
77 Correct 18 ms 3308 KB Output is correct
78 Correct 20 ms 3308 KB Output is correct
79 Correct 19 ms 3308 KB Output is correct
80 Correct 18 ms 3180 KB Output is correct
81 Correct 22 ms 3308 KB Output is correct
82 Correct 21 ms 3308 KB Output is correct
83 Correct 25 ms 3308 KB Output is correct
84 Correct 22 ms 3308 KB Output is correct
85 Correct 23 ms 3180 KB Output is correct
86 Correct 26 ms 3180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 3 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 3 ms 2796 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 4 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 4 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2924 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 4 ms 2796 KB Output is correct
28 Correct 4 ms 2796 KB Output is correct
29 Correct 4 ms 2796 KB Output is correct
30 Correct 4 ms 2796 KB Output is correct
31 Correct 5 ms 2828 KB Output is correct
32 Correct 4 ms 2796 KB Output is correct
33 Correct 4 ms 2796 KB Output is correct
34 Correct 4 ms 2944 KB Output is correct
35 Correct 5 ms 2796 KB Output is correct
36 Correct 4 ms 2796 KB Output is correct
37 Correct 4 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 3 ms 2796 KB Output is correct
40 Correct 3 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 4 ms 2816 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 3 ms 2796 KB Output is correct
45 Correct 5 ms 2924 KB Output is correct
46 Correct 16 ms 3308 KB Output is correct
47 Correct 17 ms 3308 KB Output is correct
48 Correct 16 ms 3308 KB Output is correct
49 Correct 19 ms 3564 KB Output is correct
50 Correct 17 ms 3436 KB Output is correct
51 Correct 17 ms 3436 KB Output is correct
52 Correct 20 ms 3308 KB Output is correct
53 Correct 21 ms 3436 KB Output is correct
54 Correct 21 ms 3436 KB Output is correct
55 Correct 21 ms 3436 KB Output is correct
56 Correct 21 ms 3308 KB Output is correct
57 Correct 26 ms 3180 KB Output is correct
58 Correct 26 ms 3200 KB Output is correct
59 Correct 26 ms 3308 KB Output is correct
60 Correct 26 ms 3180 KB Output is correct
61 Correct 23 ms 3308 KB Output is correct
62 Correct 23 ms 3308 KB Output is correct
63 Correct 23 ms 3328 KB Output is correct
64 Correct 14 ms 3180 KB Output is correct
65 Correct 16 ms 3180 KB Output is correct
66 Correct 17 ms 3180 KB Output is correct
67 Correct 16 ms 3180 KB Output is correct
68 Correct 17 ms 3436 KB Output is correct
69 Correct 20 ms 3308 KB Output is correct
70 Correct 20 ms 3308 KB Output is correct
71 Correct 21 ms 3308 KB Output is correct
72 Correct 26 ms 3180 KB Output is correct
73 Correct 26 ms 3180 KB Output is correct
74 Correct 22 ms 3308 KB Output is correct
75 Correct 20 ms 3308 KB Output is correct
76 Correct 19 ms 3308 KB Output is correct
77 Correct 18 ms 3308 KB Output is correct
78 Correct 20 ms 3308 KB Output is correct
79 Correct 19 ms 3308 KB Output is correct
80 Correct 18 ms 3180 KB Output is correct
81 Correct 22 ms 3308 KB Output is correct
82 Correct 21 ms 3308 KB Output is correct
83 Correct 25 ms 3308 KB Output is correct
84 Correct 22 ms 3308 KB Output is correct
85 Correct 23 ms 3180 KB Output is correct
86 Correct 26 ms 3180 KB Output is correct
87 Correct 46 ms 4188 KB Output is correct
88 Correct 159 ms 6508 KB Output is correct
89 Correct 812 ms 14316 KB Output is correct
90 Correct 808 ms 14316 KB Output is correct
91 Correct 810 ms 14444 KB Output is correct
92 Correct 796 ms 20352 KB Output is correct
93 Correct 786 ms 20332 KB Output is correct
94 Correct 789 ms 20460 KB Output is correct
95 Correct 973 ms 17740 KB Output is correct
96 Correct 995 ms 17512 KB Output is correct
97 Correct 972 ms 17648 KB Output is correct
98 Correct 972 ms 17768 KB Output is correct
99 Correct 1062 ms 17340 KB Output is correct
100 Correct 1397 ms 13460 KB Output is correct
101 Correct 1413 ms 13308 KB Output is correct
102 Correct 1422 ms 13292 KB Output is correct
103 Correct 1462 ms 14828 KB Output is correct
104 Correct 1158 ms 18744 KB Output is correct
105 Correct 1199 ms 18796 KB Output is correct
106 Correct 1181 ms 18540 KB Output is correct
107 Correct 689 ms 14636 KB Output is correct
108 Correct 797 ms 15112 KB Output is correct
109 Correct 831 ms 15292 KB Output is correct
110 Correct 854 ms 20844 KB Output is correct
111 Correct 1093 ms 18688 KB Output is correct
112 Correct 1022 ms 18140 KB Output is correct
113 Correct 1020 ms 17660 KB Output is correct
114 Correct 1429 ms 14572 KB Output is correct
115 Correct 1392 ms 14572 KB Output is correct
116 Correct 1142 ms 17556 KB Output is correct
117 Correct 950 ms 16780 KB Output is correct
118 Correct 926 ms 16236 KB Output is correct
119 Correct 892 ms 15964 KB Output is correct
120 Correct 947 ms 15980 KB Output is correct
121 Correct 920 ms 15468 KB Output is correct
122 Correct 918 ms 15212 KB Output is correct
123 Correct 1123 ms 16744 KB Output is correct
124 Correct 1095 ms 16272 KB Output is correct
125 Correct 1059 ms 16112 KB Output is correct
126 Correct 1125 ms 16284 KB Output is correct
127 Correct 1114 ms 15608 KB Output is correct
128 Correct 1016 ms 15468 KB Output is correct