Submission #716470

# Submission time Handle Problem Language Result Execution time Memory
716470 2023-03-30T06:51:51 Z pavement Construction of Highway (JOI18_construction) C++17
100 / 100
839 ms 37184 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 mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

int N, idx, u[100005], v[100005], C[100005], par[100005], sz[100005], hv[100005], pos[100005], rev[100005], ft[100005];
vector<int> disc, adj[100005];
vector<iii> segs;
stack<int> rb;

int ls(int x) {
	return x & -x;
}

void reset() {
	while (!rb.empty()) {
		int x = rb.top();
		ft[x] = 0;
		rb.pop();
	}
}

void upd(int p, int v) {
	for (; p <= N; p += ls(p)) {
		rb.push(p);
		ft[p] += v;
	}
}

int qry(int p) {
	int r = 0;
	for (; p; p -= ls(p)) r += ft[p];
	return r;
}

struct node {
	node *left, *right;
	int S, E, mxval, mival, pv;
	bool ip;
	node(int _s, int _e) : S(_s), E(_e), ip(0) {
		if (S == E) {
			mxval = mival = C[rev[S]];
			return;
		}
		int M = (S + E) >> 1;
		left = new node(S, M);
		right = new node(M + 1, E);
		mxval = max(left->mxval, right->mxval);
		mival = min(left->mival, right->mival);
	}
	void prop() {
		if (S == E || !ip) return;
		left->mxval = left->mival = left->pv = right->mxval = right->mival = right->pv = pv;
		left->ip = right->ip = 1;
		ip = 0;
	}
	void get_seg(int l, int r) {
		if (l > E || r < S) return;
		if (l <= S && E <= r && mxval == mival) {
			segs.eb(S, E, mxval);
			return;
		}
		prop();
		left->get_seg(l, r);
		right->get_seg(l, r);
	}
	void set(int l, int r, int v) {
		if (l > E || r < S) return;
		if (l <= S && E <= r) {
			mxval = mival = pv = v;
			ip = 1;
			return;
		}
		prop();
		left->set(l, r, v);
		right->set(l, r, v);
		mxval = max(left->mxval, right->mxval);
		mival = min(left->mival, right->mival);
	}
} *root;

int get_sz(int n) {
	sz[n] = 1;
	for (auto u : adj[n]) {
		sz[n] += get_sz(u);
	}
	return sz[n];
}

void dfs(int n) {
	pos[n] = ++idx;
	rev[pos[n]] = n;
	int cur_mx = -1;
	for (int i = 0; i < (int)adj[n].size(); i++) {
		if (sz[adj[n][i]] > cur_mx) {
			cur_mx = sz[adj[n][i]];
			swap(adj[n][0], adj[n][i]);
		}
	}
	for (int i = 0; i < (int)adj[n].size(); i++) {
		if (i == 0) {
			hv[adj[n][i]] = hv[n];
		} else {
			hv[adj[n][i]] = adj[n][i];
		}
		dfs(adj[n][i]);
	}
}

main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> N;
	for (int i = 1; i <= N; i++) {
		cin >> C[i];
		disc.pb(C[i]);
	}
	sort(disc.begin(), disc.end());
	for (int i = 1; i <= N; i++) {
		C[i] = lower_bound(disc.begin(), disc.end(), C[i]) - disc.begin() + 1;
	}
	for (int i = 1; i < N; i++) {
		cin >> u[i] >> v[i];
		par[v[i]] = u[i];
		adj[u[i]].pb(v[i]);
	}
	get_sz(1);
	hv[1] = 1;
	dfs(1);
	root = new node(1, N);
	for (int i = 1; i < N; i++) {
		segs.clear();
		for (int x = u[i]; x; x = par[hv[x]]) {
			root->get_seg(pos[hv[x]], pos[x]);
			root->set(pos[hv[x]], pos[x], C[v[i]]);
		}
		int cur = 0;
		sort(segs.begin(), segs.end());
		reset();
		for (auto [l, r, val] : segs) {
			cur += qry(N - val) * (r - l + 1);
			upd(N - val + 1, r - l + 1);
		}
		cout << cur << '\n';
	}
}

Compilation message

