답안 #712418

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
712418 2023-03-18T18:42:35 Z Antekb Construction of Highway (JOI18_construction) C++17
100 / 100
588 ms 34172 KB
#include<bits/stdc++.h>

#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pp pop_back
#define all(x) (x).begin(), (x).end()

using namespace std;

void debug(){cerr<<"\n";}
template<typename H, typename... T>
void debug(H h, T... t){
	cerr<<h;
	if(sizeof...(t))cerr<<", ";
	debug(t...);
}
#define deb(x...) cerr<<#x<<" = ";debug(x);

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<pii>;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int N=1<<17;
vi E[N];
int pre[N], sz[N], par[N], sci[N], dep[N], val[N], co[N];
int wsk;
void dfs_sz(int v){
	for(int u:E[v]){
		if(u!=par[v]){
			par[u]=v;
			dep[u]=dep[v]+1;
			dfs_sz(u);
			sz[v]+=sz[u];
		}
	}
	sz[v]+=1;
	for(int &u:E[v])if(u==par[v])swap(u, E[v].back());
	if(v!=1)E[v].pp();
	for(int &u:E[v])if(sz[u]>sz[E[v][0]])swap(u, E[v][0]);
	//deb(v, E[v].size());
}
void dfs(int v){
	co[wsk]=v;
	pre[v]=wsk++;
	for(int u:E[v]){
		//deb(v, u);
		if(u==E[v][0])sci[wsk]=sci[pre[v]];
		else{
			sci[wsk]=wsk;
			//deb(v, wsk);
		}
		dfs(u);
	}
}
set<int> S[N];
int sum[N+N];
int add(int v, int c){
	int ans=0;
	for(v+=N; v; v>>=1){
		sum[v]+=c;
		if(!(v&1)){
			ans+=sum[v+1];
		}
	}
	return ans;
}
ll calc(int v){
	vector<pair<int, int> > V;
	v=pre[v];
	int t=val[v];
	bool czy=1;
	while(true){
		int u=sci[v];
		//deb(v, u, S[u].size());
		if(S[u].size()){
			//deb(*S[u].begin());
		}
		vector<pair<int, int> > V2;
		int prv=u-1;
		while(prv<v-czy){
			//deb("a");
			//assert(S[u].size());
			int a=*S[u].begin();
			if(a>v-czy){
				V2.eb(v-czy-prv, val[a]);
			}
			else{
				V2.eb(a-prv, val[a]);
				S[u].erase(a);
			}
			prv=a;
		}
		reverse(all(V2));
		for(pii &i:V2)V.pb(i);
		S[u].insert(v);
		val[v]=t;
		if(u==0)break;
		v=pre[par[co[u]]];
		czy=0;
	}
	reverse(all(V));
	//deb(V.size());
	for(pii i:V){
		//deb(i.st, i.nd);
	}
	ll ans=0;
	for(int i=0; i<V.size(); i++){
		ans+=V[i].st*1ll*add(V[i].nd, V[i].st);
	}
	for(int i=0; i<V.size(); i++){
		add(V[i].nd, -V[i].st);
	}
	//assert(sum[1]==0);
	/*ll ans2=0;
	for(int i=0; i<V.size(); i++){
		for(int j=0; j<i; j++){
			if(V[j].nd>V[i].nd)ans2+=V[j].st*1ll*V[i].st;
		}
	}
	assert(ans==ans2);*/
	return ans;
}
int main(){
	//ios_base::sync_with_stdio(0);cin.tie(0);
	int n;
	cin>>n;
	map<int, int> M;
	vi V(n);
	for(int &i:V)cin>>i, M[i]=1;
	int wsk2=1;
	for(auto &i:M){
		i.nd=wsk2++;
	}
	vector<pii> edg(n-1);
	for(pii &i:edg)cin>>i.st>>i.nd, E[i.st].pb(i.nd), E[i.nd].pb(i.st);
	dfs_sz(1);
	dfs(1);
	for(int i=1; i<=n; i++){
		//deb(i, pre[i], par[i], val[i], sci[pre[i]], sz[i]);
	}
	S[0].insert(0);
	for(int i=1; i<=n; i++){
		val[pre[i]]=M[V[i-1]];
	}
	for(pii i:edg){
		cout<<calc(i.nd)<<"\n";
	}
}

Compilation message

