Submission #80767

# Submission time Handle Problem Language Result Execution time Memory
80767 2018-10-22T09:54:51 Z szawinis Construction of Highway (JOI18_construction) C++17
100 / 100
621 ms 180272 KB
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author szawinis
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
using namespace std;
#define long long long
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 1e5+1;

class Construction {

	int n;
	vector<vector<int> > g;
	vector<pair<int,int> > edges;
	vector<int> C, vals;

	vector<int> sz, nxt, par, depth;
	deque<pair<int,int> > dq[N];

	vector<int> fen;
	vector<long> ans;

	void init_dfs(int u) {
		sz[u] = 1;
		for(int &v: g[u]) {
			par[v] = u;
			depth[v] = depth[u] + 1;
			init_dfs(v);
			sz[u] += sz[v];
			if (sz[v] > sz[g[u][0]]) swap(v, g[u][0]);
		}
	}

	void dec(int u) {
		for(int &v: g[u]) {
			nxt[v] = (v == g[u][0] ? nxt[u] : v);
			dec(v);
		}
	}

	void compress() {
		for(int i = 1; i <= n; i++) vals.push_back(C[i]);
		sort(vals.begin(), vals.end());
		vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
		unordered_map<int, int> mp;
		for(int i = 0; i < vals.size(); i++) mp[vals[i]] = i;
		for(int i = 1; i <= n; i++) C[i] = mp[C[i]];
	}

	void update(int i, int v) {
		++i;
		while(i <= n) fen[i] += v, i += i & -i;
	}

	int query(int i) {
		++i;
		int ret = 0;
		while(i > 0) ret += fen[i], i -= i & -i;
		return ret;
	}

public:
	void solve(istream& cin, ostream& cout) {
		cin >> n;
		g = vector<vector<int> >(n+1);
		C = vector<int>(n+1);
		sz = vector<int>(n+1);
		nxt = vector<int>(n+1);
		par = vector<int>(n+1);
		depth = vector<int>(n+1);
		fen = vector<int>(n+1);

		for(int i = 1; i <= n; i++) cin >> C[i];
		compress();

		for(int i = 1, u, v; i < n; i++) {
			cin >> u >> v;
			g[u].push_back(v);
			edges.emplace_back(u, v);
		}
		nxt[1] = 1;
		par[1] = -1;
		init_dfs(1);
		dec(1);

		dq[1].emplace_front(C[1], 1);
		for(auto e: edges) {
			int a, b;
			tie(a, b) = e;
			vector<pair<int,int> > segs;
			for(int v = a; v != -1; v = par[nxt[v]]) {
				vector<pair<int,int> > loc;
				int sum = 0, targ = depth[v] - depth[nxt[v]] + 1;
				while(!dq[nxt[v]].empty() && sum < targ) {
					if(targ - sum < dq[nxt[v]].front().second) {
						int val, cnt;
						tie(val, cnt) = dq[nxt[v]].front();
						loc.emplace_back(val, targ - sum);
						dq[nxt[v]].pop_front();
						dq[nxt[v]].emplace_front(val, cnt - (targ - sum));
						sum = targ;
					} else {
						sum += dq[nxt[v]].front().second;
						loc.push_back(dq[nxt[v]].front());
						dq[nxt[v]].pop_front();
					}
				}
				reverse(loc.begin(), loc.end());
				segs.insert(segs.end(), loc.begin(), loc.end());
			}
			// compute answer using segs
			long res = 0;
			for(auto p: segs) {
				res += 1ll * query(p.first-1) * p.second;
				update(p.first, p.second);
			}
			for(auto p: segs) {
				update(p.first, -p.second);
			}
			ans.push_back(res);
			// update
			for(int v = b; v != -1; v = par[nxt[v]]) {
				dq[nxt[v]].emplace_front(C[b], depth[v] - depth[nxt[v]] + 1);
			}
		}
		for(long x: ans) cout << x << '\n';
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	Construction solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}

Compilation message

construction.cpp: In member function 'void Construction::compress()':
construction.cpp:55:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < vals.size(); i++) mp[vals[i]] = i;
                  ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 70 ms 67832 KB Output is correct
