Submission #943304

# Submission time Handle Problem Language Result Execution time Memory
943304 2024-03-11T10:25:57 Z lovrot Construction of Highway (JOI18_construction) C++17
100 / 100
251 ms 27688 KB
#include <cstdio> 
#include <set>
#include <vector> 
#include <algorithm> 
#include <stack> 

#define X first
#define Y second
#define PB push_back
#define MP make_pair

using namespace std; 

typedef long long ll; 
typedef pair<int, int> pii;

const int LOG = 17;
const int N = 1 << LOG; 

int A[N], B[N], C[N];

int DEP[N], SIZ[N], CH[N], chi, H[N], P[N];
vector<int> G[N], CMP;

int dfs(int u) {
	++SIZ[u];

	int mx = 0, mxi = 0; 
	for(int v : G[u]) {
		DEP[v] = DEP[u] + 1;
		int res = dfs(v);

		SIZ[u] += res;
		if(res > mx) {
			mx = res;
			mxi = v;
		}
	}

	CH[u] = !mxi ? ++chi : CH[mxi]; 
	H[CH[u]] = u;
	return SIZ[u];
}

int F[N];
stack<pii> U;

void add(int x, int val, bool f = 1) {
	if(f) U.push({x, val});
	for(; x < N; x += x & -x) F[x] += val;
}

int sum(int x) {
	int ret = 0;
	for(; x; x -= x & -x) ret += F[x];
	return ret;
}

void undo() {
	for(; !U.empty(); U.pop()) add(U.top().X, -U.top().Y, 0); 
}

set<pii> S[N]; // {DEP, VAL}

ll solve(int u, int x) { // NE ZABORAVI parent mora bit dodan u set od lanca (1 !!!)
	if(!u) {
		undo();
		return 0;
	}
	
//	printf("entry %d\n", u);

	int ch = CH[u];
	auto it = S[ch].lower_bound(MP(DEP[u], -1));

	ll ret = 0;
	for(bool f = (*it).X == DEP[u]; 1; f = 1) {
		int dep = (*it).X, val = (*it).Y;
		int siz = (f ? dep : DEP[u]) - (it == S[ch].begin() ? DEP[H[ch]] - 1 : (*prev(it)).X);

//		printf("%d| %d %d\n", it == S[ch].begin(), siz, sum(val - 1));
		ret += (ll) siz * sum(val - 1);
		add(val, siz);
		
		auto nxt = it == S[ch].begin() ? S[ch].end() : prev(it);

		if(f) S[ch].erase(it);
	
		if(nxt == S[ch].end()) break;
		it = nxt;
	}
	
	S[ch].insert({DEP[u], x});
	return ret + solve(P[H[ch]], x);
}

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", C + i); 
		CMP.PB(C[i]);
	}

	sort(CMP.begin(), CMP.end());
	CMP.erase(unique(CMP.begin(), CMP.end()), CMP.end());
	for(int i = 1; i <= n; ++i) C[i] = upper_bound(CMP.begin(), CMP.end(), C[i]) - CMP.begin();
	
	for(int i = 0; i < n - 1; ++i) {
		scanf("%d%d", A + i, B + i); 
		G[A[i]].PB(B[i]);
		P[B[i]] = A[i];
	}

	dfs(1); 
		
//	for(int i = 1; i <= n; ++i) printf("dep %d siz %d ch %d\n", DEP[i], SIZ[i], CH[i]);

	S[CH[1]].insert({DEP[1], C[1]});
	for(int i = 0; i < n - 1; ++i) {
		S[CH[B[i]]].insert({DEP[B[i]], C[B[i]]});
		printf("%lld\n", solve(A[i], C[B[i]]));
	}
	return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
construction.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   scanf("%d", C + i);
      |   ~~~~~^~~~~~~~~~~~~
