Submission #740933

# Submission time Handle Problem Language Result Execution time Memory
740933 2023-05-13T09:52:30 Z pavement Two Currencies (JOI23_currencies) C++17
100 / 100
2370 ms 59952 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, M, Q, idx, A[100005], B[100005], P[100005], C[100005], S[100005], T[100005], U[100005], X[100005], Y[100005], lo[100005], hi[100005], pre[100005], sz[100005], dep[100005], ckpt[100005], anc[25][100005];
bool can[100005];
ii ft[100005];
iii ans[100005];
vector<int> adj[100005];
map<int, vector<int> > add, ask;

int dfs(int n, int e = -1) {
	for (int k = 1; k <= 20; k++) {
		if (anc[k - 1][n] == -1) break;
		anc[k][n] = anc[k - 1][anc[k - 1][n]];
	}
	pre[n] = ++idx;
	sz[n] = 1;
	for (auto u : adj[n]) if (u != e) {
		dep[u] = dep[n] + 1;
		anc[0][u] = n;
		sz[n] += dfs(u, n);
	}
	return sz[n];
}

int lca(int u, int v) {
	if (dep[u] > dep[v]) swap(u, v);
	for (int k = 20; k >= 0; k--) {
		if (dep[v] - (1 << k) >= dep[u]) {
			v = anc[k][v];
		}
	}
	if (u == v) return u;
	for (int k = 20; k >= 0; k--) {
		if (anc[k][u] != anc[k][v]) {
			u = anc[k][u];
			v = anc[k][v];
		}
	}
	return anc[0][u];
}

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

ii qry(int p) {
	ii ret = mp(0, 0);
	for (; p; p -= ls(p)) {
		ret.first += ft[p].first;
		ret.second += ft[p].second;
	}
	return ret;
}

void upd(int l, int r, ii v) {
	for (; l <= N; l += ls(l)) {
		ft[l].first += v.first;
		ft[l].second += v.second;
	}
	for (r++; r <= N; r += ls(r)) {
		ft[r].first -= v.first;
		ft[r].second -= v.second;
	}
}

main() {
	memset(anc, -1, sizeof anc);
	//~ ios::sync_with_stdio(0);
	//~ cin.tie(0);
	cin >> N >> M >> Q;
	for (int i = 1; i < N; i++) {
		cin >> A[i] >> B[i];
		adj[A[i]].pb(B[i]);
		adj[B[i]].pb(A[i]);
	}
	dfs(1);
	for (int i = 1; i <= M; i++) {
		cin >> P[i] >> C[i];
		if (dep[A[P[i]]] < dep[B[P[i]]]) P[i] = B[P[i]];
		else P[i] = A[P[i]];
	}
	for (int i = 1; i <= Q; i++) {
		cin >> S[i] >> T[i] >> X[i] >> Y[i];
		lo[i] = 1;
		hi[i] = (int)1e9;
		U[i] = lca(S[i], T[i]);
	}
	for (int rep = 0; rep < 30; rep++) {
		vector<ii> ev;
		for (int i = 1; i <= N; i++) {
			ft[i] = mp(0, 0);
		}
		for (int i = 1; i <= Q; i++) {
			can[i] = 0;
			int mid = (lo[i] + hi[i]) / 2;
			ev.eb(mid, i);
		}
		for (int i = 1; i <= M; i++) {
			ev.eb(C[i], -i);
		}
		sort(ev.begin(), ev.end());
		for (auto [v, idx] : ev) {
			if (idx < 0) {
				idx = -idx;
				upd(pre[P[idx]], pre[P[idx]] + sz[P[idx]] - 1, mp(v, 1ll));
			} else {
				auto r1 = qry(pre[S[idx]]);
				auto r2 = qry(pre[T[idx]]);
				auto r3 = qry(pre[U[idx]]);
				auto r = mp(r1.first + r2.first - 2 * r3.first, r1.second + r2.second - 2 * r3.second);
				if (r.first <= Y[idx]) {
					can[idx] = 1;
					g0(ans[idx]) = r.first;
					g1(ans[idx]) = r.second;
				}
			}
		}
		for (int i = 1; i <= Q; i++) {
			int mid = (lo[i] + hi[i]) / 2;
			if (can[i]) {
				g2(ans[i]) = mid;
				lo[i] = mid + 1;
			} else {
				hi[i] = mid - 1;
			}
		}
	}
	for (int i = 1; i <= Q; i++) {
		auto r1 = qry(pre[S[i]]);
		auto r2 = qry(pre[T[i]]);
		auto r3 = qry(pre[U[i]]);
		auto r = mp(r1.first + r2.first - 2 * r3.first, r1.second + r2.second - 2 * r3.second);
		ckpt[i] = r.second;
		auto [a, b, c] = ans[i];
		if (c == (int)1e9) continue;
		//~ cout << i << ' ' << a << ' ' << b << ' ' << c << ' ' << S[i] << ' ' << T[i] << ' ' << U[i] << '\n';
		int extra = (Y[i] - a) / (c + 1);
		g0(ans[i]) += extra * (c + 1);
		g1(ans[i]) += extra;
	}
	for (int i = 1; i <= Q; i++) {
		auto [x, y, z] = ans[i];
		cout << max(-1ll, X[i] - (ckpt[i] - y)) << '\n';
		//~ cout << X[i] << ' ' <<  x << ' ' << y << ' ' << z << ' ' << ckpt[i] << '\n';
	}
}

