Submission #718624

# Submission time Handle Problem Language Result Execution time Memory
718624 2023-04-04T12:34:35 Z NothingXD Construction of Highway (JOI18_construction) C++17
100 / 100
1022 ms 24132 KB
/*

   Wei Wai Wei Wai
   Zumigat nan damu dimi kwayt rayt

*/

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
/*typedef __uint128_t L;
  struct FastMod {
  ull b, m;
  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
  ull reduce(ull a) {
  ull q = (ull)((L(m) * a) >> 64);
  ull r = a - q * b; // can be proven that 0 <= r < 2*b
  return r >= b ? r - b : r;
  }
  };
  FastMod FM(2);*/
void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 1e5 + 10;

int n, a[maxn], f[maxn], sz[maxn], chn[maxn], par[maxn], st[maxn], en[maxn], val[maxn], Q[maxn], tme;
vector<int> g[maxn], num;
vector<pii> ans;
set<pair<pii,int>> seg;

void add(int idx, int x){
	for (; idx <= n; idx += idx & -idx) f[idx] += x;
}

int get(int idx){
	int res = 0;
	for (; idx; idx -= idx & -idx) res += f[idx];
	return res;
}

int get(int l, int r){
	if (r < l) return 0;
	return get(r) - get(l-1);
}

void dfs(int v, int p = -1){
	par[v] = p;
	sz[v] = 1;
	for (auto u: g[v]){
		dfs(u, v);
		sz[u] += sz[v];
	}
}

void hld(int v, int c){
	st[v] = ++tme;
	chn[v] = c;
	int bc = -1;
	for (auto u: g[v]) if (bc == -1 || sz[bc] < sz[u]) bc = u;
	if (bc != -1) hld(bc, c);
	for (auto u: g[v]) if (u != bc) hld(u, u);
	en[v] = tme;
}

