답안 #674052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674052 2022-12-22T18:03:33 Z ghostwriter Construction of Highway (JOI18_construction) C++17
100 / 100
242 ms 23112 KB
// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
    #include <debug.h>
#else
    #define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
// #define int long long
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { T d2 = (a | b) & -(a | b); a /= d2; b /= d2; WHILE(b) { a = a % b; swap(a, b); } return a * d2; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
void _assert(bool statement) { if (statement) return; cerr << "\n>> Assertion failed!\n"; exit(0); }
void _assert(bool statement, const str &message) { if (statement) return; cerr << "\n>> Assertion failed: " << message << '\n'; exit(0); }
void _error(const str &message) { cerr << "\n>> Error: " << message << '\n'; exit(0); }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    GOAT
----------------------------------------------------------------
*/
const int N = 1e5 + 5;
int n, f[N], c[N], s[N], h[N], p[N], heavy[N], pos[N], head[N], num[N], timer = 1;
vi adj[N];
vpi a[N], hist;
pi e[N];
void input(int test_id) {
	cin >> n;
	FOR(i, 1, n) cin >> c[i];
	FOR(i, 1, n - 1) {
		cin >> e[i].st >> e[i].nd;
		adj[e[i].st].pb(e[i].nd);
	}
}
void compress(int c[]) {
	vi v;
	FOR(i, 1, n) v.pb(c[i]);
	sort(all(v));
	FOR(i, 1, n) c[i] = lb(all(v), c[i]) - v.bg() + 1;
}
void upd(int pos, int v) { hist.pb({pos, v}); for (int i = pos; i <= n; i += (i & -i)) f[i] += v; }
int get(int pos) { int rs = 0; for (int i = pos; i > 0; i -= (i & -i)) rs += f[i]; return rs; }
void clear() { EACH(j, hist) { int pos = j.st, v = j.nd; for (int i = pos; i <= n; i += (i & -i)) f[i] -= v; } hist.clear(); }
void dfs(int u) {
	EACH(v, adj[u]) {
		h[v] = h[u] + 1;
		p[v] = u;
		dfs(v);
		s[u] += s[v];
		if (!heavy[u]) heavy[u] = v;
		else if (s[heavy[u]] < s[v]) heavy[v] = u;
	}
}
void hld(int u) {
	pos[u] = timer;
	if (a[pos[u]].empty()) head[pos[u]] = u;
	a[pos[u]].pb({c[u], 1});
	num[u] = sz(a[pos[u]]);
	if (heavy[u]) hld(heavy[u]);
	EACH(v, adj[u]) {
		if (v == heavy[u]) continue;
		++timer;
		hld(v);
	}
}
ll get(int u, int v) {
	ll rs = 0;
	WHILE(u) {
		vpi a1;
		int cpos = pos[u], cnum = num[u];
		FSN(j, sz(a[cpos])) {
			if (!cnum) break;
			int tk = min(cnum, a[cpos][j].nd);
			bool ok = 1;
			if (tk == a[cpos][j].nd) a[cpos]._pb();
			else {
				a[cpos][j].nd -= tk;
				ok = 0;
			}
			cnum -= tk;
			a1.pb({a[cpos][j].st, tk});
			if (!ok) break;
		}
		reverse(all(a1));
		EACH(i, a1) {
			rs += 1LL * i.nd * get(i.st - 1);
			upd(i.st, i.nd);
		}
		a[cpos].pb({v, num[u]});
		u = p[head[cpos]];
	}
	clear();
	return rs;
}
void solve(int test_id) {
	compress(c);
	dfs(1);
	hld(1);
	FOR(i, 1, timer) reverse(all(a[i]));
	FOR(i, 1, n - 1) {
		int u = e[i].st, v = e[i].nd;
		cout << get(u, c[v]) << '\n';
	}
}
void reinit(int test_id) {

}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    int test_num = 1;
    // cin >> test_num; // comment if the problem does not requires multitest
    FOR(i, 1, test_num) {
        input(i); // input in noninteractive problems for case #i
        solve(i); // main function to solve case #i
        reinit(i); // reinit global data to default used in case #i
    }
    #ifdef LOCAL
        cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms.\n";
    #endif
    return 0;
}
/*
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 5032 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 5044 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5044 KB Output is correct
12 Correct 3 ms 5044 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5048 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5048 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5044 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5044 KB Output is correct
30 Correct 3 ms 5040 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5096 KB Output is correct
34 Correct 3 ms 5044 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5048 KB Output is correct
38 Correct 3 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 4 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 5032 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 5044 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5044 KB Output is correct
12 Correct 3 ms 5044 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5048 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5048 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5044 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5044 KB Output is correct
30 Correct 3 ms 5040 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5096 KB Output is correct
34 Correct 3 ms 5044 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5048 KB Output is correct
38 Correct 3 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 4 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 3 ms 5068 KB Output is correct
46 Correct 7 ms 5460 KB Output is correct
47 Correct 7 ms 5412 KB Output is correct
48 Correct 7 ms 5460 KB Output is correct
49 Correct 5 ms 5816 KB Output is correct
50 Correct 5 ms 5716 KB Output is correct
51 Correct 5 ms 5716 KB Output is correct
52 Correct 7 ms 5560 KB Output is correct
53 Correct 5 ms 5588 KB Output is correct
54 Correct 6 ms 5640 KB Output is correct
55 Correct 6 ms 5588 KB Output is correct
56 Correct 6 ms 5512 KB Output is correct
57 Correct 7 ms 5440 KB Output is correct
58 Correct 9 ms 5460 KB Output is correct
59 Correct 8 ms 5476 KB Output is correct
60 Correct 8 ms 5432 KB Output is correct
61 Correct 5 ms 5516 KB Output is correct
62 Correct 6 ms 5588 KB Output is correct
63 Correct 6 ms 5588 KB Output is correct
64 Correct 6 ms 5332 KB Output is correct
65 Correct 6 ms 5364 KB Output is correct
66 Correct 7 ms 5332 KB Output is correct
67 Correct 7 ms 5332 KB Output is correct
68 Correct 5 ms 5752 KB Output is correct
69 Correct 5 ms 5560 KB Output is correct
70 Correct 5 ms 5620 KB Output is correct
71 Correct 5 ms 5568 KB Output is correct
72 Correct 8 ms 5440 KB Output is correct
73 Correct 9 ms 5332 KB Output is correct
74 Correct 5 ms 5564 KB Output is correct
75 Correct 5 ms 5436 KB Output is correct
76 Correct 6 ms 5436 KB Output is correct
77 Correct 5 ms 5460 KB Output is correct
78 Correct 6 ms 5436 KB Output is correct
79 Correct 6 ms 5460 KB Output is correct
80 Correct 5 ms 5460 KB Output is correct
81 Correct 6 ms 5432 KB Output is correct
82 Correct 6 ms 5492 KB Output is correct
83 Correct 6 ms 5436 KB Output is correct
84 Correct 7 ms 5460 KB Output is correct
85 Correct 5 ms 5460 KB Output is correct
86 Correct 6 ms 5360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 5032 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 5044 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5044 KB Output is correct
12 Correct 3 ms 5044 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5048 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5048 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5044 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5044 KB Output is correct
30 Correct 3 ms 5040 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5096 KB Output is correct
34 Correct 3 ms 5044 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5048 KB Output is correct
38 Correct 3 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 4 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 3 ms 5068 KB Output is correct
46 Correct 7 ms 5460 KB Output is correct
47 Correct 7 ms 5412 KB Output is correct
48 Correct 7 ms 5460 KB Output is correct
49 Correct 5 ms 5816 KB Output is correct
50 Correct 5 ms 5716 KB Output is correct
51 Correct 5 ms 5716 KB Output is correct
52 Correct 7 ms 5560 KB Output is correct
53 Correct 5 ms 5588 KB Output is correct
54 Correct 6 ms 5640 KB Output is correct
55 Correct 6 ms 5588 KB Output is correct
56 Correct 6 ms 5512 KB Output is correct
57 Correct 7 ms 5440 KB Output is correct
58 Correct 9 ms 5460 KB Output is correct
59 Correct 8 ms 5476 KB Output is correct
60 Correct 8 ms 5432 KB Output is correct
61 Correct 5 ms 5516 KB Output is correct
62 Correct 6 ms 5588 KB Output is correct
63 Correct 6 ms 5588 KB Output is correct
64 Correct 6 ms 5332 KB Output is correct
65 Correct 6 ms 5364 KB Output is correct
66 Correct 7 ms 5332 KB Output is correct
67 Correct 7 ms 5332 KB Output is correct
68 Correct 5 ms 5752 KB Output is correct
69 Correct 5 ms 5560 KB Output is correct
70 Correct 5 ms 5620 KB Output is correct
71 Correct 5 ms 5568 KB Output is correct
72 Correct 8 ms 5440 KB Output is correct
73 Correct 9 ms 5332 KB Output is correct
74 Correct 5 ms 5564 KB Output is correct
75 Correct 5 ms 5436 KB Output is correct
76 Correct 6 ms 5436 KB Output is correct
77 Correct 5 ms 5460 KB Output is correct
78 Correct 6 ms 5436 KB Output is correct
79 Correct 6 ms 5460 KB Output is correct
80 Correct 5 ms 5460 KB Output is correct
81 Correct 6 ms 5432 KB Output is correct
82 Correct 6 ms 5492 KB Output is correct
83 Correct 6 ms 5436 KB Output is correct
84 Correct 7 ms 5460 KB Output is correct
85 Correct 5 ms 5460 KB Output is correct
86 Correct 6 ms 5360 KB Output is correct
87 Correct 16 ms 5972 KB Output is correct
88 Correct 47 ms 7996 KB Output is correct
89 Correct 178 ms 14984 KB Output is correct
90 Correct 183 ms 15036 KB Output is correct
91 Correct 163 ms 15000 KB Output is correct
92 Correct 90 ms 23100 KB Output is correct
93 Correct 92 ms 23112 KB Output is correct
94 Correct 85 ms 23100 KB Output is correct
95 Correct 85 ms 19740 KB Output is correct
96 Correct 86 ms 20120 KB Output is correct
97 Correct 88 ms 20104 KB Output is correct
98 Correct 92 ms 20152 KB Output is correct
99 Correct 92 ms 19036 KB Output is correct
100 Correct 205 ms 14684 KB Output is correct
101 Correct 241 ms 14980 KB Output is correct
102 Correct 231 ms 14988 KB Output is correct
103 Correct 242 ms 14940 KB Output is correct
104 Correct 105 ms 19008 KB Output is correct
105 Correct 99 ms 18908 KB Output is correct
106 Correct 98 ms 19048 KB Output is correct
107 Correct 145 ms 13860 KB Output is correct
108 Correct 164 ms 14304 KB Output is correct
109 Correct 168 ms 14620 KB Output is correct
110 Correct 80 ms 22444 KB Output is correct
111 Correct 86 ms 19744 KB Output is correct
112 Correct 77 ms 19524 KB Output is correct
113 Correct 85 ms 18600 KB Output is correct
114 Correct 215 ms 14760 KB Output is correct
115 Correct 235 ms 14288 KB Output is correct
116 Correct 98 ms 18364 KB Output is correct
117 Correct 91 ms 16692 KB Output is correct
118 Correct 85 ms 16004 KB Output is correct
119 Correct 90 ms 15736 KB Output is correct
120 Correct 80 ms 16308 KB Output is correct
121 Correct 84 ms 15692 KB Output is correct
122 Correct 84 ms 15408 KB Output is correct
123 Correct 100 ms 16764 KB Output is correct
124 Correct 100 ms 16072 KB Output is correct
125 Correct 96 ms 15752 KB Output is correct
126 Correct 96 ms 16452 KB Output is correct
127 Correct 101 ms 15888 KB Output is correct
128 Correct 92 ms 15480 KB Output is correct