Submission #706702

# Submission time Handle Problem Language Result Execution time Memory
706702 2023-03-07T12:14:29 Z attemptn63 Construction of Highway (JOI18_construction) C++17
100 / 100
336 ms 112900 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define double long double
#define endl '\n'
#define all(k) (k).begin(), (k).end()
#define pb push_back
#define fi first
#define se second
#define sz(k) (int)(k).size()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pi pair<long long, long long>
#define mp make_pair
#define ti tuple<long long, long long, long long>
#define INF (long long)1e18
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define sign(x) ((x > 0) - (x < 0))
template <class T>using vec = vector<T>;
template <class T>using min_heap = priority_queue<T, vec<T>, greater<T>>;
template <class T>using max_heap = priority_queue<T>;
template <class T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T>using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifdef DEBUG
template <typename T>
inline void debug(T k){
	cout << k << " ";
}
template <typename T, typename... Args>
inline void debug(T k, Args... args){
	cout << k << " | ";
	debug(args...);
}
#define debug(...) cout << "[" << #__VA_ARGS__ << "] : ";debug(__VA_ARGS__);cout << endl
template <typename T>
ostream &operator<<(ostream &o_str, const vec<T> &p){
	o_str << "{ ";
	for (auto i = 0; i < (int)p.size(); i++){
		o_str << p[i];
		if (i < (int)p.size() - 1){
			o_str << ", ";
		}
	}
	return o_str << " }" << endl;
}
template <typename T>
ostream &operator<<(ostream &o_str, const deque<T> &p){
	o_str << "{ ";
	for (auto i = 0; i < (int)p.size(); i++){
		o_str << p[i];
		if (i < (int)p.size() - 1){
			o_str << ", ";
		}
	}
	return o_str << " }" << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o_str, const pair<T, U> &p){
	return o_str << "(" << p.fi << ", " << p.se << ")";
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &o_str, const tuple<T, U, V> &p){
	return o_str << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")";
}
template <typename T>
ostream &operator<<(ostream &o_str, const set<T> &p){
	o_str << "{ ";
	for (auto i = p.begin(); i != p.end(); i++){
		o_str << *i;
		if (i != --p.end()){
			o_str << ", ";
		}
	}
	return o_str << " }" << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o_str, const map<T, U> &p){
	o_str << "{ ";
	for (auto i = p.begin(); i != p.end(); i++){
		o_str << i->first << ": " << i->second;
		if (i != --p.end()){
			o_str << ", ";
		}
	}
	return o_str << " }" << endl;
}
template <typename T>
ostream &operator<<(ostream &o_str, const queue<T> &p){
	queue<T> q = p;
	o_str << "{ ";
	while (!q.empty()){
		o_str << q.front();
		q.pop();
		if (!q.empty()){
			o_str << ", ";
		}
	}
	return o_str << " }" << endl;
}
template <typename T, typename U, typename cmp>
ostream &operator<<(ostream &o_str, const priority_queue<T,U,cmp> &p){
	priority_queue<T,U,cmp> q = p;
	o_str << "{ ";
	while (!q.empty()){
		o_str << q.top();
		q.pop();
		if (!q.empty()){
			o_str << ", ";
		}
	}
	return o_str << " }" << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o_str, const set<T, U> &p){
	o_str << "{ ";
	for (auto i = p.begin(); i != p.end(); i++){
		o_str << *i;
		if (i != --p.end()){
			o_str << ", ";
		}
	}
	return o_str << " }" << endl;
}
template<typename T, typename U>
ostream &operator<<(ostream &o_str, const multiset<T, U> &p){
	o_str << "{ ";
	for (auto i = p.begin(); i != p.end(); i++){
		o_str << *i;
		if (i != --p.end()){
			o_str << ", ";
		}
	}
	return o_str << " }" << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o_str, const unordered_map<T, U> &p){
	o_str << "{ ";
	for (auto i = p.begin(); i != p.end(); i++){
		o_str << i->first << ": " << i->second;
		o_str << ", ";
	}
	return o_str << " }" << endl;
}
#else
template <typename T>
inline void debug(T k){}
template <typename T, typename... Args>
inline void debug(T k, Args... args) {}
#define debug(...)
template <typename T>
ostream &operator<<(ostream &o_str, const vec<T> &p){return o_str;}
template <typename T>
ostream &operator<<(ostream &o_str, const deque<T> &p) { return o_str; }
template <typename T, typename U>
ostream &operator<<(ostream &o_str, const pair<T, U> &p) { return o_str; }
template <typename T, typename U, typename V>
ostream &operator<<(ostream &o_str, const tuple<T, U, V> &p) { return o_str; }
template <typename T>
ostream &operator<<(ostream &o_str, const set<T> &p) { return o_str; }
template <typename T, typename U>
ostream &operator<<(ostream &o_str, const map<T, U> &p) { return o_str; }
template <typename T>
ostream &operator<<(ostream &o_str, const queue<T> &p) { return o_str; }
template <typename T, typename U, typename cmp>
ostream &operator<<(ostream &o_str, const priority_queue<T,U,cmp> &p){return o_str;}
template <typename T, typename U>
ostream &operator<<(ostream &o_str, const set<T, U> &p) { return o_str; }
template<typename T, typename U>
ostream &operator<<(ostream &o_str, const multiset<T, U> &p) { return o_str; }
template <typename T, typename U>
ostream &operator<<(ostream &o_str, const unordered_map<T, U> &p) { return o_str; }
#endif