void change(int l, int r, int x){
	vector<pii> res;
	auto it = seg.lower_bound({{l, 0}, 0});
	if (it != seg.begin()){
		it--;
		pair<pii,int> tmp = *it;
		if (tmp.F.S >= l){
			res.push_back({tmp.S, tmp.F.S - l + 1});
			seg.erase(it);
			seg.insert({{tmp.F.F, l-1}, tmp.S});
		}
	}
	while(!seg.empty()){
		auto it = seg.lower_bound({{l, 0}, 0});
		if (it == seg.end() || (*it).F.F > r) break;
		pair<pii,int> tmp = *it;
		if (tmp.F.S <= r){
			res.push_back({tmp.S, tmp.F.S - tmp.F.F + 1});
			seg.erase(it);
		}
		else{
			res.push_back({tmp.S, r - tmp.F.F + 1});
			seg.erase(it);
			seg.insert({{r+1, tmp.F.S}, tmp.S});
		}
	}
	seg.insert({{l, r}, x});
	reverse(all(res));
	for (auto x: res) ans.push_back(x);
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> n;

	for (int i = 1; i <= n; i++){
		cin >> a[i];
		num.push_back(a[i]);
	}

	sort(all(num));
	num.resize(distance(num.begin(), unique(all(num))));

	for (int i = 1; i <= n; i++){
		a[i] = lower_bound(all(num), a[i]) - num.begin() + 1;
	}

	for (int i = 1; i < n; i++){
		int x; cin >> x >> Q[i];
		g[x].push_back(Q[i]);
	}

	dfs(1);
	hld(1, 1);
	for (int i = 1; i <= n; i++){
		seg.insert({{st[i], st[i]}, a[i]});
	}

	for (int i = 1; i < n; i++){
		ans.clear();
		int v = par[Q[i]];
		while(v != -1){
			change(st[chn[v]], st[v], a[Q[i]]);
			v = par[chn[v]];
		}
		reverse(all(ans));
		ll res = 0;
		for (auto [x, y]: ans){
			res += 1ll * get(x+1, n) * y;
			add(x, y);
		}
		cout << res << '\n';
		for (auto [x, y]: ans){
			add(x, -y);
		}
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 3 ms 2684 KB Output is correct
6 Correct 3 ms 2688 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 2 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 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2644 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 3 ms 2772 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
23 Correct 4 ms 2772 KB Output is correct
24 Correct 3 ms 2680 KB Output is correct
25 Correct 3 ms 2644 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 3 ms 2684 KB Output is correct
28 Correct 3 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 3 ms 2772 KB Output is correct
31 Correct 3 ms 2684 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 3 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 3 ms 2688 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 3 ms 2772 KB Output is correct
43 Correct 3 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 3 ms 2684 KB Output is correct
6 Correct 3 ms 2688 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 2 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 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2644 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 3 ms 2772 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
23 Correct 4 ms 2772 KB Output is correct
24 Correct 3 ms 2680 KB Output is correct
25 Correct 3 ms 2644 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 3 ms 2684 KB Output is correct
28 Correct 3 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 3 ms 2772 KB Output is correct
31 Correct 3 ms 2684 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 3 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 3 ms 2688 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 3 ms 2772 KB Output is correct
43 Correct 3 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 4 ms 2748 KB Output is correct
46 Correct 12 ms 3156 KB Output is correct
47 Correct 13 ms 3156 KB Output is correct
48 Correct 12 ms 3148 KB Output is correct
49 Correct 6 ms 3540 KB Output is correct
50 Correct 6 ms 3540 KB Output is correct
51 Correct 6 ms 3540 KB Output is correct
52 Correct 6 ms 3284 KB Output is correct
53 Correct 6 ms 3412 KB Output is correct
54 Correct 6 ms 3328 KB Output is correct
55 Correct 6 ms 3412 KB Output is correct
56 Correct 6 ms 3284 KB Output is correct
57 Correct 19 ms 3216 KB Output is correct
58 Correct 19 ms 3256 KB Output is correct
59 Correct 18 ms 3156 KB Output is correct
60 Correct 18 ms 3240 KB Output is correct
61 Correct 6 ms 3284 KB Output is correct
62 Correct 6 ms 3332 KB Output is correct
63 Correct 6 ms 3284 KB Output is correct
64 Correct 12 ms 3164 KB Output is correct
65 Correct 12 ms 3200 KB Output is correct
66 Correct 15 ms 3156 KB Output is correct
67 Correct 13 ms 3156 KB Output is correct
68 Correct 7 ms 3464 KB Output is correct
69 Correct 6 ms 3332 KB Output is correct
70 Correct 6 ms 3284 KB Output is correct
71 Correct 5 ms 3284 KB Output is correct
72 Correct 18 ms 3276 KB Output is correct
73 Correct 20 ms 3204 KB Output is correct
74 Correct 6 ms 3328 KB Output is correct
75 Correct 6 ms 3208 KB Output is correct
76 Correct 6 ms 3204 KB Output is correct
77 Correct 6 ms 3204 KB Output is correct
78 Correct 7 ms 3204 KB Output is correct
79 Correct 6 ms 3208 KB Output is correct
80 Correct 7 ms 3156 KB Output is correct
81 Correct 7 ms 3284 KB Output is correct
82 Correct 6 ms 3156 KB Output is correct
83 Correct 7 ms 3156 KB Output is correct
84 Correct 6 ms 3156 KB Output is correct
85 Correct 6 ms 3156 KB Output is correct
86 Correct 8 ms 3212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 3 ms 2684 KB Output is correct
6 Correct 3 ms 2688 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 2 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 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2644 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 3 ms 2772 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
23 Correct 4 ms 2772 KB Output is correct
24 Correct 3 ms 2680 KB Output is correct
25 Correct 3 ms 2644 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 3 ms 2684 KB Output is correct
28 Correct 3 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 3 ms 2772 KB Output is correct
31 Correct 3 ms 2684 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 3 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 3 ms 2688 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 3 ms 2772 KB Output is correct
43 Correct 3 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 4 ms 2748 KB Output is correct
46 Correct 12 ms 3156 KB Output is correct
47 Correct 13 ms 3156 KB Output is correct
48 Correct 12 ms 3148 KB Output is correct
49 Correct 6 ms 3540 KB Output is correct
50 Correct 6 ms 3540 KB Output is correct
51 Correct 6 ms 3540 KB Output is correct
52 Correct 6 ms 3284 KB Output is correct
53 Correct 6 ms 3412 KB Output is correct
54 Correct 6 ms 3328 KB Output is correct
55 Correct 6 ms 3412 KB Output is correct
56 Correct 6 ms 3284 KB Output is correct
57 Correct 19 ms 3216 KB Output is correct
58 Correct 19 ms 3256 KB Output is correct
59 Correct 18 ms 3156 KB Output is correct
60 Correct 18 ms 3240 KB Output is correct
61 Correct 6 ms 3284 KB Output is correct
62 Correct 6 ms 3332 KB Output is correct
63 Correct 6 ms 3284 KB Output is correct
64 Correct 12 ms 3164 KB Output is correct
65 Correct 12 ms 3200 KB Output is correct
66 Correct 15 ms 3156 KB Output is correct
67 Correct 13 ms 3156 KB Output is correct
68 Correct 7 ms 3464 KB Output is correct
69 Correct 6 ms 3332 KB Output is correct
70 Correct 6 ms 3284 KB Output is correct
71 Correct 5 ms 3284 KB Output is correct
72 Correct 18 ms 3276 KB Output is correct
73 Correct 20 ms 3204 KB Output is correct
74 Correct 6 ms 3328 KB Output is correct
75 Correct 6 ms 3208 KB Output is correct
76 Correct 6 ms 3204 KB Output is correct
77 Correct 6 ms 3204 KB Output is correct
78 Correct 7 ms 3204 KB Output is correct
79 Correct 6 ms 3208 KB Output is correct
80 Correct 7 ms 3156 KB Output is correct
81 Correct 7 ms 3284 KB Output is correct
82 Correct 6 ms 3156 KB Output is correct
83 Correct 7 ms 3156 KB Output is correct
84 Correct 6 ms 3156 KB Output is correct
85 Correct 6 ms 3156 KB Output is correct
86 Correct 8 ms 3212 KB Output is correct
87 Correct 36 ms 4144 KB Output is correct
88 Correct 134 ms 6784 KB Output is correct
89 Correct 534 ms 16504 KB Output is correct
90 Correct 614 ms 16480 KB Output is correct
91 Correct 550 ms 16444 KB Output is correct
92 Correct 154 ms 24060 KB Output is correct
93 Correct 166 ms 24132 KB Output is correct
94 Correct 142 ms 24048 KB Output is correct
95 Correct 157 ms 20236 KB Output is correct
96 Correct 173 ms 20544 KB Output is correct
97 Correct 169 ms 20572 KB Output is correct
98 Correct 171 ms 20560 KB Output is correct
99 Correct 141 ms 19608 KB Output is correct
100 Correct 993 ms 16588 KB Output is correct
101 Correct 1022 ms 16848 KB Output is correct
102 Correct 959 ms 17100 KB Output is correct
103 Correct 973 ms 17000 KB Output is correct
104 Correct 168 ms 19584 KB Output is correct
105 Correct 148 ms 19548 KB Output is correct
106 Correct 186 ms 19568 KB Output is correct
107 Correct 520 ms 15412 KB Output is correct
108 Correct 537 ms 15480 KB Output is correct
109 Correct 523 ms 15812 KB Output is correct
110 Correct 128 ms 23132 KB Output is correct
111 Correct 154 ms 20228 KB Output is correct
112 Correct 154 ms 19584 KB Output is correct
113 Correct 132 ms 18632 KB Output is correct
114 Correct 889 ms 16708 KB Output is correct
115 Correct 870 ms 15872 KB Output is correct
116 Correct 155 ms 18560 KB Output is correct
117 Correct 160 ms 17720 KB Output is correct
118 Correct 155 ms 17184 KB Output is correct
119 Correct 167 ms 16844 KB Output is correct
120 Correct 139 ms 16852 KB Output is correct
121 Correct 157 ms 16488 KB Output is correct
122 Correct 157 ms 16216 KB Output is correct
123 Correct 165 ms 17788 KB Output is correct
124 Correct 218 ms 17228 KB Output is correct
125 Correct 210 ms 16916 KB Output is correct
126 Correct 189 ms 17020 KB Output is correct
127 Correct 207 ms 16548 KB Output is correct
128 Correct 198 ms 16304 KB Output is correct