Submission #828810

# Submission time Handle Problem Language Result Execution time Memory
828810 2023-08-17T16:31:01 Z MohamedAhmed04 Construction of Highway (JOI18_construction) C++14
100 / 100
217 ms 21908 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 1e5 + 10 ;

int arr[MAX] ;
int a[MAX] , b[MAX] ;
int n ;

vector< vector<int> >adj(MAX) ;

int bit[MAX] ;
stack< pair<int , int> >adds ;

void add(int i , int x)
{
	adds.push({i , x}) ;
	for(; i <= n ; i += (i & (-i)))
		bit[i] += x ;
}

int get(int i)
{
	int sum = 0 ;
	for(; i > 0 ; i -= (i & (-i)))
		sum += bit[i] ;
	return sum ;
}

int query(int l , int r)
{
	return (get(r) - get(l-1)) ;
}

void Clear()
{
	while(adds.size())
	{
		pair<int , int>p = adds.top() ;
		adds.pop() ;
		int i = p.first , x = p.second ;
		for(; i <= n ; i += (i & (-i)))
			bit[i] -= x ;
	}
}

void compress()
{
	vector<int>v ;
	for(int i = 1 ; i <= n ; ++i)
		v.push_back(arr[i]) ;
	sort(v.begin() , v.end()) ;
	v.erase(unique(v.begin() , v.end()) , v.end()) ;
	for(int i = 1 ; i <= n ; ++i)
	{
		arr[i] = lower_bound(v.begin() , v.end() , arr[i]) - v.begin() ;
		arr[i]++ ;
	}
}

int sz[MAX] , head[MAX] , P[MAX] ;
int in[MAX] , out[MAX] ;
int tim = 0 ;

void dfs(int node)
{
	sz[node] = 1 ;
	for(auto &child : adj[node])
	{
		P[child] = node ;
		dfs(child) ;
		sz[node] += sz[child] ;
		if(sz[child] > sz[adj[node][0]])
			swap(child , adj[node][0]) ;
	}
}

void hld(int node)
{
	in[node] = ++tim ;
	for(auto &child : adj[node])
	{
		if(child == adj[node][0])
			head[child] = head[node] ;
		else
			head[child] = child ;
		hld(child) ;
	}
	out[node] = tim ;
}

vector< array<int , 3> >v[MAX] ;
vector< pair<int , int> >vp ;