int n, val[100005],depth[100005],sz[100005],hpar[100005],par[100005];
deque<pi> hld[100005];
vec<int> adj[100005],disc;
vec<pi> edges,chain;
void dfs_sz(int i){
	sz[i] = 1;
	for(auto &j: adj[i]){
		par[j] = i;
		depth[j] = depth[i] + 1;
		dfs_sz(j);
		sz[i] += sz[j];
		if(sz[j] > sz[adj[i][0]]) swap(j,adj[i][0]);
	}
}
void dfs_hld(int i){
	for(auto it: adj[i]){
		if(it == adj[i][0]) hpar[it] = hpar[i];
		else hpar[it] = it;
		dfs_hld(it);
	}
}

int bit[100005];
void init(int n){
	for(int i = 0;i<=n;i++) bit[i] = 0;
}
void upd(int i,int v){
	i++;
	for(;i<=n;i+=(i&-i)) bit[i] += v;
}
int qry(int i){
	i++;
	int ret = 0;
	for(;i;i-=(i&-i)) ret += bit[i];
	return ret;
}
signed main(){
	speed;
	cin >> n;
	for(int i = 0;i<n;i++) cin >> val[i];
	hpar[0] = 0;
	par[0] = -1;
	hld[0].push_back({val[0],1LL});
	for(int i = 0;i<n-1;i++){
		int a,b; cin >> a >> b; a--; b--;
		adj[a].push_back(b);
		edges.push_back({a,b});
	}
	dfs_sz(0);
	dfs_hld(0);
	for(int i = 0;i<n-1;i++){
		chain.clear();
		disc.clear();
		int a = edges[i].fi;
		while(a != -1){
			int apar = hpar[a];
			int siz = depth[a] - depth[apar] + 1;
			vec<pi> temp;
			for(int k = 0;k<sz(hld[apar]);k++){	
				auto [cnt,v] = hld[apar][k];
				if(cnt <= siz){
					siz -= cnt;
					temp.push_back({cnt,v});
				}
				else{
					temp.push_back({siz,v});
					break;
				}
			}
			for(int i = sz(temp)-1;i>=0;i--){
				chain.push_back(temp[i]);
				disc.push_back(temp[i].second);
			}
			a = par[apar];
		}
		reverse(all(chain));
		sort(all(disc));
		disc.erase(unique(all(disc)),disc.end());
		init(sz(disc));
		int ans = 0,mx = 0;	
		for(auto [u,v]: chain){
			v = lower_bound(all(disc),v) - disc.begin();
			ans += u * (mx - qry(v));
			mx += u;
			upd(v,u);
		}
		cout << ans << endl;
		int u = edges[i].se;
		while(u != -1){
			int nxtu = hpar[u];
			int siz = depth[u] - depth[nxtu] + 1;
			while(sz(hld[nxtu]) > 0){
				int cnt = hld[nxtu][0].first;
				if(cnt <= siz){
					siz -= cnt;
					hld[nxtu].pop_front();
				}
				else{
					hld[nxtu][0].first -= siz;
					break;
				}
			}
			hld[nxtu].push_front({depth[u] - depth[nxtu] + 1,val[edges[i].se]});
			u = par[nxtu];
		}
	}
}	
# Verdict Execution time Memory Grader output
1 Correct 40 ms 69964 KB Output is correct
2 Correct 40 ms 70004 KB Output is correct
3 Correct 44 ms 70060 KB Output is correct
4 Correct 45 ms 70128 KB Output is correct
5 Correct 45 ms 70200 KB Output is correct
6 Correct 46 ms 70136 KB Output is correct
7 Correct 47 ms 70204 KB Output is correct
8 Correct 41 ms 70052 KB Output is correct
9 Correct 41 ms 70104 KB Output is correct
10 Correct 41 ms 70104 KB Output is correct
11 Correct 46 ms 70220 KB Output is correct
12 Correct 44 ms 70192 KB Output is correct
13 Correct 45 ms 70192 KB Output is correct
14 Correct 50 ms 70216 KB Output is correct
15 Correct 45 ms 70132 KB Output is correct
16 Correct 47 ms 70144 KB Output is correct
17 Correct 45 ms 70148 KB Output is correct
18 Correct 45 ms 70248 KB Output is correct
19 Correct 48 ms 70128 KB Output is correct
20 Correct 46 ms 70128 KB Output is correct
21 Correct 44 ms 70120 KB Output is correct
22 Correct 46 ms 70192 KB Output is correct
23 Correct 44 ms 70196 KB Output is correct
24 Correct 43 ms 70176 KB Output is correct
25 Correct 44 ms 70152 KB Output is correct
26 Correct 41 ms 70108 KB Output is correct
27 Correct 43 ms 70132 KB Output is correct
28 Correct 46 ms 70132 KB Output is correct
29 Correct 43 ms 70132 KB Output is correct
30 Correct 43 ms 70080 KB Output is correct
31 Correct 48 ms 70172 KB Output is correct
32 Correct 47 ms 70220 KB Output is correct
33 Correct 43 ms 70108 KB Output is correct
34 Correct 47 ms 70120 KB Output is correct
35 Correct 45 ms 70124 KB Output is correct
36 Correct 48 ms 70080 KB Output is correct
37 Correct 43 ms 70100 KB Output is correct
38 Correct 43 ms 70188 KB Output is correct
39 Correct 43 ms 70100 KB Output is correct
40 Correct 46 ms 70100 KB Output is correct
41 Correct 43 ms 70100 KB Output is correct
42 Correct 43 ms 70192 KB Output is correct
43 Correct 44 ms 70136 KB Output is correct
44 Correct 43 ms 70132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 69964 KB Output is correct
2 Correct 40 ms 70004 KB Output is correct
3 Correct 44 ms 70060 KB Output is correct
4 Correct 45 ms 70128 KB Output is correct
5 Correct 45 ms 70200 KB Output is correct
6 Correct 46 ms 70136 KB Output is correct
7 Correct 47 ms 70204 KB Output is correct
8 Correct 41 ms 70052 KB Output is correct
9 Correct 41 ms 70104 KB Output is correct
10 Correct 41 ms 70104 KB Output is correct
11 Correct 46 ms 70220 KB Output is correct
12 Correct 44 ms 70192 KB Output is correct
13 Correct 45 ms 70192 KB Output is correct
14 Correct 50 ms 70216 KB Output is correct
15 Correct 45 ms 70132 KB Output is correct
16 Correct 47 ms 70144 KB Output is correct
17 Correct 45 ms 70148 KB Output is correct
18 Correct 45 ms 70248 KB Output is correct
19 Correct 48 ms 70128 KB Output is correct
20 Correct 46 ms 70128 KB Output is correct
21 Correct 44 ms 70120 KB Output is correct
22 Correct 46 ms 70192 KB Output is correct
23 Correct 44 ms 70196 KB Output is correct
24 Correct 43 ms 70176 KB Output is correct
25 Correct 44 ms 70152 KB Output is correct
26 Correct 41 ms 70108 KB Output is correct
27 Correct 43 ms 70132 KB Output is correct
28 Correct 46 ms 70132 KB Output is correct
29 Correct 43 ms 70132 KB Output is correct
30 Correct 43 ms 70080 KB Output is correct
31 Correct 48 ms 70172 KB Output is correct
32 Correct 47 ms 70220 KB Output is correct
33 Correct 43 ms 70108 KB Output is correct
34 Correct 47 ms 70120 KB Output is correct
35 Correct 45 ms 70124 KB Output is correct
36 Correct 48 ms 70080 KB Output is correct
37 Correct 43 ms 70100 KB Output is correct
38 Correct 43 ms 70188 KB Output is correct
39 Correct 43 ms 70100 KB Output is correct
40 Correct 46 ms 70100 KB Output is correct
41 Correct 43 ms 70100 KB Output is correct
42 Correct 43 ms 70192 KB Output is correct
43 Correct 44 ms 70136 KB Output is correct
44 Correct 43 ms 70132 KB Output is correct
45 Correct 44 ms 70364 KB Output is correct
46 Correct 49 ms 71412 KB Output is correct
47 Correct 49 ms 71452 KB Output is correct
48 Correct 49 ms 71456 KB Output is correct
49 Correct 42 ms 70660 KB Output is correct
50 Correct 42 ms 70856 KB Output is correct
51 Correct 43 ms 70768 KB Output is correct
52 Correct 46 ms 71664 KB Output is correct
53 Correct 47 ms 71608 KB Output is correct
54 Correct 47 ms 71708 KB Output is correct
55 Correct 46 ms 71628 KB Output is correct
56 Correct 47 ms 71484 KB Output is correct
57 Correct 49 ms 71372 KB Output is correct
58 Correct 52 ms 71344 KB Output is correct
59 Correct 50 ms 71372 KB Output is correct
60 Correct 49 ms 71296 KB Output is correct
61 Correct 50 ms 71512 KB Output is correct
62 Correct 48 ms 71488 KB Output is correct
63 Correct 47 ms 71548 KB Output is correct
64 Correct 49 ms 71372 KB Output is correct
65 Correct 49 ms 71340 KB Output is correct
66 Correct 49 ms 71304 KB Output is correct
67 Correct 50 ms 71420 KB Output is correct
68 Correct 43 ms 70732 KB Output is correct
69 Correct 47 ms 71676 KB Output is correct
70 Correct 46 ms 71624 KB Output is correct
71 Correct 48 ms 71464 KB Output is correct
72 Correct 49 ms 71348 KB Output is correct
73 Correct 50 ms 71268 KB Output is correct
74 Correct 48 ms 71556 KB Output is correct
75 Correct 45 ms 71500 KB Output is correct
76 Correct 47 ms 71396 KB Output is correct
77 Correct 47 ms 71368 KB Output is correct
78 Correct 49 ms 71372 KB Output is correct
79 Correct 47 ms 71328 KB Output is correct
80 Correct 47 ms 71428 KB Output is correct
81 Correct 47 ms 71508 KB Output is correct
82 Correct 46 ms 71472 KB Output is correct
83 Correct 49 ms 71428 KB Output is correct
84 Correct 46 ms 71372 KB Output is correct
85 Correct 46 ms 71420 KB Output is correct
86 Correct 47 ms 71428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 69964 KB Output is correct
2 Correct 40 ms 70004 KB Output is correct
3 Correct 44 ms 70060 KB Output is correct
4 Correct 45 ms 70128 KB Output is correct
5 Correct 45 ms 70200 KB Output is correct
6 Correct 46 ms 70136 KB Output is correct
7 Correct 47 ms 70204 KB Output is correct
8 Correct 41 ms 70052 KB Output is correct
9 Correct 41 ms 70104 KB Output is correct
10 Correct 41 ms 70104 KB Output is correct
11 Correct 46 ms 70220 KB Output is correct
12 Correct 44 ms 70192 KB Output is correct
13 Correct 45 ms 70192 KB Output is correct
14 Correct 50 ms 70216 KB Output is correct
15 Correct 45 ms 70132 KB Output is correct
16 Correct 47 ms 70144 KB Output is correct
17 Correct 45 ms 70148 KB Output is correct
18 Correct 45 ms 70248 KB Output is correct
19 Correct 48 ms 70128 KB Output is correct
20 Correct 46 ms 70128 KB Output is correct
21 Correct 44 ms 70120 KB Output is correct
22 Correct 46 ms 70192 KB Output is correct
23 Correct 44 ms 70196 KB Output is correct
24 Correct 43 ms 70176 KB Output is correct
25 Correct 44 ms 70152 KB Output is correct
26 Correct 41 ms 70108 KB Output is correct
27 Correct 43 ms 70132 KB Output is correct
28 Correct 46 ms 70132 KB Output is correct
29 Correct 43 ms 70132 KB Output is correct
30 Correct 43 ms 70080 KB Output is correct
31 Correct 48 ms 70172 KB Output is correct
32 Correct 47 ms 70220 KB Output is correct
33 Correct 43 ms 70108 KB Output is correct
34 Correct 47 ms 70120 KB Output is correct
35 Correct 45 ms 70124 KB Output is correct
36 Correct 48 ms 70080 KB Output is correct
37 Correct 43 ms 70100 KB Output is correct
38 Correct 43 ms 70188 KB Output is correct
39 Correct 43 ms 70100 KB Output is correct
40 Correct 46 ms 70100 KB Output is correct
41 Correct 43 ms 70100 KB Output is correct
42 Correct 43 ms 70192 KB Output is correct
43 Correct 44 ms 70136 KB Output is correct
44 Correct 43 ms 70132 KB Output is correct
45 Correct 44 ms 70364 KB Output is correct
46 Correct 49 ms 71412 KB Output is correct
47 Correct 49 ms 71452 KB Output is correct
48 Correct 49 ms 71456 KB Output is correct
49 Correct 42 ms 70660 KB Output is correct
50 Correct 42 ms 70856 KB Output is correct
51 Correct 43 ms 70768 KB Output is correct
52 Correct 46 ms 71664 KB Output is correct
53 Correct 47 ms 71608 KB Output is correct
54 Correct 47 ms 71708 KB Output is correct
55 Correct 46 ms 71628 KB Output is correct
56 Correct 47 ms 71484 KB Output is correct
57 Correct 49 ms 71372 KB Output is correct
58 Correct 52 ms 71344 KB Output is correct
59 Correct 50 ms 71372 KB Output is correct
60 Correct 49 ms 71296 KB Output is correct
61 Correct 50 ms 71512 KB Output is correct
62 Correct 48 ms 71488 KB Output is correct
63 Correct 47 ms 71548 KB Output is correct
64 Correct 49 ms 71372 KB Output is correct
65 Correct 49 ms 71340 KB Output is correct
66 Correct 49 ms 71304 KB Output is correct
67 Correct 50 ms 71420 KB Output is correct
68 Correct 43 ms 70732 KB Output is correct
69 Correct 47 ms 71676 KB Output is correct
70 Correct 46 ms 71624 KB Output is correct
71 Correct 48 ms 71464 KB Output is correct
72 Correct 49 ms 71348 KB Output is correct
73 Correct 50 ms 71268 KB Output is correct
74 Correct 48 ms 71556 KB Output is correct
75 Correct 45 ms 71500 KB Output is correct
76 Correct 47 ms 71396 KB Output is correct
77 Correct 47 ms 71368 KB Output is correct
78 Correct 49 ms 71372 KB Output is correct
79 Correct 47 ms 71328 KB Output is correct
80 Correct 47 ms 71428 KB Output is correct
81 Correct 47 ms 71508 KB Output is correct
82 Correct 46 ms 71472 KB Output is correct
83 Correct 49 ms 71428 KB Output is correct
84 Correct 46 ms 71372 KB Output is correct
85 Correct 46 ms 71420 KB Output is correct
86 Correct 47 ms 71428 KB Output is correct
87 Correct 58 ms 73568 KB Output is correct
88 Correct 97 ms 80672 KB Output is correct
89 Correct 274 ms 105616 KB Output is correct
90 Correct 258 ms 105588 KB Output is correct
91 Correct 256 ms 105520 KB Output is correct
92 Correct 109 ms 88736 KB Output is correct
93 Correct 112 ms 88724 KB Output is correct
94 Correct 109 ms 88756 KB Output is correct
95 Correct 138 ms 112440 KB Output is correct
96 Correct 159 ms 112836 KB Output is correct
97 Correct 152 ms 112880 KB Output is correct
98 Correct 154 ms 112900 KB Output is correct
99 Correct 148 ms 109264 KB Output is correct
100 Correct 291 ms 97468 KB Output is correct
101 Correct 331 ms 97716 KB Output is correct
102 Correct 331 ms 97764 KB Output is correct
103 Correct 336 ms 97748 KB Output is correct
104 Correct 162 ms 109252 KB Output is correct
105 Correct 153 ms 109236 KB Output is correct
106 Correct 164 ms 109288 KB Output is correct
107 Correct 252 ms 104892 KB Output is correct
108 Correct 256 ms 105028 KB Output is correct
109 Correct 259 ms 105172 KB Output is correct
110 Correct 97 ms 88124 KB Output is correct
111 Correct 139 ms 112476 KB Output is correct
112 Correct 141 ms 112028 KB Output is correct
113 Correct 145 ms 108600 KB Output is correct
114 Correct 281 ms 97464 KB Output is correct
115 Correct 336 ms 97112 KB Output is correct
116 Correct 163 ms 108604 KB Output is correct
117 Correct 158 ms 106908 KB Output is correct
118 Correct 160 ms 106296 KB Output is correct
119 Correct 162 ms 105952 KB Output is correct
120 Correct 146 ms 106600 KB Output is correct
121 Correct 148 ms 105944 KB Output is correct
122 Correct 155 ms 105580 KB Output is correct
123 Correct 161 ms 107040 KB Output is correct
124 Correct 165 ms 106340 KB Output is correct
125 Correct 166 ms 105964 KB Output is correct
126 Correct 162 ms 106844 KB Output is correct
127 Correct 159 ms 105944 KB Output is correct
128 Correct 161 ms 105680 KB Output is correct