Submission #734378

# Submission time Handle Problem Language Result Execution time Memory
734378 2023-05-02T10:47:12 Z NeroZein Two Currencies (JOI23_currencies) C++17
100 / 100
4807 ms 45536 KB
#include "bits/stdc++.h"
#define int long long
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

struct queries {
	int u, v, gold, silver;
};

const int Log = 18;
const int N = 100005;

int n, m, q;
vector<int> g[N]; 
pair<int,int> e[N];
pair<int,int> ch[N];

int idd; 
int up[Log][N];
int in[N], out[N], dep[N];

void Dfs (int v, int p) {
	in[v] = ++idd;
	for (int u : g[v]) {
		if (u == p) continue;
		dep[u] = dep[v] + 1; 
		up[0][u] = v;
		for (int i = 1; i < Log; ++i) {
			up[i][u] = up[i - 1][up[i - 1][u]];
		}
		Dfs(u, v);
	}
	out[v] = idd;
}

int Lca (int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	for (int i = 0; i < Log; ++i) {
		if ((dep[u] - dep[v]) >> i & 1) {
			u = up[i][u];
		}
	}
	if (u == v) {
		return u;
	}
	for (int i = Log - 1; i >= 0; --i) {
		if (up[i][u] != up[i][v]) {
			u = up[i][u], v = up[i][v];
		}
	}
	return up[0][u];
}

int tree[N];
void upd (int id, int v) {
	while (id < N) {
		tree[id] += v;
		id += id & -id;
	}
}

void upd (int l, int r, int v) {
	upd(l, v);
	upd(r + 1, -v); 
}

int qry (int id) {
	int ret = 0;
	while (id) {
		ret += tree[id];
		id -= id & -id;
	}
	return ret;
}

int getSum (int u, int v) {
	return qry(in[u]) + qry(in[v]) - 2 * qry(in[Lca(u, v)]); 
}

void addEdge (int i, bool weighted) {
	int u = e[ch[i].first].first, v = e[ch[i].first].second; 
	if (dep[u] < dep[v]) swap(u, v); 
	if (weighted) {
		upd(in[u], out[u], ch[i].second);
	} else {
		upd(in[u], out[u], 1);
	}
}

