답안 #529074

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529074 2022-02-22T05:27:54 Z 8e7 Construction of Highway (JOI18_construction) C++17
100 / 100
255 ms 21336 KB
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ...b ){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 100005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
struct BIT{
	ll bit[maxn];
	vector<pii> ch;
	void modify(int ind, int val) {
		ch.push_back({ind, val});
		for (;ind < maxn;ind += ind & (-ind)) bit[ind] += val;
	}
	ll query(int ind) {
		ll ret = 0;
		for (;ind > 0;ind -= ind & (-ind)) ret += bit[ind];
		return ret;
	}
	void rev() {
		for (auto [ind, val]:ch) {
			for (;ind < maxn;ind += ind & (-ind)) bit[ind] -= val;
		}
		ch.clear();
	}
} bit;

vector<int> adj[maxn];

int c[maxn], par[maxn], siz[maxn], dep[maxn], ti[maxn], up[maxn], ord[maxn];
int nxt[maxn];
void getsiz(int n, int pa, int d) {
	dep[n] = d;
	siz[n] = 1;
	for (int v:adj[n]) {
		getsiz(v, n, d+1);
		siz[n] += siz[v];	
	}
}
void mark(int n, int pa, int vi) {
	up[n] = vi;
	int best = 0, bind = 0;
	for (int v:adj[n]) {
		if (siz[v] > best) {
			best = siz[v], bind = v;
		}
	}
	nxt[n] = bind;
	for (int v:adj[n]) {
		if (v != bind) {
			mark(v, n, v);
		} else {
			mark(v, n, vi);
		}
	}
}
ll ans[maxn];

vector<pii> se[maxn];

int main() {
	io
	int n;
	cin >> n;
	vector<int> val;
	for (int i = 1;i <= n;i++) cin >> c[i], val.push_back(c[i]);		
	sort(val.begin(), val.end());
	val.resize(int(unique(val.begin(), val.end()) - val.begin()));
	for (int i = 1;i <= n;i++) c[i] = (lower_bound(val.begin(), val.end(), c[i]) - val.begin() + 1);	

	for (int i = 1;i <= n - 1;i++) {
		int u, v;
		cin >> u >> v;
		par[v] = u;
		ti[v] = i;
		ord[i] = v;
		adj[u].push_back(v);
	}
	getsiz(1, 0, 0);
	mark(1, 0, 1);
	
	se[1].push_back({0, 1});
	pary(up + 1, up + n + 1);
	for (int id = 1;id <= n - 1;id++) {
		int i = par[ord[id]], nc = c[ord[id]];
		vector<pii> col;
		col.push_back({dep[ord[id]], maxn - 1});
		debug(i);
		while (i) {
			//get all segments into col and update
			int p = up[i], di = dep[i] + 1, last = 0;
			vector<pii> &cur = se[p];
			vector<pii> tmp;
			int siz = cur.size();

			while (siz && cur.back().ff < di) {
				last = cur.back().ss;	
				tmp.push_back(cur.back());	
				cur.pop_back();
				siz--;
			}
			if (ti[nxt[i]] < id && !(siz && cur.back().ff == di)) cur.push_back({di, last});
			cur.push_back({dep[p], nc});

			reverse(tmp.begin(), tmp.end());
			col.insert(col.end(), tmp.begin(), tmp.end());
			for (auto x:cur) debug(x.ff, x.ss);
			i = par[p];
		}
		i = ord[id];
		//debug(col[0].ff, col[0].ss);
		for (int j = 1;j < col.size();j++) {
			//debug(col[j].ff, col[j].ss);
			ans[i] += bit.query(col[j].ss - 1) * (col[j-1].ff - col[j].ff);
			bit.modify(col[j].ss, col[j-1].ff - col[j].ff);
		}
		if (i == up[i]) {
			se[i].push_back({dep[i], c[i]});
		}
		bit.rev();
	}

	for (int i = 1;i <= n - 1;i++) cout << ans[ord[i]] << "\n";
}
/*
7
1 2 3 4 5 6 1
1 2
2 3
3 4
3 5
2 6
4 7

9
1 4 5 2 7 3 6 3 1
1 2
1 3
3 4
4 5
4 6
1 7
2 8
5 9

10
1 5 8 6 7 2 9 3 4 10
1 5
1 8
5 9
8 3
5 4
8 6
9 7
3 10
4 2
*/

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:13:19: warning: statement has no effect [-Wunused-value]
   13 | #define pary(...) 0
      |                   ^
construction.cpp:96:2: note: in expansion of macro 'pary'
   96 |  pary(up + 1, up + n + 1);
      |  ^~~~
construction.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 0
      |                    ^
construction.cpp:101:3: note: in expansion of macro 'debug'
  101 |   debug(i);
      |   ^~~~~
construction.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 0
      |                    ^
construction.cpp:120:21: note: in expansion of macro 'debug'
  120 |    for (auto x:cur) debug(x.ff, x.ss);
      |                     ^~~~~
construction.cpp:120:14: warning: variable 'x' set but not used [-Wunused-but-set-variable]
  120 |    for (auto x:cur) debug(x.ff, x.ss);
      |              ^
construction.cpp:125:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |   for (int j = 1;j < col.size();j++) {
      |                  ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5060 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 3 ms 5068 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 4 ms 5088 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5164 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5164 KB Output is correct
13 Correct 3 ms 5068 KB Output is correct
14 Correct 3 ms 5164 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
16 Correct 3 ms 5068 KB Output is correct
17 Correct 3 ms 5068 KB Output is correct
18 Correct 3 ms 5044 KB Output is correct
19 Correct 3 ms 5068 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 3 ms 5148 KB Output is correct
23 Correct 3 ms 5032 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5160 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 3 ms 5068 KB Output is correct
30 Correct 3 ms 5068 KB Output is correct
31 Correct 3 ms 5068 KB Output is correct
32 Correct 3 ms 5156 KB Output is correct
33 Correct 3 ms 5068 KB Output is correct
34 Correct 3 ms 5032 KB Output is correct
35 Correct 5 ms 5068 KB Output is correct
36 Correct 3 ms 5032 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 3 ms 5028 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 4 ms 5196 KB Output is correct
42 Correct 3 ms 5032 KB Output is correct
43 Correct 5 ms 5068 KB Output is correct
44 Correct 10 ms 5068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5060 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 3 ms 5068 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 4 ms 5088 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5164 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5164 KB Output is correct
13 Correct 3 ms 5068 KB Output is correct
14 Correct 3 ms 5164 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
16 Correct 3 ms 5068 KB Output is correct
17 Correct 3 ms 5068 KB Output is correct
18 Correct 3 ms 5044 KB Output is correct
19 Correct 3 ms 5068 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 3 ms 5148 KB Output is correct
23 Correct 3 ms 5032 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5160 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 3 ms 5068 KB Output is correct
30 Correct 3 ms 5068 KB Output is correct
31 Correct 3 ms 5068 KB Output is correct
32 Correct 3 ms 5156 KB Output is correct
33 Correct 3 ms 5068 KB Output is correct
34 Correct 3 ms 5032 KB Output is correct
35 Correct 5 ms 5068 KB Output is correct
36 Correct 3 ms 5032 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 3 ms 5028 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 4 ms 5196 KB Output is correct
42 Correct 3 ms 5032 KB Output is correct
43 Correct 5 ms 5068 KB Output is correct
44 Correct 10 ms 5068 KB Output is correct
45 Correct 4 ms 5196 KB Output is correct
46 Correct 7 ms 5452 KB Output is correct
47 Correct 7 ms 5424 KB Output is correct
48 Correct 7 ms 5452 KB Output is correct
49 Correct 6 ms 5792 KB Output is correct
50 Correct 6 ms 5708 KB Output is correct
51 Correct 6 ms 5764 KB Output is correct
52 Correct 5 ms 5676 KB Output is correct
53 Correct 6 ms 5680 KB Output is correct
54 Correct 6 ms 5680 KB Output is correct
55 Correct 5 ms 5672 KB Output is correct
56 Correct 5 ms 5556 KB Output is correct
57 Correct 9 ms 5444 KB Output is correct
58 Correct 12 ms 5492 KB Output is correct
59 Correct 9 ms 5500 KB Output is correct
60 Correct 9 ms 5428 KB Output is correct
61 Correct 6 ms 5548 KB Output is correct
62 Correct 6 ms 5548 KB Output is correct
63 Correct 6 ms 5580 KB Output is correct
64 Correct 7 ms 5452 KB Output is correct
65 Correct 9 ms 5372 KB Output is correct
66 Correct 8 ms 5432 KB Output is correct
67 Correct 7 ms 5424 KB Output is correct
68 Correct 5 ms 5688 KB Output is correct
69 Correct 6 ms 5680 KB Output is correct
70 Correct 6 ms 5580 KB Output is correct
71 Correct 6 ms 5580 KB Output is correct
72 Correct 9 ms 5424 KB Output is correct
73 Correct 9 ms 5424 KB Output is correct
74 Correct 6 ms 5548 KB Output is correct
75 Correct 6 ms 5552 KB Output is correct
76 Correct 6 ms 5452 KB Output is correct
77 Correct 6 ms 5452 KB Output is correct
78 Correct 5 ms 5452 KB Output is correct
79 Correct 6 ms 5452 KB Output is correct
80 Correct 5 ms 5488 KB Output is correct
81 Correct 6 ms 5548 KB Output is correct
82 Correct 6 ms 5420 KB Output is correct
83 Correct 6 ms 5452 KB Output is correct
84 Correct 5 ms 5508 KB Output is correct
85 Correct 6 ms 5452 KB Output is correct
86 Correct 6 ms 5424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5060 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 3 ms 5068 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 4 ms 5088 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5164 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5164 KB Output is correct
13 Correct 3 ms 5068 KB Output is correct
14 Correct 3 ms 5164 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
16 Correct 3 ms 5068 KB Output is correct
17 Correct 3 ms 5068 KB Output is correct
18 Correct 3 ms 5044 KB Output is correct
19 Correct 3 ms 5068 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 3 ms 5148 KB Output is correct
23 Correct 3 ms 5032 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5160 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 3 ms 5068 KB Output is correct
30 Correct 3 ms 5068 KB Output is correct
31 Correct 3 ms 5068 KB Output is correct
32 Correct 3 ms 5156 KB Output is correct
33 Correct 3 ms 5068 KB Output is correct
34 Correct 3 ms 5032 KB Output is correct
35 Correct 5 ms 5068 KB Output is correct
36 Correct 3 ms 5032 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 3 ms 5028 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 4 ms 5196 KB Output is correct
42 Correct 3 ms 5032 KB Output is correct
43 Correct 5 ms 5068 KB Output is correct
44 Correct 10 ms 5068 KB Output is correct
45 Correct 4 ms 5196 KB Output is correct
46 Correct 7 ms 5452 KB Output is correct
47 Correct 7 ms 5424 KB Output is correct
48 Correct 7 ms 5452 KB Output is correct
49 Correct 6 ms 5792 KB Output is correct
50 Correct 6 ms 5708 KB Output is correct
51 Correct 6 ms 5764 KB Output is correct
52 Correct 5 ms 5676 KB Output is correct
53 Correct 6 ms 5680 KB Output is correct
54 Correct 6 ms 5680 KB Output is correct
55 Correct 5 ms 5672 KB Output is correct
56 Correct 5 ms 5556 KB Output is correct
57 Correct 9 ms 5444 KB Output is correct
58 Correct 12 ms 5492 KB Output is correct
59 Correct 9 ms 5500 KB Output is correct
60 Correct 9 ms 5428 KB Output is correct
61 Correct 6 ms 5548 KB Output is correct
62 Correct 6 ms 5548 KB Output is correct
63 Correct 6 ms 5580 KB Output is correct
64 Correct 7 ms 5452 KB Output is correct
65 Correct 9 ms 5372 KB Output is correct
66 Correct 8 ms 5432 KB Output is correct
67 Correct 7 ms 5424 KB Output is correct
68 Correct 5 ms 5688 KB Output is correct
69 Correct 6 ms 5680 KB Output is correct
70 Correct 6 ms 5580 KB Output is correct
71 Correct 6 ms 5580 KB Output is correct
72 Correct 9 ms 5424 KB Output is correct
73 Correct 9 ms 5424 KB Output is correct
74 Correct 6 ms 5548 KB Output is correct
75 Correct 6 ms 5552 KB Output is correct
76 Correct 6 ms 5452 KB Output is correct
77 Correct 6 ms 5452 KB Output is correct
78 Correct 5 ms 5452 KB Output is correct
79 Correct 6 ms 5452 KB Output is correct
80 Correct 5 ms 5488 KB Output is correct
81 Correct 6 ms 5548 KB Output is correct
82 Correct 6 ms 5420 KB Output is correct
83 Correct 6 ms 5452 KB Output is correct
84 Correct 5 ms 5508 KB Output is correct
85 Correct 6 ms 5452 KB Output is correct
86 Correct 6 ms 5424 KB Output is correct
87 Correct 15 ms 6052 KB Output is correct
88 Correct 49 ms 7660 KB Output is correct
89 Correct 171 ms 13740 KB Output is correct
90 Correct 171 ms 13676 KB Output is correct
91 Correct 197 ms 13712 KB Output is correct
92 Correct 96 ms 21276 KB Output is correct
93 Correct 91 ms 21316 KB Output is correct
94 Correct 90 ms 21336 KB Output is correct
95 Correct 89 ms 18840 KB Output is correct
96 Correct 98 ms 18992 KB Output is correct
97 Correct 93 ms 18856 KB Output is correct
98 Correct 94 ms 18856 KB Output is correct
99 Correct 102 ms 17604 KB Output is correct
100 Correct 233 ms 13916 KB Output is correct
101 Correct 254 ms 13848 KB Output is correct
102 Correct 250 ms 13780 KB Output is correct
103 Correct 255 ms 13756 KB Output is correct
104 Correct 98 ms 17592 KB Output is correct
105 Correct 104 ms 17580 KB Output is correct
106 Correct 104 ms 17596 KB Output is correct
107 Correct 151 ms 12988 KB Output is correct
108 Correct 154 ms 12996 KB Output is correct
109 Correct 162 ms 13252 KB Output is correct
110 Correct 77 ms 20652 KB Output is correct
111 Correct 90 ms 18840 KB Output is correct
112 Correct 79 ms 18112 KB Output is correct
113 Correct 80 ms 16768 KB Output is correct
114 Correct 229 ms 13892 KB Output is correct
115 Correct 233 ms 12996 KB Output is correct
116 Correct 86 ms 16812 KB Output is correct
117 Correct 93 ms 15560 KB Output is correct
118 Correct 96 ms 14868 KB Output is correct
119 Correct 95 ms 14508 KB Output is correct
120 Correct 84 ms 14788 KB Output is correct
121 Correct 87 ms 14116 KB Output is correct
122 Correct 85 ms 13864 KB Output is correct
123 Correct 107 ms 15548 KB Output is correct
124 Correct 104 ms 14916 KB Output is correct
125 Correct 105 ms 14616 KB Output is correct
126 Correct 94 ms 14904 KB Output is correct
127 Correct 102 ms 14352 KB Output is correct
128 Correct 95 ms 13860 KB Output is correct