2 Correct 74 ms 68100 KB Output is correct
3 Correct 79 ms 68100 KB Output is correct
4 Correct 69 ms 68312 KB Output is correct
5 Correct 72 ms 68408 KB Output is correct
6 Correct 73 ms 68452 KB Output is correct
7 Correct 100 ms 68464 KB Output is correct
8 Correct 72 ms 68464 KB Output is correct
9 Correct 75 ms 68464 KB Output is correct
10 Correct 79 ms 68464 KB Output is correct
11 Correct 73 ms 68548 KB Output is correct
12 Correct 72 ms 68644 KB Output is correct
13 Correct 75 ms 68716 KB Output is correct
14 Correct 76 ms 68728 KB Output is correct
15 Correct 72 ms 68868 KB Output is correct
16 Correct 74 ms 68868 KB Output is correct
17 Correct 71 ms 68868 KB Output is correct
18 Correct 72 ms 68868 KB Output is correct
19 Correct 71 ms 68872 KB Output is correct
20 Correct 72 ms 68872 KB Output is correct
21 Correct 73 ms 68976 KB Output is correct
22 Correct 74 ms 68976 KB Output is correct
23 Correct 72 ms 68976 KB Output is correct
24 Correct 72 ms 68976 KB Output is correct
25 Correct 74 ms 68976 KB Output is correct
26 Correct 73 ms 68976 KB Output is correct
27 Correct 74 ms 68976 KB Output is correct
28 Correct 75 ms 68976 KB Output is correct
29 Correct 74 ms 69024 KB Output is correct
30 Correct 73 ms 69024 KB Output is correct
31 Correct 73 ms 69024 KB Output is correct
32 Correct 71 ms 69024 KB Output is correct
33 Correct 70 ms 69024 KB Output is correct
34 Correct 74 ms 69024 KB Output is correct
35 Correct 76 ms 69024 KB Output is correct
36 Correct 90 ms 69124 KB Output is correct
37 Correct 76 ms 69124 KB Output is correct
38 Correct 77 ms 69124 KB Output is correct
39 Correct 75 ms 69124 KB Output is correct
40 Correct 73 ms 69124 KB Output is correct
41 Correct 81 ms 69124 KB Output is correct
42 Correct 92 ms 69148 KB Output is correct
43 Correct 88 ms 69148 KB Output is correct
44 Correct 78 ms 69148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 67832 KB Output is correct
2 Correct 74 ms 68100 KB Output is correct
3 Correct 79 ms 68100 KB Output is correct
4 Correct 69 ms 68312 KB Output is correct
5 Correct 72 ms 68408 KB Output is correct
6 Correct 73 ms 68452 KB Output is correct
7 Correct 100 ms 68464 KB Output is correct
8 Correct 72 ms 68464 KB Output is correct
9 Correct 75 ms 68464 KB Output is correct
10 Correct 79 ms 68464 KB Output is correct
11 Correct 73 ms 68548 KB Output is correct
12 Correct 72 ms 68644 KB Output is correct
13 Correct 75 ms 68716 KB Output is correct
14 Correct 76 ms 68728 KB Output is correct
15 Correct 72 ms 68868 KB Output is correct
16 Correct 74 ms 68868 KB Output is correct
17 Correct 71 ms 68868 KB Output is correct
18 Correct 72 ms 68868 KB Output is correct
19 Correct 71 ms 68872 KB Output is correct
20 Correct 72 ms 68872 KB Output is correct
21 Correct 73 ms 68976 KB Output is correct
22 Correct 74 ms 68976 KB Output is correct
23 Correct 72 ms 68976 KB Output is correct
24 Correct 72 ms 68976 KB Output is correct
25 Correct 74 ms 68976 KB Output is correct
26 Correct 73 ms 68976 KB Output is correct
27 Correct 74 ms 68976 KB Output is correct
28 Correct 75 ms 68976 KB Output is correct
29 Correct 74 ms 69024 KB Output is correct
30 Correct 73 ms 69024 KB Output is correct
31 Correct 73 ms 69024 KB Output is correct
32 Correct 71 ms 69024 KB Output is correct
33 Correct 70 ms 69024 KB Output is correct
34 Correct 74 ms 69024 KB Output is correct
35 Correct 76 ms 69024 KB Output is correct
36 Correct 90 ms 69124 KB Output is correct
37 Correct 76 ms 69124 KB Output is correct
38 Correct 77 ms 69124 KB Output is correct
39 Correct 75 ms 69124 KB Output is correct
40 Correct 73 ms 69124 KB Output is correct
41 Correct 81 ms 69124 KB Output is correct
42 Correct 92 ms 69148 KB Output is correct
43 Correct 88 ms 69148 KB Output is correct
44 Correct 78 ms 69148 KB Output is correct
45 Correct 103 ms 69172 KB Output is correct
46 Correct 87 ms 70268 KB Output is correct
47 Correct 85 ms 70416 KB Output is correct
48 Correct 80 ms 70496 KB Output is correct
49 Correct 76 ms 70496 KB Output is correct
50 Correct 91 ms 70496 KB Output is correct
51 Correct 76 ms 70496 KB Output is correct
52 Correct 77 ms 70928 KB Output is correct
53 Correct 81 ms 70988 KB Output is correct
54 Correct 81 ms 71044 KB Output is correct
55 Correct 90 ms 71140 KB Output is correct
56 Correct 87 ms 71140 KB Output is correct
57 Correct 100 ms 71188 KB Output is correct
58 Correct 85 ms 71220 KB Output is correct
59 Correct 95 ms 71304 KB Output is correct
60 Correct 83 ms 71372 KB Output is correct
61 Correct 81 ms 71496 KB Output is correct
62 Correct 84 ms 71524 KB Output is correct
63 Correct 81 ms 71592 KB Output is correct
64 Correct 89 ms 71676 KB Output is correct
65 Correct 79 ms 71724 KB Output is correct
66 Correct 90 ms 71792 KB Output is correct
67 Correct 82 ms 71792 KB Output is correct
68 Correct 77 ms 71792 KB Output is correct
69 Correct 85 ms 72056 KB Output is correct
70 Correct 84 ms 72112 KB Output is correct
71 Correct 94 ms 72112 KB Output is correct
72 Correct 92 ms 72112 KB Output is correct
73 Correct 83 ms 72112 KB Output is correct
74 Correct 77 ms 72428 KB Output is correct
75 Correct 84 ms 72428 KB Output is correct
76 Correct 81 ms 72428 KB Output is correct
77 Correct 78 ms 72428 KB Output is correct
78 Correct 125 ms 72428 KB Output is correct
79 Correct 76 ms 72476 KB Output is correct
80 Correct 76 ms 72524 KB Output is correct
81 Correct 83 ms 72572 KB Output is correct
82 Correct 82 ms 72636 KB Output is correct
83 Correct 82 ms 72700 KB Output is correct
84 Correct 78 ms 72796 KB Output is correct
85 Correct 81 ms 72844 KB Output is correct
86 Correct 81 ms 72892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 67832 KB Output is correct
2 Correct 74 ms 68100 KB Output is correct
3 Correct 79 ms 68100 KB Output is correct
4 Correct 69 ms 68312 KB Output is correct
5 Correct 72 ms 68408 KB Output is correct
6 Correct 73 ms 68452 KB Output is correct
7 Correct 100 ms 68464 KB Output is correct
8 Correct 72 ms 68464 KB Output is correct
9 Correct 75 ms 68464 KB Output is correct
10 Correct 79 ms 68464 KB Output is correct
11 Correct 73 ms 68548 KB Output is correct
12 Correct 72 ms 68644 KB Output is correct
13 Correct 75 ms 68716 KB Output is correct
14 Correct 76 ms 68728 KB Output is correct
15 Correct 72 ms 68868 KB Output is correct
16 Correct 74 ms 68868 KB Output is correct
17 Correct 71 ms 68868 KB Output is correct
18 Correct 72 ms 68868 KB Output is correct
19 Correct 71 ms 68872 KB Output is correct
20 Correct 72 ms 68872 KB Output is correct
21 Correct 73 ms 68976 KB Output is correct
22 Correct 74 ms 68976 KB Output is correct
23 Correct 72 ms 68976 KB Output is correct
24 Correct 72 ms 68976 KB Output is correct
25 Correct 74 ms 68976 KB Output is correct
26 Correct 73 ms 68976 KB Output is correct
27 Correct 74 ms 68976 KB Output is correct
28 Correct 75 ms 68976 KB Output is correct
29 Correct 74 ms 69024 KB Output is correct
30 Correct 73 ms 69024 KB Output is correct
31 Correct 73 ms 69024 KB Output is correct
32 Correct 71 ms 69024 KB Output is correct
33 Correct 70 ms 69024 KB Output is correct
34 Correct 74 ms 69024 KB Output is correct
35 Correct 76 ms 69024 KB Output is correct
36 Correct 90 ms 69124 KB Output is correct
37 Correct 76 ms 69124 KB Output is correct
38 Correct 77 ms 69124 KB Output is correct
39 Correct 75 ms 69124 KB Output is correct
40 Correct 73 ms 69124 KB Output is correct
41 Correct 81 ms 69124 KB Output is correct
42 Correct 92 ms 69148 KB Output is correct
43 Correct 88 ms 69148 KB Output is correct
44 Correct 78 ms 69148 KB Output is correct
45 Correct 103 ms 69172 KB Output is correct
46 Correct 87 ms 70268 KB Output is correct
47 Correct 85 ms 70416 KB Output is correct
48 Correct 80 ms 70496 KB Output is correct
49 Correct 76 ms 70496 KB Output is correct
50 Correct 91 ms 70496 KB Output is correct
51 Correct 76 ms 70496 KB Output is correct
52 Correct 77 ms 70928 KB Output is correct
53 Correct 81 ms 70988 KB Output is correct
54 Correct 81 ms 71044 KB Output is correct
55 Correct 90 ms 71140 KB Output is correct
56 Correct 87 ms 71140 KB Output is correct
57 Correct 100 ms 71188 KB Output is correct
58 Correct 85 ms 71220 KB Output is correct
59 Correct 95 ms 71304 KB Output is correct
60 Correct 83 ms 71372 KB Output is correct
61 Correct 81 ms 71496 KB Output is correct
62 Correct 84 ms 71524 KB Output is correct
63 Correct 81 ms 71592 KB Output is correct
64 Correct 89 ms 71676 KB Output is correct
65 Correct 79 ms 71724 KB Output is correct
66 Correct 90 ms 71792 KB Output is correct
67 Correct 82 ms 71792 KB Output is correct
68 Correct 77 ms 71792 KB Output is correct
69 Correct 85 ms 72056 KB Output is correct
70 Correct 84 ms 72112 KB Output is correct
71 Correct 94 ms 72112 KB Output is correct
72 Correct 92 ms 72112 KB Output is correct
73 Correct 83 ms 72112 KB Output is correct
74 Correct 77 ms 72428 KB Output is correct
75 Correct 84 ms 72428 KB Output is correct
76 Correct 81 ms 72428 KB Output is correct
77 Correct 78 ms 72428 KB Output is correct
78 Correct 125 ms 72428 KB Output is correct
79 Correct 76 ms 72476 KB Output is correct
80 Correct 76 ms 72524 KB Output is correct
81 Correct 83 ms 72572 KB Output is correct
82 Correct 82 ms 72636 KB Output is correct
83 Correct 82 ms 72700 KB Output is correct
84 Correct 78 ms 72796 KB Output is correct
85 Correct 81 ms 72844 KB Output is correct
86 Correct 81 ms 72892 KB Output is correct
87 Correct 105 ms 75152 KB Output is correct
88 Correct 159 ms 82616 KB Output is correct
89 Correct 427 ms 108856 KB Output is correct
90 Correct 437 ms 110968 KB Output is correct
91 Correct 425 ms 113076 KB Output is correct
92 Correct 237 ms 113076 KB Output is correct
93 Correct 239 ms 113076 KB Output is correct
94 Correct 238 ms 113076 KB Output is correct
95 Correct 272 ms 124484 KB Output is correct
96 Correct 282 ms 126360 KB Output is correct
97 Correct 269 ms 128612 KB Output is correct
98 Correct 275 ms 130596 KB Output is correct
99 Correct 290 ms 131764 KB Output is correct
100 Correct 524 ms 131764 KB Output is correct
101 Correct 621 ms 131764 KB Output is correct
102 Correct 605 ms 131764 KB Output is correct
103 Correct 599 ms 131764 KB Output is correct
104 Correct 276 ms 141764 KB Output is correct
105 Correct 269 ms 143880 KB Output is correct
106 Correct 298 ms 145972 KB Output is correct
107 Correct 389 ms 145972 KB Output is correct
108 Correct 418 ms 147168 KB Output is correct
109 Correct 414 ms 148712 KB Output is correct
110 Correct 188 ms 148712 KB Output is correct
111 Correct 257 ms 155136 KB Output is correct
112 Correct 256 ms 156380 KB Output is correct
113 Correct 259 ms 156756 KB Output is correct
114 Correct 505 ms 156756 KB Output is correct
115 Correct 542 ms 156756 KB Output is correct
116 Correct 257 ms 161464 KB Output is correct
117 Correct 281 ms 162400 KB Output is correct
118 Correct 291 ms 163840 KB Output is correct
119 Correct 271 ms 165680 KB Output is correct
120 Correct 260 ms 167572 KB Output is correct
121 Correct 244 ms 168732 KB Output is correct
122 Correct 276 ms 170132 KB Output is correct
123 Correct 284 ms 172460 KB Output is correct
124 Correct 323 ms 173988 KB Output is correct
125 Correct 277 ms 175564 KB Output is correct
126 Correct 261 ms 177624 KB Output is correct
127 Correct 263 ms 178732 KB Output is correct
128 Correct 271 ms 180272 KB Output is correct