construction.cpp: In function 'll calc(int)':
construction.cpp:108:10: warning: variable 'i' set but not used [-Wunused-but-set-variable]
  108 |  for(pii i:V){
      |          ^
construction.cpp:112:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |  for(int i=0; i<V.size(); i++){
      |               ~^~~~~~~~~
construction.cpp:115:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |  for(int i=0; i<V.size(); i++){
      |               ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9556 KB Output is correct
2 Correct 6 ms 9572 KB Output is correct
3 Correct 5 ms 9556 KB Output is correct
4 Correct 6 ms 9556 KB Output is correct
5 Correct 6 ms 9556 KB Output is correct
6 Correct 6 ms 9556 KB Output is correct
7 Correct 8 ms 9556 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9580 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 7 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9556 KB Output is correct
16 Correct 6 ms 9556 KB Output is correct
17 Correct 6 ms 9604 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 5 ms 9572 KB Output is correct
20 Correct 6 ms 9556 KB Output is correct
21 Correct 6 ms 9684 KB Output is correct
22 Correct 7 ms 9556 KB Output is correct
23 Correct 6 ms 9556 KB Output is correct
24 Correct 6 ms 9556 KB Output is correct
25 Correct 6 ms 9568 KB Output is correct
26 Correct 5 ms 9596 KB Output is correct
27 Correct 6 ms 9684 KB Output is correct
28 Correct 5 ms 9556 KB Output is correct
29 Correct 5 ms 9556 KB Output is correct
30 Correct 6 ms 9556 KB Output is correct
31 Correct 6 ms 9560 KB Output is correct
32 Correct 5 ms 9556 KB Output is correct
33 Correct 5 ms 9556 KB Output is correct
34 Correct 6 ms 9556 KB Output is correct
35 Correct 6 ms 9556 KB Output is correct
36 Correct 6 ms 9556 KB Output is correct
37 Correct 5 ms 9556 KB Output is correct
38 Correct 6 ms 9556 KB Output is correct
39 Correct 6 ms 9556 KB Output is correct
40 Correct 5 ms 9556 KB Output is correct
41 Correct 6 ms 9672 KB Output is correct
42 Correct 5 ms 9556 KB Output is correct
43 Correct 5 ms 9556 KB Output is correct
44 Correct 5 ms 9556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9556 KB Output is correct
2 Correct 6 ms 9572 KB Output is correct
3 Correct 5 ms 9556 KB Output is correct
4 Correct 6 ms 9556 KB Output is correct
5 Correct 6 ms 9556 KB Output is correct
6 Correct 6 ms 9556 KB Output is correct
7 Correct 8 ms 9556 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9580 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 7 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9556 KB Output is correct
16 Correct 6 ms 9556 KB Output is correct
17 Correct 6 ms 9604 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 5 ms 9572 KB Output is correct
20 Correct 6 ms 9556 KB Output is correct
21 Correct 6 ms 9684 KB Output is correct
22 Correct 7 ms 9556 KB Output is correct
23 Correct 6 ms 9556 KB Output is correct
24 Correct 6 ms 9556 KB Output is correct
25 Correct 6 ms 9568 KB Output is correct
26 Correct 5 ms 9596 KB Output is correct
27 Correct 6 ms 9684 KB Output is correct
28 Correct 5 ms 9556 KB Output is correct
29 Correct 5 ms 9556 KB Output is correct
30 Correct 6 ms 9556 KB Output is correct
31 Correct 6 ms 9560 KB Output is correct
32 Correct 5 ms 9556 KB Output is correct
33 Correct 5 ms 9556 KB Output is correct
34 Correct 6 ms 9556 KB Output is correct
35 Correct 6 ms 9556 KB Output is correct
36 Correct 6 ms 9556 KB Output is correct
37 Correct 5 ms 9556 KB Output is correct
38 Correct 6 ms 9556 KB Output is correct
39 Correct 6 ms 9556 KB Output is correct
40 Correct 5 ms 9556 KB Output is correct
41 Correct 6 ms 9672 KB Output is correct
42 Correct 5 ms 9556 KB Output is correct
43 Correct 5 ms 9556 KB Output is correct
44 Correct 5 ms 9556 KB Output is correct
45 Correct 7 ms 9684 KB Output is correct
46 Correct 14 ms 10196 KB Output is correct
47 Correct 15 ms 10196 KB Output is correct
48 Correct 17 ms 10184 KB Output is correct
49 Correct 13 ms 10336 KB Output is correct
50 Correct 12 ms 10388 KB Output is correct
51 Correct 11 ms 10324 KB Output is correct
52 Correct 11 ms 10444 KB Output is correct
53 Correct 11 ms 10452 KB Output is correct
54 Correct 12 ms 10452 KB Output is correct
55 Correct 12 ms 10452 KB Output is correct
56 Correct 12 ms 10296 KB Output is correct
57 Correct 16 ms 10204 KB Output is correct
58 Correct 17 ms 10232 KB Output is correct
59 Correct 17 ms 10224 KB Output is correct
60 Correct 17 ms 10176 KB Output is correct
61 Correct 12 ms 10264 KB Output is correct
62 Correct 13 ms 10360 KB Output is correct
63 Correct 13 ms 10324 KB Output is correct
64 Correct 16 ms 9940 KB Output is correct
65 Correct 13 ms 9908 KB Output is correct
66 Correct 14 ms 9956 KB Output is correct
67 Correct 16 ms 9988 KB Output is correct
68 Correct 9 ms 10196 KB Output is correct
69 Correct 11 ms 10492 KB Output is correct
70 Correct 9 ms 10196 KB Output is correct
71 Correct 9 ms 10068 KB Output is correct
72 Correct 16 ms 10196 KB Output is correct
73 Correct 14 ms 9912 KB Output is correct
74 Correct 10 ms 10068 KB Output is correct
75 Correct 11 ms 10196 KB Output is correct
76 Correct 13 ms 10160 KB Output is correct
77 Correct 13 ms 10212 KB Output is correct
78 Correct 11 ms 10072 KB Output is correct
79 Correct 10 ms 9940 KB Output is correct
80 Correct 14 ms 9940 KB Output is correct
81 Correct 12 ms 10176 KB Output is correct
82 Correct 12 ms 10196 KB Output is correct
83 Correct 11 ms 10196 KB Output is correct
84 Correct 10 ms 10032 KB Output is correct
85 Correct 10 ms 10048 KB Output is correct
86 Correct 10 ms 9992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9556 KB Output is correct
2 Correct 6 ms 9572 KB Output is correct
3 Correct 5 ms 9556 KB Output is correct
4 Correct 6 ms 9556 KB Output is correct
5 Correct 6 ms 9556 KB Output is correct
6 Correct 6 ms 9556 KB Output is correct
7 Correct 8 ms 9556 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9580 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 7 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9556 KB Output is correct
16 Correct 6 ms 9556 KB Output is correct
17 Correct 6 ms 9604 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 5 ms 9572 KB Output is correct
20 Correct 6 ms 9556 KB Output is correct
21 Correct 6 ms 9684 KB Output is correct
22 Correct 7 ms 9556 KB Output is correct
23 Correct 6 ms 9556 KB Output is correct
24 Correct 6 ms 9556 KB Output is correct
25 Correct 6 ms 9568 KB Output is correct
26 Correct 5 ms 9596 KB Output is correct
27 Correct 6 ms 9684 KB Output is correct
28 Correct 5 ms 9556 KB Output is correct
29 Correct 5 ms 9556 KB Output is correct
30 Correct 6 ms 9556 KB Output is correct
31 Correct 6 ms 9560 KB Output is correct
32 Correct 5 ms 9556 KB Output is correct
33 Correct 5 ms 9556 KB Output is correct
34 Correct 6 ms 9556 KB Output is correct
35 Correct 6 ms 9556 KB Output is correct
36 Correct 6 ms 9556 KB Output is correct
37 Correct 5 ms 9556 KB Output is correct
38 Correct 6 ms 9556 KB Output is correct
39 Correct 6 ms 9556 KB Output is correct
40 Correct 5 ms 9556 KB Output is correct
41 Correct 6 ms 9672 KB Output is correct
42 Correct 5 ms 9556 KB Output is correct
43 Correct 5 ms 9556 KB Output is correct
44 Correct 5 ms 9556 KB Output is correct
45 Correct 7 ms 9684 KB Output is correct
46 Correct 14 ms 10196 KB Output is correct
47 Correct 15 ms 10196 KB Output is correct
48 Correct 17 ms 10184 KB Output is correct
49 Correct 13 ms 10336 KB Output is correct
50 Correct 12 ms 10388 KB Output is correct
51 Correct 11 ms 10324 KB Output is correct
52 Correct 11 ms 10444 KB Output is correct
53 Correct 11 ms 10452 KB Output is correct
54 Correct 12 ms 10452 KB Output is correct
55 Correct 12 ms 10452 KB Output is correct
56 Correct 12 ms 10296 KB Output is correct
57 Correct 16 ms 10204 KB Output is correct
58 Correct 17 ms 10232 KB Output is correct
59 Correct 17 ms 10224 KB Output is correct
60 Correct 17 ms 10176 KB Output is correct
61 Correct 12 ms 10264 KB Output is correct
62 Correct 13 ms 10360 KB Output is correct
63 Correct 13 ms 10324 KB Output is correct
64 Correct 16 ms 9940 KB Output is correct
65 Correct 13 ms 9908 KB Output is correct
66 Correct 14 ms 9956 KB Output is correct
67 Correct 16 ms 9988 KB Output is correct
68 Correct 9 ms 10196 KB Output is correct
69 Correct 11 ms 10492 KB Output is correct
70 Correct 9 ms 10196 KB Output is correct
71 Correct 9 ms 10068 KB Output is correct
72 Correct 16 ms 10196 KB Output is correct
73 Correct 14 ms 9912 KB Output is correct
74 Correct 10 ms 10068 KB Output is correct
75 Correct 11 ms 10196 KB Output is correct
76 Correct 13 ms 10160 KB Output is correct
77 Correct 13 ms 10212 KB Output is correct
78 Correct 11 ms 10072 KB Output is correct
79 Correct 10 ms 9940 KB Output is correct
80 Correct 14 ms 9940 KB Output is correct
81 Correct 12 ms 10176 KB Output is correct
82 Correct 12 ms 10196 KB Output is correct
83 Correct 11 ms 10196 KB Output is correct
84 Correct 10 ms 10032 KB Output is correct
85 Correct 10 ms 10048 KB Output is correct
86 Correct 10 ms 9992 KB Output is correct
87 Correct 31 ms 11084 KB Output is correct
88 Correct 95 ms 14240 KB Output is correct
89 Correct 417 ms 25140 KB Output is correct
90 Correct 407 ms 25184 KB Output is correct
91 Correct 383 ms 25164 KB Output is correct
92 Correct 217 ms 30028 KB Output is correct
93 Correct 199 ms 30036 KB Output is correct
94 Correct 201 ms 30064 KB Output is correct
95 Correct 235 ms 32200 KB Output is correct
96 Correct 230 ms 31992 KB Output is correct
97 Correct 241 ms 34140 KB Output is correct
98 Correct 226 ms 34172 KB Output is correct
99 Correct 216 ms 30952 KB Output is correct
100 Correct 537 ms 26860 KB Output is correct
101 Correct 540 ms 27080 KB Output is correct
102 Correct 588 ms 27184 KB Output is correct
103 Correct 570 ms 27272 KB Output is correct
104 Correct 254 ms 30708 KB Output is correct
105 Correct 263 ms 30760 KB Output is correct
106 Correct 281 ms 30868 KB Output is correct
107 Correct 265 ms 20968 KB Output is correct
108 Correct 341 ms 21200 KB Output is correct
109 Correct 340 ms 23072 KB Output is correct
110 Correct 149 ms 26176 KB Output is correct
111 Correct 210 ms 33808 KB Output is correct
112 Correct 154 ms 28108 KB Output is correct
113 Correct 153 ms 24784 KB Output is correct
114 Correct 541 ms 26840 KB Output is correct
115 Correct 391 ms 21120 KB Output is correct
116 Correct 179 ms 24668 KB Output is correct
117 Correct 245 ms 28272 KB Output is correct
118 Correct 218 ms 27588 KB Output is correct
119 Correct 208 ms 27180 KB Output is correct
120 Correct 145 ms 22592 KB Output is correct
121 Correct 152 ms 22132 KB Output is correct
122 Correct 169 ms 21768 KB Output is correct
123 Correct 268 ms 28240 KB Output is correct
124 Correct 266 ms 27672 KB Output is correct
125 Correct 243 ms 27236 KB Output is correct
126 Correct 207 ms 22924 KB Output is correct
127 Correct 176 ms 22092 KB Output is correct
128 Correct 215 ms 21804 KB Output is correct