construction.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |   scanf("%d%d", A + i, B + i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 4 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13656 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13736 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13912 KB Output is correct
15 Correct 3 ms 13656 KB Output is correct
16 Correct 4 ms 13660 KB Output is correct
17 Correct 4 ms 13744 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13908 KB Output is correct
20 Correct 6 ms 13656 KB Output is correct
21 Correct 4 ms 13912 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 4 ms 13740 KB Output is correct
25 Correct 3 ms 13656 KB Output is correct
26 Correct 3 ms 13656 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 3 ms 13660 KB Output is correct
31 Correct 3 ms 13660 KB Output is correct
32 Correct 3 ms 13660 KB Output is correct
33 Correct 4 ms 13660 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13660 KB Output is correct
36 Correct 3 ms 13660 KB Output is correct
37 Correct 3 ms 13736 KB Output is correct
38 Correct 4 ms 13660 KB Output is correct
39 Correct 4 ms 13660 KB Output is correct
40 Correct 6 ms 13656 KB Output is correct
41 Correct 3 ms 13656 KB Output is correct
42 Correct 3 ms 13660 KB Output is correct
43 Correct 3 ms 13660 KB Output is correct
44 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 4 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13656 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13736 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13912 KB Output is correct
15 Correct 3 ms 13656 KB Output is correct
16 Correct 4 ms 13660 KB Output is correct
17 Correct 4 ms 13744 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13908 KB Output is correct
20 Correct 6 ms 13656 KB Output is correct
21 Correct 4 ms 13912 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 4 ms 13740 KB Output is correct
25 Correct 3 ms 13656 KB Output is correct
26 Correct 3 ms 13656 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 3 ms 13660 KB Output is correct
31 Correct 3 ms 13660 KB Output is correct
32 Correct 3 ms 13660 KB Output is correct
33 Correct 4 ms 13660 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13660 KB Output is correct
36 Correct 3 ms 13660 KB Output is correct
37 Correct 3 ms 13736 KB Output is correct
38 Correct 4 ms 13660 KB Output is correct
39 Correct 4 ms 13660 KB Output is correct
40 Correct 6 ms 13656 KB Output is correct
41 Correct 3 ms 13656 KB Output is correct
42 Correct 3 ms 13660 KB Output is correct
43 Correct 3 ms 13660 KB Output is correct
44 Correct 3 ms 13660 KB Output is correct
45 Correct 4 ms 13656 KB Output is correct
46 Correct 7 ms 14168 KB Output is correct
47 Correct 7 ms 13888 KB Output is correct
48 Correct 7 ms 13916 KB Output is correct
49 Correct 5 ms 14172 KB Output is correct
50 Correct 5 ms 14172 KB Output is correct
51 Correct 5 ms 14172 KB Output is correct
52 Correct 5 ms 14172 KB Output is correct
53 Correct 5 ms 14172 KB Output is correct
54 Correct 6 ms 14172 KB Output is correct
55 Correct 5 ms 14000 KB Output is correct
56 Correct 5 ms 13916 KB Output is correct
57 Correct 8 ms 13872 KB Output is correct
58 Correct 9 ms 13916 KB Output is correct
59 Correct 9 ms 13916 KB Output is correct
60 Correct 9 ms 13916 KB Output is correct
61 Correct 5 ms 13916 KB Output is correct
62 Correct 5 ms 14000 KB Output is correct
63 Correct 5 ms 13916 KB Output is correct
64 Correct 6 ms 13916 KB Output is correct
65 Correct 7 ms 13916 KB Output is correct
66 Correct 7 ms 13916 KB Output is correct
67 Correct 9 ms 13912 KB Output is correct
68 Correct 6 ms 14172 KB Output is correct
69 Correct 6 ms 14172 KB Output is correct
70 Correct 5 ms 14172 KB Output is correct
71 Correct 5 ms 13916 KB Output is correct
72 Correct 8 ms 13944 KB Output is correct
73 Correct 8 ms 13916 KB Output is correct
74 Correct 6 ms 13916 KB Output is correct
75 Correct 5 ms 13916 KB Output is correct
76 Correct 5 ms 13916 KB Output is correct
77 Correct 6 ms 13780 KB Output is correct
78 Correct 5 ms 13912 KB Output is correct
79 Correct 5 ms 13916 KB Output is correct
80 Correct 5 ms 13916 KB Output is correct
81 Correct 7 ms 13916 KB Output is correct
82 Correct 5 ms 13916 KB Output is correct
83 Correct 7 ms 13816 KB Output is correct
84 Correct 5 ms 13916 KB Output is correct
85 Correct 5 ms 13916 KB Output is correct
86 Correct 6 ms 13884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 4 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 4 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 3 ms 13656 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13736 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13912 KB Output is correct
15 Correct 3 ms 13656 KB Output is correct
16 Correct 4 ms 13660 KB Output is correct
17 Correct 4 ms 13744 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13908 KB Output is correct
20 Correct 6 ms 13656 KB Output is correct
21 Correct 4 ms 13912 KB Output is correct
22 Correct 3 ms 13660 KB Output is correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 4 ms 13740 KB Output is correct
25 Correct 3 ms 13656 KB Output is correct
26 Correct 3 ms 13656 KB Output is correct
27 Correct 3 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 3 ms 13660 KB Output is correct
31 Correct 3 ms 13660 KB Output is correct
32 Correct 3 ms 13660 KB Output is correct
33 Correct 4 ms 13660 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13660 KB Output is correct
36 Correct 3 ms 13660 KB Output is correct
37 Correct 3 ms 13736 KB Output is correct
38 Correct 4 ms 13660 KB Output is correct
39 Correct 4 ms 13660 KB Output is correct
40 Correct 6 ms 13656 KB Output is correct
41 Correct 3 ms 13656 KB Output is correct
42 Correct 3 ms 13660 KB Output is correct
43 Correct 3 ms 13660 KB Output is correct
44 Correct 3 ms 13660 KB Output is correct
45 Correct 4 ms 13656 KB Output is correct
46 Correct 7 ms 14168 KB Output is correct
47 Correct 7 ms 13888 KB Output is correct
48 Correct 7 ms 13916 KB Output is correct
49 Correct 5 ms 14172 KB Output is correct
50 Correct 5 ms 14172 KB Output is correct
51 Correct 5 ms 14172 KB Output is correct
52 Correct 5 ms 14172 KB Output is correct
53 Correct 5 ms 14172 KB Output is correct
54 Correct 6 ms 14172 KB Output is correct
55 Correct 5 ms 14000 KB Output is correct
56 Correct 5 ms 13916 KB Output is correct
57 Correct 8 ms 13872 KB Output is correct
58 Correct 9 ms 13916 KB Output is correct
59 Correct 9 ms 13916 KB Output is correct
60 Correct 9 ms 13916 KB Output is correct
61 Correct 5 ms 13916 KB Output is correct
62 Correct 5 ms 14000 KB Output is correct
63 Correct 5 ms 13916 KB Output is correct
64 Correct 6 ms 13916 KB Output is correct
65 Correct 7 ms 13916 KB Output is correct
66 Correct 7 ms 13916 KB Output is correct
67 Correct 9 ms 13912 KB Output is correct
68 Correct 6 ms 14172 KB Output is correct
69 Correct 6 ms 14172 KB Output is correct
70 Correct 5 ms 14172 KB Output is correct
71 Correct 5 ms 13916 KB Output is correct
72 Correct 8 ms 13944 KB Output is correct
73 Correct 8 ms 13916 KB Output is correct
74 Correct 6 ms 13916 KB Output is correct
75 Correct 5 ms 13916 KB Output is correct
76 Correct 5 ms 13916 KB Output is correct
77 Correct 6 ms 13780 KB Output is correct
78 Correct 5 ms 13912 KB Output is correct
79 Correct 5 ms 13916 KB Output is correct
80 Correct 5 ms 13916 KB Output is correct
81 Correct 7 ms 13916 KB Output is correct
82 Correct 5 ms 13916 KB Output is correct
83 Correct 7 ms 13816 KB Output is correct
84 Correct 5 ms 13916 KB Output is correct
85 Correct 5 ms 13916 KB Output is correct
86 Correct 6 ms 13884 KB Output is correct
87 Correct 15 ms 14172 KB Output is correct
88 Correct 42 ms 15572 KB Output is correct
89 Correct 158 ms 19916 KB Output is correct
90 Correct 165 ms 20000 KB Output is correct
91 Correct 153 ms 19952 KB Output is correct
92 Correct 78 ms 25540 KB Output is correct
93 Correct 76 ms 27592 KB Output is correct
94 Correct 78 ms 27688 KB Output is correct
95 Correct 84 ms 26824 KB Output is correct
96 Correct 88 ms 27340 KB Output is correct
97 Correct 95 ms 27480 KB Output is correct
98 Correct 86 ms 27340 KB Output is correct
99 Correct 76 ms 24776 KB Output is correct
100 Correct 213 ms 23240 KB Output is correct
101 Correct 251 ms 23536 KB Output is correct
102 Correct 236 ms 23384 KB Output is correct
103 Correct 242 ms 23440 KB Output is correct
104 Correct 85 ms 24788 KB Output is correct
105 Correct 83 ms 24540 KB Output is correct
106 Correct 98 ms 24640 KB Output is correct
107 Correct 124 ms 21444 KB Output is correct
108 Correct 140 ms 21452 KB Output is correct
109 Correct 148 ms 21680 KB Output is correct
110 Correct 73 ms 27088 KB Output is correct
111 Correct 85 ms 26824 KB Output is correct
112 Correct 89 ms 26568 KB Output is correct
113 Correct 83 ms 23956 KB Output is correct
114 Correct 192 ms 23228 KB Output is correct
115 Correct 215 ms 22984 KB Output is correct
116 Correct 77 ms 23980 KB Output is correct
117 Correct 79 ms 22460 KB Output is correct
118 Correct 82 ms 21844 KB Output is correct
119 Correct 76 ms 21328 KB Output is correct
120 Correct 67 ms 21960 KB Output is correct
121 Correct 83 ms 21456 KB Output is correct
122 Correct 73 ms 21136 KB Output is correct
123 Correct 89 ms 22476 KB Output is correct
124 Correct 95 ms 21736 KB Output is correct
125 Correct 94 ms 21452 KB Output is correct
126 Correct 104 ms 22216 KB Output is correct
127 Correct 79 ms 21424 KB Output is correct
128 Correct 78 ms 21192 KB Output is correct