int ans[N]; 
int L[N], R[N];
pair<int,int> Mid[N]; 

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> m >> q; 
	for (int i = 0; i < n - 1; ++i) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
		e[i] = {u, v};   
	}
	Dfs(1, 1);
	for (int i = 0; i < m; ++i) {
		int p, c;
		cin >> p >> c;
		ch[i] = {p - 1, c}; 
		addEdge(i, 0);
	}
	vector<queries> qs(q); 
	for (int i = 0; i < q; ++i) {
		int u, v, gold, silver;
		cin >> u >> v >> gold >> silver;
		qs[i] = {u, v, gold, silver}; 
		ans[i] = getSum(u, v); 
	}
	sort(ch, ch + m, [&](pair<int,int>& a, pair<int,int>& b) {
		return a.second < b.second;
	});
	for (int i = 0; i < q; ++i) {
		L[i] = -1, R[i] = m - 1; 
	}
	while (true) {
		memset(tree, 0, sizeof tree);
		bool changed = false;
		for (int i = 0; i < q; ++i) {
			if (L[i] != R[i]) {
				Mid[i] = make_pair((L[i] + R[i] + 1) >> 1, i);				
				changed = true; 
			} else {
				Mid[i] = make_pair(L[i], i); 
			}
		}
		sort(Mid, Mid + q); 
		int p = 0;
		if (!changed) {
			for (int i = 0; i < q; ++i) {
				while (p <= Mid[i].first) {
					addEdge(p, 0); 
					p++; 
				}
				ans[Mid[i].second] = qs[Mid[i].second].gold - (ans[Mid[i].second] - getSum(qs[Mid[i].second].u, qs[Mid[i].second].v));  
			}
			break; 
		}
		for (int i = 0; i < q; ++i) {
			while (p <= Mid[i].first) {
				addEdge(p, 1); 
				p++;
			}
			int id = Mid[i].second;
			if (getSum(qs[id].u, qs[id].v) <= qs[id].silver) {
				L[id] = Mid[i].first;
			} else {
				R[id] = Mid[i].first - 1; 
			}
		}
	}
	for (int i = 0; i < q; ++i) {
		cout << max(-1LL, ans[i]) << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3540 KB Output is correct
2 Correct 2 ms 3540 KB Output is correct
3 Correct 2 ms 3540 KB Output is correct
4 Correct 2 ms 3540 KB Output is correct
5 Correct 11 ms 4112 KB Output is correct
6 Correct 16 ms 4196 KB Output is correct
7 Correct 17 ms 4028 KB Output is correct
8 Correct 16 ms 4220 KB Output is correct
9 Correct 15 ms 4168 KB Output is correct
10 Correct 14 ms 4180 KB Output is correct
11 Correct 17 ms 4180 KB Output is correct
12 Correct 16 ms 4212 KB Output is correct
13 Correct 15 ms 4312 KB Output is correct
14 Correct 15 ms 4276 KB Output is correct
15 Correct 15 ms 4244 KB Output is correct
16 Correct 15 ms 4308 KB Output is correct
17 Correct 17 ms 4228 KB Output is correct
18 Correct 15 ms 4236 KB Output is correct
19 Correct 13 ms 4224 KB Output is correct
20 Correct 14 ms 4224 KB Output is correct
21 Correct 17 ms 4172 KB Output is correct
22 Correct 13 ms 4180 KB Output is correct
23 Correct 12 ms 4216 KB Output is correct
24 Correct 15 ms 4220 KB Output is correct
25 Correct 15 ms 4212 KB Output is correct
26 Correct 10 ms 4168 KB Output is correct
27 Correct 11 ms 4168 KB Output is correct
28 Correct 10 ms 4220 KB Output is correct
29 Correct 13 ms 4180 KB Output is correct
30 Correct 16 ms 4180 KB Output is correct
31 Correct 16 ms 4220 KB Output is correct
32 Correct 17 ms 4180 KB Output is correct
33 Correct 12 ms 4308 KB Output is correct
34 Correct 13 ms 4324 KB Output is correct
35 Correct 11 ms 4308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3480 KB Output is correct
2 Correct 15 ms 4180 KB Output is correct
3 Correct 17 ms 4204 KB Output is correct
4 Correct 16 ms 4212 KB Output is correct
5 Correct 2856 ms 31164 KB Output is correct
6 Correct 3354 ms 26472 KB Output is correct
7 Correct 3959 ms 29220 KB Output is correct
8 Correct 2739 ms 28296 KB Output is correct
9 Correct 2665 ms 27880 KB Output is correct
10 Correct 4807 ms 34060 KB Output is correct
11 Correct 4697 ms 39252 KB Output is correct
12 Correct 4393 ms 39152 KB Output is correct
13 Correct 4533 ms 39236 KB Output is correct
14 Correct 4088 ms 39244 KB Output is correct
15 Correct 2989 ms 45108 KB Output is correct
16 Correct 2676 ms 45536 KB Output is correct
17 Correct 3108 ms 44812 KB Output is correct
18 Correct 4443 ms 39156 KB Output is correct
19 Correct 4679 ms 39176 KB Output is correct
20 Correct 4498 ms 39260 KB Output is correct
21 Correct 4026 ms 38732 KB Output is correct
22 Correct 4006 ms 38836 KB Output is correct
23 Correct 4067 ms 38916 KB Output is correct
24 Correct 4139 ms 38828 KB Output is correct
25 Correct 2640 ms 39704 KB Output is correct
26 Correct 3160 ms 39676 KB Output is correct
27 Correct 1680 ms 39528 KB Output is correct
28 Correct 792 ms 38964 KB Output is correct
29 Correct 796 ms 39004 KB Output is correct
30 Correct 1317 ms 39292 KB Output is correct
31 Correct 1387 ms 39300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3540 KB Output is correct
2 Correct 12 ms 4328 KB Output is correct
3 Correct 13 ms 4328 KB Output is correct
4 Correct 12 ms 4308 KB Output is correct
5 Correct 1502 ms 35688 KB Output is correct
6 Correct 1557 ms 34940 KB Output is correct
7 Correct 1397 ms 26644 KB Output is correct
8 Correct 2419 ms 39828 KB Output is correct
9 Correct 2329 ms 39912 KB Output is correct
10 Correct 2381 ms 39840 KB Output is correct
11 Correct 1167 ms 39856 KB Output is correct
12 Correct 1141 ms 39836 KB Output is correct
13 Correct 1173 ms 39884 KB Output is correct
14 Correct 665 ms 39832 KB Output is correct
15 Correct 688 ms 39832 KB Output is correct
16 Correct 940 ms 39860 KB Output is correct
17 Correct 937 ms 39884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3540 KB Output is correct
2 Correct 2 ms 3540 KB Output is correct
3 Correct 2 ms 3540 KB Output is correct
4 Correct 2 ms 3540 KB Output is correct
5 Correct 11 ms 4112 KB Output is correct
6 Correct 16 ms 4196 KB Output is correct
7 Correct 17 ms 4028 KB Output is correct
8 Correct 16 ms 4220 KB Output is correct
9 Correct 15 ms 4168 KB Output is correct
10 Correct 14 ms 4180 KB Output is correct
11 Correct 17 ms 4180 KB Output is correct
12 Correct 16 ms 4212 KB Output is correct
13 Correct 15 ms 4312 KB Output is correct
14 Correct 15 ms 4276 KB Output is correct
15 Correct 15 ms 4244 KB Output is correct
16 Correct 15 ms 4308 KB Output is correct
17 Correct 17 ms 4228 KB Output is correct
18 Correct 15 ms 4236 KB Output is correct
19 Correct 13 ms 4224 KB Output is correct
20 Correct 14 ms 4224 KB Output is correct
21 Correct 17 ms 4172 KB Output is correct
22 Correct 13 ms 4180 KB Output is correct
23 Correct 12 ms 4216 KB Output is correct
24 Correct 15 ms 4220 KB Output is correct
25 Correct 15 ms 4212 KB Output is correct
26 Correct 10 ms 4168 KB Output is correct
27 Correct 11 ms 4168 KB Output is correct
28 Correct 10 ms 4220 KB Output is correct
29 Correct 13 ms 4180 KB Output is correct
30 Correct 16 ms 4180 KB Output is correct
31 Correct 16 ms 4220 KB Output is correct
32 Correct 17 ms 4180 KB Output is correct
33 Correct 12 ms 4308 KB Output is correct
34 Correct 13 ms 4324 KB Output is correct
35 Correct 11 ms 4308 KB Output is correct
36 Correct 2 ms 3480 KB Output is correct
37 Correct 15 ms 4180 KB Output is correct
38 Correct 17 ms 4204 KB Output is correct
39 Correct 16 ms 4212 KB Output is correct
40 Correct 2856 ms 31164 KB Output is correct
41 Correct 3354 ms 26472 KB Output is correct
42 Correct 3959 ms 29220 KB Output is correct
43 Correct 2739 ms 28296 KB Output is correct
44 Correct 2665 ms 27880 KB Output is correct
45 Correct 4807 ms 34060 KB Output is correct
46 Correct 4697 ms 39252 KB Output is correct
47 Correct 4393 ms 39152 KB Output is correct
48 Correct 4533 ms 39236 KB Output is correct
49 Correct 4088 ms 39244 KB Output is correct
50 Correct 2989 ms 45108 KB Output is correct
51 Correct 2676 ms 45536 KB Output is correct
52 Correct 3108 ms 44812 KB Output is correct
53 Correct 4443 ms 39156 KB Output is correct
54 Correct 4679 ms 39176 KB Output is correct
55 Correct 4498 ms 39260 KB Output is correct
56 Correct 4026 ms 38732 KB Output is correct
57 Correct 4006 ms 38836 KB Output is correct
58 Correct 4067 ms 38916 KB Output is correct
59 Correct 4139 ms 38828 KB Output is correct
60 Correct 2640 ms 39704 KB Output is correct
61 Correct 3160 ms 39676 KB Output is correct
62 Correct 1680 ms 39528 KB Output is correct
63 Correct 792 ms 38964 KB Output is correct
64 Correct 796 ms 39004 KB Output is correct
65 Correct 1317 ms 39292 KB Output is correct
66 Correct 1387 ms 39300 KB Output is correct
67 Correct 3 ms 3540 KB Output is correct
68 Correct 12 ms 4328 KB Output is correct
69 Correct 13 ms 4328 KB Output is correct
70 Correct 12 ms 4308 KB Output is correct
71 Correct 1502 ms 35688 KB Output is correct
72 Correct 1557 ms 34940 KB Output is correct
73 Correct 1397 ms 26644 KB Output is correct
74 Correct 2419 ms 39828 KB Output is correct
75 Correct 2329 ms 39912 KB Output is correct
76 Correct 2381 ms 39840 KB Output is correct
77 Correct 1167 ms 39856 KB Output is correct
78 Correct 1141 ms 39836 KB Output is correct
79 Correct 1173 ms 39884 KB Output is correct
80 Correct 665 ms 39832 KB Output is correct
81 Correct 688 ms 39832 KB Output is correct
82 Correct 940 ms 39860 KB Output is correct
83 Correct 937 ms 39884 KB Output is correct
84 Correct 2657 ms 32196 KB Output is correct
85 Correct 2173 ms 26460 KB Output is correct
86 Correct 2017 ms 25784 KB Output is correct
87 Correct 4117 ms 39176 KB Output is correct
88 Correct 4002 ms 38652 KB Output is correct
89 Correct 4091 ms 39188 KB Output is correct
90 Correct 4194 ms 39188 KB Output is correct
91 Correct 4201 ms 39216 KB Output is correct
92 Correct 3494 ms 43212 KB Output is correct
93 Correct 3083 ms 44560 KB Output is correct
94 Correct 4466 ms 39216 KB Output is correct
95 Correct 4454 ms 39232 KB Output is correct
96 Correct 4452 ms 39160 KB Output is correct
97 Correct 4380 ms 39240 KB Output is correct
98 Correct 4078 ms 38952 KB Output is correct
99 Correct 4104 ms 38768 KB Output is correct
100 Correct 4056 ms 38552 KB Output is correct
101 Correct 4114 ms 38816 KB Output is correct
102 Correct 2582 ms 39548 KB Output is correct
103 Correct 2832 ms 39552 KB Output is correct
104 Correct 2900 ms 39480 KB Output is correct
105 Correct 826 ms 39240 KB Output is correct
106 Correct 783 ms 38960 KB Output is correct
107 Correct 1246 ms 39300 KB Output is correct
108 Correct 1277 ms 39248 KB Output is correct