construction.cpp:132:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  132 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 3 ms 2864 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2816 KB Output is correct
16 Correct 3 ms 2824 KB Output is correct
17 Correct 3 ms 2820 KB Output is correct
18 Correct 3 ms 2828 KB Output is correct
19 Correct 3 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2812 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 3 ms 2812 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 3 ms 2772 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 3 ms 2820 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 3 ms 2772 KB Output is correct
35 Correct 3 ms 2732 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 4 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 3 ms 2812 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2820 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 3 ms 2864 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2816 KB Output is correct
16 Correct 3 ms 2824 KB Output is correct
17 Correct 3 ms 2820 KB Output is correct
18 Correct 3 ms 2828 KB Output is correct
19 Correct 3 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2812 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 3 ms 2812 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 3 ms 2772 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 3 ms 2820 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 3 ms 2772 KB Output is correct
35 Correct 3 ms 2732 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 4 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 3 ms 2812 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2820 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 3 ms 2900 KB Output is correct
46 Correct 9 ms 3820 KB Output is correct
47 Correct 10 ms 3720 KB Output is correct
48 Correct 10 ms 3832 KB Output is correct
49 Correct 6 ms 4052 KB Output is correct
50 Correct 6 ms 3936 KB Output is correct
51 Correct 7 ms 4052 KB Output is correct
52 Correct 7 ms 3924 KB Output is correct
53 Correct 9 ms 4044 KB Output is correct
54 Correct 7 ms 3992 KB Output is correct
55 Correct 6 ms 4044 KB Output is correct
56 Correct 7 ms 3832 KB Output is correct
57 Correct 12 ms 3760 KB Output is correct
58 Correct 14 ms 3812 KB Output is correct
59 Correct 13 ms 3724 KB Output is correct
60 Correct 12 ms 3796 KB Output is correct
61 Correct 7 ms 3796 KB Output is correct
62 Correct 10 ms 3888 KB Output is correct
63 Correct 8 ms 3796 KB Output is correct
64 Correct 10 ms 3716 KB Output is correct
65 Correct 11 ms 3720 KB Output is correct
66 Correct 10 ms 3796 KB Output is correct
67 Correct 9 ms 3720 KB Output is correct
68 Correct 6 ms 3976 KB Output is correct
69 Correct 7 ms 3924 KB Output is correct
70 Correct 6 ms 3924 KB Output is correct
71 Correct 6 ms 3796 KB Output is correct
72 Correct 13 ms 3840 KB Output is correct
73 Correct 12 ms 3776 KB Output is correct
74 Correct 7 ms 3796 KB Output is correct
75 Correct 7 ms 3796 KB Output is correct
76 Correct 7 ms 3828 KB Output is correct
77 Correct 8 ms 3820 KB Output is correct
78 Correct 7 ms 3720 KB Output is correct
79 Correct 7 ms 3800 KB Output is correct
80 Correct 7 ms 3796 KB Output is correct
81 Correct 9 ms 3820 KB Output is correct
82 Correct 8 ms 3720 KB Output is correct
83 Correct 8 ms 3796 KB Output is correct
84 Correct 8 ms 3796 KB Output is correct
85 Correct 8 ms 3796 KB Output is correct
86 Correct 9 ms 3800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 3 ms 2864 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2816 KB Output is correct
16 Correct 3 ms 2824 KB Output is correct
17 Correct 3 ms 2820 KB Output is correct
18 Correct 3 ms 2828 KB Output is correct
19 Correct 3 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2812 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 3 ms 2812 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 3 ms 2772 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 3 ms 2820 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 3 ms 2772 KB Output is correct
35 Correct 3 ms 2732 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 4 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 3 ms 2812 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2820 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 3 ms 2900 KB Output is correct
46 Correct 9 ms 3820 KB Output is correct
47 Correct 10 ms 3720 KB Output is correct
48 Correct 10 ms 3832 KB Output is correct
49 Correct 6 ms 4052 KB Output is correct
50 Correct 6 ms 3936 KB Output is correct
51 Correct 7 ms 4052 KB Output is correct
52 Correct 7 ms 3924 KB Output is correct
53 Correct 9 ms 4044 KB Output is correct
54 Correct 7 ms 3992 KB Output is correct
55 Correct 6 ms 4044 KB Output is correct
56 Correct 7 ms 3832 KB Output is correct
57 Correct 12 ms 3760 KB Output is correct
58 Correct 14 ms 3812 KB Output is correct
59 Correct 13 ms 3724 KB Output is correct
60 Correct 12 ms 3796 KB Output is correct
61 Correct 7 ms 3796 KB Output is correct
62 Correct 10 ms 3888 KB Output is correct
63 Correct 8 ms 3796 KB Output is correct
64 Correct 10 ms 3716 KB Output is correct
65 Correct 11 ms 3720 KB Output is correct
66 Correct 10 ms 3796 KB Output is correct
67 Correct 9 ms 3720 KB Output is correct
68 Correct 6 ms 3976 KB Output is correct
69 Correct 7 ms 3924 KB Output is correct
70 Correct 6 ms 3924 KB Output is correct
71 Correct 6 ms 3796 KB Output is correct
72 Correct 13 ms 3840 KB Output is correct
73 Correct 12 ms 3776 KB Output is correct
74 Correct 7 ms 3796 KB Output is correct
75 Correct 7 ms 3796 KB Output is correct
76 Correct 7 ms 3828 KB Output is correct
77 Correct 8 ms 3820 KB Output is correct
78 Correct 7 ms 3720 KB Output is correct
79 Correct 7 ms 3800 KB Output is correct
80 Correct 7 ms 3796 KB Output is correct
81 Correct 9 ms 3820 KB Output is correct
82 Correct 8 ms 3720 KB Output is correct
83 Correct 8 ms 3796 KB Output is correct
84 Correct 8 ms 3796 KB Output is correct
85 Correct 8 ms 3796 KB Output is correct
86 Correct 9 ms 3800 KB Output is correct
87 Correct 26 ms 5460 KB Output is correct
88 Correct 109 ms 11092 KB Output is correct
89 Correct 413 ms 30412 KB Output is correct
90 Correct 428 ms 30384 KB Output is correct
91 Correct 439 ms 30332 KB Output is correct
92 Correct 189 ms 36260 KB Output is correct
93 Correct 182 ms 36356 KB Output is correct
94 Correct 188 ms 36228 KB Output is correct
95 Correct 176 ms 36928 KB Output is correct
96 Correct 168 ms 37156 KB Output is correct
97 Correct 169 ms 37136 KB Output is correct
98 Correct 174 ms 37184 KB Output is correct
99 Correct 174 ms 32544 KB Output is correct
100 Correct 582 ms 30304 KB Output is correct
101 Correct 636 ms 30700 KB Output is correct
102 Correct 839 ms 30696 KB Output is correct
103 Correct 837 ms 30672 KB Output is correct
104 Correct 286 ms 32452 KB Output is correct
105 Correct 247 ms 32520 KB Output is correct
106 Correct 276 ms 32580 KB Output is correct
107 Correct 368 ms 28780 KB Output is correct
108 Correct 400 ms 29648 KB Output is correct
109 Correct 433 ms 29956 KB Output is correct
110 Correct 177 ms 35668 KB Output is correct
111 Correct 170 ms 36936 KB Output is correct
112 Correct 181 ms 36584 KB Output is correct
113 Correct 162 ms 31812 KB Output is correct
114 Correct 542 ms 30364 KB Output is correct
115 Correct 603 ms 30044 KB Output is correct
116 Correct 228 ms 31900 KB Output is correct
117 Correct 235 ms 31056 KB Output is correct
118 Correct 208 ms 30756 KB Output is correct
119 Correct 254 ms 30400 KB Output is correct
120 Correct 230 ms 30624 KB Output is correct
121 Correct 198 ms 30248 KB Output is correct
122 Correct 244 ms 30104 KB Output is correct
123 Correct 317 ms 31020 KB Output is correct
124 Correct 313 ms 30632 KB Output is correct
125 Correct 326 ms 30492 KB Output is correct
126 Correct 325 ms 30724 KB Output is correct
127 Correct 278 ms 30472 KB Output is correct
128 Correct 323 ms 30184 KB Output is correct