long long solve(int node)
{
	Clear() ;
	long long ans = 0 ;
	int now = node ;
	while(now)
	{
		vp.clear() ;
		while(v[head[now]].size() && v[head[now]].back()[1] < in[now])
			vp.emplace_back(v[head[now]].back()[2] , v[head[now]].back()[1] - v[head[now]].back()[0] + 1) , v[head[now]].pop_back() ;
		if(v[head[now]].size() && v[head[now]].back()[0] <= in[now])
		{
			int l = v[head[now]].back()[0] , r = v[head[now]].back()[1] , x = v[head[now]].back()[2] ;
			vp.emplace_back(x , in[now] - l + 1) ;
			v[head[now]].pop_back() ;
			v[head[now]].push_back({in[now]+1 , r , x}) ;
		}
		v[head[now]].push_back({in[head[now]] , in[now] , arr[node]}) ;
		reverse(vp.begin() , vp.end()) ;
		for(auto &p : vp)
			ans += 1ll * p.second * get(p.first-1) , add(p.first , p.second) ;
		now = P[head[now]] ;
	}
	return ans ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n ;
	for(int i = 1 ; i <= n ; ++i)
		cin>>arr[i] ;
	for(int i = 1 ; i <= n-1 ; ++i)
	{
		cin>>a[i]>>b[i] ;
		adj[a[i]].push_back(b[i]) ;
	}
	compress() ;
	head[1] = 1 ;
	dfs(1) ;
	hld(1) ;
	for(int i = 1 ; i <= n-1 ; ++i)
		cout<<solve(b[i])<<"\n" ;
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4996 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5120 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5056 KB Output is correct
13 Correct 3 ms 5012 KB Output is correct
14 Correct 3 ms 5064 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5056 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5128 KB Output is correct
21 Correct 4 ms 5124 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5016 KB Output is correct
24 Correct 3 ms 5052 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5120 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 4 ms 5052 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 2 ms 5076 KB Output is correct
38 Correct 2 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5052 KB Output is correct
42 Correct 2 ms 5052 KB Output is correct
43 Correct 3 ms 5048 KB Output is correct
44 Correct 2 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4996 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5120 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5056 KB Output is correct
13 Correct 3 ms 5012 KB Output is correct
14 Correct 3 ms 5064 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5056 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5128 KB Output is correct
21 Correct 4 ms 5124 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5016 KB Output is correct
24 Correct 3 ms 5052 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5120 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 4 ms 5052 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 2 ms 5076 KB Output is correct
38 Correct 2 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5052 KB Output is correct
42 Correct 2 ms 5052 KB Output is correct
43 Correct 3 ms 5048 KB Output is correct
44 Correct 2 ms 5076 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 7 ms 5332 KB Output is correct
47 Correct 6 ms 5444 KB Output is correct
48 Correct 6 ms 5332 KB Output is correct
49 Correct 5 ms 5716 KB Output is correct
50 Correct 5 ms 5716 KB Output is correct
51 Correct 4 ms 5696 KB Output is correct
52 Correct 5 ms 5572 KB Output is correct
53 Correct 5 ms 5588 KB Output is correct
54 Correct 5 ms 5580 KB Output is correct
55 Correct 5 ms 5572 KB Output is correct
56 Correct 4 ms 5460 KB Output is correct
57 Correct 6 ms 5332 KB Output is correct
58 Correct 7 ms 5332 KB Output is correct
59 Correct 7 ms 5332 KB Output is correct
60 Correct 7 ms 5312 KB Output is correct
61 Correct 4 ms 5460 KB Output is correct
62 Correct 4 ms 5460 KB Output is correct
63 Correct 5 ms 5460 KB Output is correct
64 Correct 8 ms 5332 KB Output is correct
65 Correct 5 ms 5332 KB Output is correct
66 Correct 6 ms 5332 KB Output is correct
67 Correct 6 ms 5316 KB Output is correct
68 Correct 4 ms 5700 KB Output is correct
69 Correct 4 ms 5588 KB Output is correct
70 Correct 4 ms 5588 KB Output is correct
71 Correct 4 ms 5444 KB Output is correct
72 Correct 6 ms 5332 KB Output is correct
73 Correct 6 ms 5332 KB Output is correct
74 Correct 4 ms 5440 KB Output is correct
75 Correct 5 ms 5452 KB Output is correct
76 Correct 4 ms 5460 KB Output is correct
77 Correct 5 ms 5448 KB Output is correct
78 Correct 4 ms 5444 KB Output is correct
79 Correct 4 ms 5460 KB Output is correct
80 Correct 4 ms 5460 KB Output is correct
81 Correct 5 ms 5460 KB Output is correct
82 Correct 5 ms 5448 KB Output is correct
83 Correct 5 ms 5440 KB Output is correct
84 Correct 4 ms 5460 KB Output is correct
85 Correct 5 ms 5460 KB Output is correct
86 Correct 4 ms 5444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4996 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5120 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5056 KB Output is correct
13 Correct 3 ms 5012 KB Output is correct
14 Correct 3 ms 5064 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5056 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5128 KB Output is correct
21 Correct 4 ms 5124 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5016 KB Output is correct
24 Correct 3 ms 5052 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5120 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 4 ms 5052 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 2 ms 5076 KB Output is correct
38 Correct 2 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5052 KB Output is correct
42 Correct 2 ms 5052 KB Output is correct
43 Correct 3 ms 5048 KB Output is correct
44 Correct 2 ms 5076 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 7 ms 5332 KB Output is correct
47 Correct 6 ms 5444 KB Output is correct
48 Correct 6 ms 5332 KB Output is correct
49 Correct 5 ms 5716 KB Output is correct
50 Correct 5 ms 5716 KB Output is correct
51 Correct 4 ms 5696 KB Output is correct
52 Correct 5 ms 5572 KB Output is correct
53 Correct 5 ms 5588 KB Output is correct
54 Correct 5 ms 5580 KB Output is correct
55 Correct 5 ms 5572 KB Output is correct
56 Correct 4 ms 5460 KB Output is correct
57 Correct 6 ms 5332 KB Output is correct
58 Correct 7 ms 5332 KB Output is correct
59 Correct 7 ms 5332 KB Output is correct
60 Correct 7 ms 5312 KB Output is correct
61 Correct 4 ms 5460 KB Output is correct
62 Correct 4 ms 5460 KB Output is correct
63 Correct 5 ms 5460 KB Output is correct
64 Correct 8 ms 5332 KB Output is correct
65 Correct 5 ms 5332 KB Output is correct
66 Correct 6 ms 5332 KB Output is correct
67 Correct 6 ms 5316 KB Output is correct
68 Correct 4 ms 5700 KB Output is correct
69 Correct 4 ms 5588 KB Output is correct
70 Correct 4 ms 5588 KB Output is correct
71 Correct 4 ms 5444 KB Output is correct
72 Correct 6 ms 5332 KB Output is correct
73 Correct 6 ms 5332 KB Output is correct
74 Correct 4 ms 5440 KB Output is correct
75 Correct 5 ms 5452 KB Output is correct
76 Correct 4 ms 5460 KB Output is correct
77 Correct 5 ms 5448 KB Output is correct
78 Correct 4 ms 5444 KB Output is correct
79 Correct 4 ms 5460 KB Output is correct
80 Correct 4 ms 5460 KB Output is correct
81 Correct 5 ms 5460 KB Output is correct
82 Correct 5 ms 5448 KB Output is correct
83 Correct 5 ms 5440 KB Output is correct
84 Correct 4 ms 5460 KB Output is correct
85 Correct 5 ms 5460 KB Output is correct
86 Correct 4 ms 5444 KB Output is correct
87 Correct 12 ms 5924 KB Output is correct
88 Correct 36 ms 7848 KB Output is correct
89 Correct 180 ms 14532 KB Output is correct
90 Correct 132 ms 14520 KB Output is correct
91 Correct 165 ms 14480 KB Output is correct
92 Correct 66 ms 21908 KB Output is correct
93 Correct 78 ms 21860 KB Output is correct
94 Correct 65 ms 21800 KB Output is correct
95 Correct 67 ms 18940 KB Output is correct
96 Correct 87 ms 19452 KB Output is correct
97 Correct 69 ms 19368 KB Output is correct
98 Correct 77 ms 19364 KB Output is correct
99 Correct 74 ms 18088 KB Output is correct
100 Correct 141 ms 14080 KB Output is correct
101 Correct 199 ms 14392 KB Output is correct
102 Correct 217 ms 14372 KB Output is correct
103 Correct 177 ms 14392 KB Output is correct
104 Correct 90 ms 17988 KB Output is correct
105 Correct 79 ms 18012 KB Output is correct
106 Correct 81 ms 17988 KB Output is correct
107 Correct 117 ms 13280 KB Output is correct
108 Correct 119 ms 13508 KB Output is correct
109 Correct 131 ms 13888 KB Output is correct
110 Correct 56 ms 20924 KB Output is correct
111 Correct 69 ms 18996 KB Output is correct
112 Correct 58 ms 18420 KB Output is correct
113 Correct 70 ms 16944 KB Output is correct
114 Correct 145 ms 14096 KB Output is correct
115 Correct 162 ms 13428 KB Output is correct
116 Correct 75 ms 17044 KB Output is correct
117 Correct 71 ms 16252 KB Output is correct
118 Correct 72 ms 15708 KB Output is correct
119 Correct 71 ms 15424 KB Output is correct
120 Correct 63 ms 15428 KB Output is correct
121 Correct 64 ms 15024 KB Output is correct
122 Correct 62 ms 14780 KB Output is correct
123 Correct 79 ms 16180 KB Output is correct
124 Correct 85 ms 15824 KB Output is correct
125 Correct 79 ms 15532 KB Output is correct
126 Correct 71 ms 15560 KB Output is correct
127 Correct 68 ms 15044 KB Output is correct
128 Correct 89 ms 14852 KB Output is correct