Compilation message

currencies.cpp:89:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   89 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 22228 KB Output is correct
2 Correct 11 ms 22228 KB Output is correct
3 Correct 10 ms 22228 KB Output is correct
4 Correct 10 ms 22228 KB Output is correct
5 Correct 27 ms 22776 KB Output is correct
6 Correct 28 ms 22888 KB Output is correct
7 Correct 27 ms 22780 KB Output is correct
8 Correct 29 ms 22896 KB Output is correct
9 Correct 29 ms 22896 KB Output is correct
10 Correct 31 ms 22860 KB Output is correct
11 Correct 31 ms 22892 KB Output is correct
12 Correct 31 ms 22904 KB Output is correct
13 Correct 30 ms 22988 KB Output is correct
14 Correct 30 ms 22956 KB Output is correct
15 Correct 31 ms 22936 KB Output is correct
16 Correct 29 ms 22920 KB Output is correct
17 Correct 29 ms 22928 KB Output is correct
18 Correct 30 ms 22936 KB Output is correct
19 Correct 29 ms 22824 KB Output is correct
20 Correct 30 ms 22880 KB Output is correct
21 Correct 31 ms 22892 KB Output is correct
22 Correct 32 ms 22876 KB Output is correct
23 Correct 28 ms 22868 KB Output is correct
24 Correct 29 ms 22868 KB Output is correct
25 Correct 31 ms 22900 KB Output is correct
26 Correct 28 ms 22896 KB Output is correct
27 Correct 26 ms 22904 KB Output is correct
28 Correct 26 ms 22868 KB Output is correct
29 Correct 30 ms 22852 KB Output is correct
30 Correct 32 ms 22856 KB Output is correct
31 Correct 28 ms 22824 KB Output is correct
32 Correct 30 ms 22908 KB Output is correct
33 Correct 28 ms 23004 KB Output is correct
34 Correct 28 ms 23016 KB Output is correct
35 Correct 30 ms 23008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 22228 KB Output is correct
2 Correct 24 ms 22880 KB Output is correct
3 Correct 24 ms 22904 KB Output is correct
4 Correct 29 ms 23020 KB Output is correct
5 Correct 1178 ms 48620 KB Output is correct
6 Correct 1311 ms 49456 KB Output is correct
7 Correct 1213 ms 49492 KB Output is correct
8 Correct 1027 ms 44568 KB Output is correct
9 Correct 946 ms 44136 KB Output is correct
10 Correct 1372 ms 53340 KB Output is correct
11 Correct 1387 ms 53484 KB Output is correct
12 Correct 1432 ms 53380 KB Output is correct
13 Correct 1377 ms 53476 KB Output is correct
14 Correct 1365 ms 53352 KB Output is correct
15 Correct 1414 ms 59136 KB Output is correct
16 Correct 1285 ms 59528 KB Output is correct
17 Correct 1396 ms 58816 KB Output is correct
18 Correct 1606 ms 53160 KB Output is correct
19 Correct 1672 ms 53408 KB Output is correct
20 Correct 1744 ms 53248 KB Output is correct
21 Correct 1445 ms 53016 KB Output is correct
22 Correct 1483 ms 53192 KB Output is correct
23 Correct 1453 ms 53084 KB Output is correct
24 Correct 1485 ms 53264 KB Output is correct
25 Correct 1292 ms 53672 KB Output is correct
26 Correct 1307 ms 53668 KB Output is correct
27 Correct 1134 ms 53764 KB Output is correct
28 Correct 1064 ms 53304 KB Output is correct
29 Correct 1047 ms 53344 KB Output is correct
30 Correct 1076 ms 53500 KB Output is correct
31 Correct 1146 ms 53528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 22228 KB Output is correct
2 Correct 28 ms 23024 KB Output is correct
3 Correct 28 ms 22996 KB Output is correct
4 Correct 29 ms 23132 KB Output is correct
5 Correct 1109 ms 50932 KB Output is correct
6 Correct 1025 ms 50396 KB Output is correct
7 Correct 1388 ms 50936 KB Output is correct
8 Correct 1927 ms 59764 KB Output is correct
9 Correct 1947 ms 59784 KB Output is correct
10 Correct 1946 ms 59760 KB Output is correct
11 Correct 1780 ms 59728 KB Output is correct
12 Correct 1705 ms 59952 KB Output is correct
13 Correct 1607 ms 59676 KB Output is correct
14 Correct 1580 ms 59628 KB Output is correct
15 Correct 1493 ms 59584 KB Output is correct
16 Correct 1767 ms 59772 KB Output is correct
17 Correct 1682 ms 59912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 22228 KB Output is correct
2 Correct 11 ms 22228 KB Output is correct
3 Correct 10 ms 22228 KB Output is correct
4 Correct 10 ms 22228 KB Output is correct
5 Correct 27 ms 22776 KB Output is correct
6 Correct 28 ms 22888 KB Output is correct
7 Correct 27 ms 22780 KB Output is correct
8 Correct 29 ms 22896 KB Output is correct
9 Correct 29 ms 22896 KB Output is correct
10 Correct 31 ms 22860 KB Output is correct
11 Correct 31 ms 22892 KB Output is correct
12 Correct 31 ms 22904 KB Output is correct
13 Correct 30 ms 22988 KB Output is correct
14 Correct 30 ms 22956 KB Output is correct
15 Correct 31 ms 22936 KB Output is correct
16 Correct 29 ms 22920 KB Output is correct
17 Correct 29 ms 22928 KB Output is correct
18 Correct 30 ms 22936 KB Output is correct
19 Correct 29 ms 22824 KB Output is correct
20 Correct 30 ms 22880 KB Output is correct
21 Correct 31 ms 22892 KB Output is correct
22 Correct 32 ms 22876 KB Output is correct
23 Correct 28 ms 22868 KB Output is correct
24 Correct 29 ms 22868 KB Output is correct
25 Correct 31 ms 22900 KB Output is correct
26 Correct 28 ms 22896 KB Output is correct
27 Correct 26 ms 22904 KB Output is correct
28 Correct 26 ms 22868 KB Output is correct
29 Correct 30 ms 22852 KB Output is correct
30 Correct 32 ms 22856 KB Output is correct
31 Correct 28 ms 22824 KB Output is correct
32 Correct 30 ms 22908 KB Output is correct
33 Correct 28 ms 23004 KB Output is correct
34 Correct 28 ms 23016 KB Output is correct
35 Correct 30 ms 23008 KB Output is correct
36 Correct 10 ms 22228 KB Output is correct
37 Correct 24 ms 22880 KB Output is correct
38 Correct 24 ms 22904 KB Output is correct
39 Correct 29 ms 23020 KB Output is correct
40 Correct 1178 ms 48620 KB Output is correct
41 Correct 1311 ms 49456 KB Output is correct
42 Correct 1213 ms 49492 KB Output is correct
43 Correct 1027 ms 44568 KB Output is correct
44 Correct 946 ms 44136 KB Output is correct
45 Correct 1372 ms 53340 KB Output is correct
46 Correct 1387 ms 53484 KB Output is correct
47 Correct 1432 ms 53380 KB Output is correct
48 Correct 1377 ms 53476 KB Output is correct
49 Correct 1365 ms 53352 KB Output is correct
50 Correct 1414 ms 59136 KB Output is correct
51 Correct 1285 ms 59528 KB Output is correct
52 Correct 1396 ms 58816 KB Output is correct
53 Correct 1606 ms 53160 KB Output is correct
54 Correct 1672 ms 53408 KB Output is correct
55 Correct 1744 ms 53248 KB Output is correct
56 Correct 1445 ms 53016 KB Output is correct
57 Correct 1483 ms 53192 KB Output is correct
58 Correct 1453 ms 53084 KB Output is correct
59 Correct 1485 ms 53264 KB Output is correct
60 Correct 1292 ms 53672 KB Output is correct
61 Correct 1307 ms 53668 KB Output is correct
62 Correct 1134 ms 53764 KB Output is correct
63 Correct 1064 ms 53304 KB Output is correct
64 Correct 1047 ms 53344 KB Output is correct
65 Correct 1076 ms 53500 KB Output is correct
66 Correct 1146 ms 53528 KB Output is correct
67 Correct 10 ms 22228 KB Output is correct
68 Correct 28 ms 23024 KB Output is correct
69 Correct 28 ms 22996 KB Output is correct
70 Correct 29 ms 23132 KB Output is correct
71 Correct 1109 ms 50932 KB Output is correct
72 Correct 1025 ms 50396 KB Output is correct
73 Correct 1388 ms 50936 KB Output is correct
74 Correct 1927 ms 59764 KB Output is correct
75 Correct 1947 ms 59784 KB Output is correct
76 Correct 1946 ms 59760 KB Output is correct
77 Correct 1780 ms 59728 KB Output is correct
78 Correct 1705 ms 59952 KB Output is correct
79 Correct 1607 ms 59676 KB Output is correct
80 Correct 1580 ms 59628 KB Output is correct
81 Correct 1493 ms 59584 KB Output is correct
82 Correct 1767 ms 59772 KB Output is correct
83 Correct 1682 ms 59912 KB Output is correct
84 Correct 1532 ms 47568 KB Output is correct
85 Correct 1313 ms 45544 KB Output is correct
86 Correct 1236 ms 44488 KB Output is correct
87 Correct 2021 ms 53404 KB Output is correct
88 Correct 1961 ms 53240 KB Output is correct
89 Correct 2119 ms 53416 KB Output is correct
90 Correct 2143 ms 53544 KB Output is correct
91 Correct 2140 ms 53520 KB Output is correct
92 Correct 2119 ms 57420 KB Output is correct
93 Correct 2052 ms 58772 KB Output is correct
94 Correct 2199 ms 53316 KB Output is correct
95 Correct 2370 ms 53444 KB Output is correct
96 Correct 2321 ms 53376 KB Output is correct
97 Correct 2107 ms 53368 KB Output is correct
98 Correct 1877 ms 53384 KB Output is correct
99 Correct 1905 ms 53096 KB Output is correct
100 Correct 1823 ms 53008 KB Output is correct
101 Correct 2027 ms 53300 KB Output is correct
102 Correct 1855 ms 53752 KB Output is correct
103 Correct 1939 ms 53700 KB Output is correct
104 Correct 1882 ms 53588 KB Output is correct
105 Correct 1535 ms 53588 KB Output is correct
106 Correct 1466 ms 53416 KB Output is correct
107 Correct 1474 ms 53512 KB Output is correct
108 Correct 1433 ms 53380 KB Output is correct