Submission #813273

# Submission time Handle Problem Language Result Execution time Memory
813273 2023-08-07T14:54:02 Z hugo_pm Tourism (JOI23_tourism) C++17
100 / 100
1405 ms 135632 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

struct FT {
	vector<int> s;
	FT(int n) : s(n) {}
	void update(int pos, int dif) { // a[pos] += dif
		for (; pos < sz(s); pos |= pos + 1) s[pos] += dif;
	}
	int query(int pos) { // sum of values in [0, pos)
		int res = 0;
		for (; pos > 0; pos &= pos - 1) res += s[pos-1];
		return res;
	}
};

const int LG = 17;
static_assert((1<<LG) > 100'000);

struct Req {
	int left, right, iReq;
};
string to_string(Req rq) { return to_string(make_pair(rq.left,rq.right)); }
struct Spot {
	int node, pos;
};

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int nbNode, nbSpot, nbReq;
	cin >> nbNode >> nbSpot >> nbReq;
	vector<vi> adj(nbNode);
	rep(iEdge, 0, nbNode-1) {
		int u, v; cin >> u >> v;
		--u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	vector<vi> anc(LG, vi(nbNode, -1));
	vector<int> tin(nbNode), tout(nbNode), prof(nbNode);
	{
		int lastTime = 0;
		auto Dfs = [&] (auto dfs, int node, int parent) -> void {
			anc[0][node] = parent;
			rep(lvl, 0, LG-1) {
				if (anc[lvl][node] == -1) break;
				anc[lvl+1][node] = anc[lvl][anc[lvl][node]];
			}
			tin[node] = lastTime++;
			for (int child : adj[node]) if (child != parent) {
				prof[child] = prof[node]+1;
				dfs(dfs, child, node);
			}
			tout[node] = lastTime++;
		};
		Dfs(Dfs, 0, -1);
	}
	auto ancestor = [&] (int ance, int child) {
		return tin[ance] <= tin[child] && tout[child] <= tout[ance];
	};
	auto lca = [&] (int u, int v) {
		if (prof[u] > prof[v]) swap(u, v);
		int firstJmp = prof[v] - prof[u];
		rep(lvl, 0, LG) if ((1<<lvl) & firstJmp) {
			v = anc[lvl][v];
		}
		assert(prof[u] == prof[v]);
		if (u == v) return u;
		for (int lvl = LG-1; lvl >= 0; --lvl) {
			int pu = anc[lvl][u], pv = anc[lvl][v];
			if (pu != -1 && pv != -1 && pu != pv) {
				u = pu, v = pv;
			}
		}
		assert(u != v);
		u = anc[0][u], v = anc[0][v];
		assert(u == v);
		return u;
	};
	vector<Spot> spots(nbSpot);
	rep(iSpot, 0, nbSpot) {
		auto &s = spots[iSpot];
		cin >> s.node; --s.node;
		s.pos = iSpot;
	}
	vector<int> answers(nbReq);
	vector<Req> allReqs(nbReq);
	rep(iReq, 0, nbReq) {
		int L, R;
		cin >> L >> R;
		--L, --R;
		allReqs[iReq] = {L, R, iReq};
	}
	auto compTin = [&] (const Spot &x, const Spot &y) {
		return tin[x.node] < tin[y.node];
	};
	auto Solve = [&] (auto solve, int leftDPR, int rightDPR, vector<Req> reqs) -> void {
		if (reqs.empty()) return;
		assert(leftDPR <= rightDPR);
		int mid = (leftDPR+rightDPR)/2, szDPR = (rightDPR-leftDPR+1);
		dbg(leftDPR, mid, rightDPR, reqs);
		vector<Spot> subset(begin(spots)+leftDPR, begin(spots)+rightDPR+1);
		sort(all(subset), compTin);
		const int FICTIVE_LCA_POS = -1e6;
		rep(i, 0, rightDPR-leftDPR) { // attention!
			subset.push_back({lca(subset[i].node, subset[i+1].node), FICTIVE_LCA_POS});
		}
		sort(all(subset), compTin);

		int szItv = sz(subset);
		vector<vector<pii>> adjVirt(szItv);
		auto Virt = [&] (auto virt, int iCur) -> int {
			int iLook = iCur+1;
			while (iLook < szItv && ancestor(subset[iCur].node, subset[iLook].node)) {
				int virtWeight = prof[subset[iLook].node] - prof[subset[iCur].node];
				adjVirt[iCur].emplace_back(iLook, virtWeight);
				adjVirt[iLook].emplace_back(iCur, virtWeight);
				iLook = virt(virt, iLook);
			}
			return iLook;
		};
		assert(Virt(Virt, 0) == szItv);
		FT fenwick(szDPR);
		vector<vector<pii>> sweepLine(szDPR);
		auto add = [&] (int cl, int cr, int weight) {
			if (leftDPR <= cl) {
				fenwick.update(0, weight);
				// remove in 0, readd in cr
				sweepLine[cl-leftDPR].push_back({0, -weight});
				if (cr <= rightDPR)
					sweepLine[cl-leftDPR].push_back({cr-leftDPR, weight});
			} else if (cr <= rightDPR) {
				fenwick.update(cr-leftDPR, weight);
			}
		};
		auto Dfs = [&] (auto dfs, int vNode, int parent) -> pii {
			int closestLeft = -1e9, closestRight = 1e9;
			const int &curPos = subset[vNode].pos;
			if (curPos != FICTIVE_LCA_POS) {
				if (curPos < mid)
					closestLeft = curPos;
				else if (curPos > mid)
					closestRight = curPos;
			}
			for (auto [vChild, weight] : adjVirt[vNode]) if (vChild != parent) {
				auto [ccleft, ccright] = dfs(dfs, vChild, vNode);
				add(ccleft, ccright, weight);
				chmax(closestLeft, ccleft), chmin(closestRight, ccright);
			}
			dbg(vNode, closestLeft, closestRight);
			return {closestLeft, closestRight};
		};

		int root = 0;
		while (subset[root].pos != mid) ++root;
		Dfs(Dfs, root, -1);
		vector<Req> sL, sR;
		vector<vector<Req>> toComp(szDPR);
		for (const auto &rq : reqs) {
			if (rq.right < mid)
				sL.push_back(rq);
			else if (rq.left > mid)
				sR.push_back(rq);
			else
				toComp[rq.left-leftDPR].push_back(rq);
		}
		rep(compCl, 0, szDPR) {
			for (const auto &rq : toComp[compCl]) {
				answers[rq.iReq] = fenwick.query(rq.right-leftDPR+1);
			}
			for (const auto &[compCr, delta] : sweepLine[compCl]) {
				fenwick.update(compCr, delta);
			}
		}
		solve(solve, leftDPR, mid-1, sL);
		solve(solve, mid+1, rightDPR, sR);
	};
	Solve(Solve, 0, nbSpot-1, allReqs);
	rep(iReq, 0, nbReq) {
		cout << answers[iReq]+1 << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 444 KB Output is correct
8 Correct 1 ms 444 KB Output is correct
9 Correct 1 ms 580 KB Output is correct
10 Correct 1 ms 576 KB Output is correct
11 Correct 2 ms 576 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 580 KB Output is correct
23 Correct 1 ms 580 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 444 KB Output is correct
8 Correct 1 ms 444 KB Output is correct
9 Correct 1 ms 580 KB Output is correct
10 Correct 1 ms 576 KB Output is correct
11 Correct 2 ms 576 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 580 KB Output is correct
23 Correct 1 ms 580 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 6 ms 1708 KB Output is correct
31 Correct 7 ms 1836 KB Output is correct
32 Correct 11 ms 2260 KB Output is correct
33 Correct 8 ms 2228 KB Output is correct
34 Correct 9 ms 2228 KB Output is correct
35 Correct 3 ms 1616 KB Output is correct
36 Correct 3 ms 1620 KB Output is correct
37 Correct 3 ms 1620 KB Output is correct
38 Correct 7 ms 2816 KB Output is correct
39 Correct 7 ms 2640 KB Output is correct
40 Correct 7 ms 2672 KB Output is correct
41 Correct 3 ms 2172 KB Output is correct
42 Correct 3 ms 2132 KB Output is correct
43 Correct 3 ms 2004 KB Output is correct
44 Correct 8 ms 2516 KB Output is correct
45 Correct 8 ms 2492 KB Output is correct
46 Correct 7 ms 2516 KB Output is correct
47 Correct 3 ms 1748 KB Output is correct
48 Correct 3 ms 1740 KB Output is correct
49 Correct 3 ms 1876 KB Output is correct
50 Correct 7 ms 2328 KB Output is correct
51 Correct 6 ms 2260 KB Output is correct
52 Correct 6 ms 2248 KB Output is correct
53 Correct 6 ms 2260 KB Output is correct
54 Correct 6 ms 2248 KB Output is correct
55 Correct 6 ms 2260 KB Output is correct
56 Correct 5 ms 2132 KB Output is correct
57 Correct 1 ms 980 KB Output is correct
58 Correct 11 ms 2240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 5 ms 2132 KB Output is correct
4 Correct 503 ms 100016 KB Output is correct
5 Correct 335 ms 76984 KB Output is correct
6 Correct 524 ms 109984 KB Output is correct
7 Correct 652 ms 122212 KB Output is correct
8 Correct 636 ms 129052 KB Output is correct
9 Correct 661 ms 132264 KB Output is correct
10 Correct 656 ms 127888 KB Output is correct
11 Correct 674 ms 121936 KB Output is correct
12 Correct 136 ms 84872 KB Output is correct
13 Correct 134 ms 84712 KB Output is correct
14 Correct 132 ms 84888 KB Output is correct
15 Correct 53 ms 37692 KB Output is correct
16 Correct 972 ms 135632 KB Output is correct
17 Correct 356 ms 95872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 445 ms 46340 KB Output is correct
3 Correct 804 ms 68848 KB Output is correct
4 Correct 572 ms 56912 KB Output is correct
5 Correct 167 ms 54816 KB Output is correct
6 Correct 241 ms 69520 KB Output is correct
7 Correct 379 ms 80444 KB Output is correct
8 Correct 538 ms 82508 KB Output is correct
9 Correct 738 ms 83748 KB Output is correct
10 Correct 872 ms 84144 KB Output is correct
11 Correct 969 ms 84324 KB Output is correct
12 Correct 1138 ms 84700 KB Output is correct
13 Correct 1233 ms 85732 KB Output is correct
14 Correct 1405 ms 88352 KB Output is correct
15 Correct 1390 ms 98584 KB Output is correct
16 Correct 850 ms 86204 KB Output is correct
17 Correct 792 ms 86304 KB Output is correct
18 Correct 860 ms 86308 KB Output is correct
19 Correct 151 ms 56372 KB Output is correct
20 Correct 195 ms 72092 KB Output is correct
21 Correct 262 ms 79860 KB Output is correct
22 Correct 363 ms 83992 KB Output is correct
23 Correct 392 ms 85812 KB Output is correct
24 Correct 480 ms 87064 KB Output is correct
25 Correct 591 ms 87340 KB Output is correct
26 Correct 738 ms 87564 KB Output is correct
27 Correct 813 ms 87948 KB Output is correct
28 Correct 969 ms 87952 KB Output is correct
29 Correct 1024 ms 88048 KB Output is correct
30 Correct 1125 ms 88236 KB Output is correct
31 Correct 1171 ms 89524 KB Output is correct
32 Correct 1264 ms 91148 KB Output is correct
33 Correct 1316 ms 94800 KB Output is correct
34 Correct 1325 ms 102536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 280 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 6 ms 2180 KB Output is correct
4 Correct 593 ms 79440 KB Output is correct
5 Correct 607 ms 81048 KB Output is correct
6 Correct 738 ms 91868 KB Output is correct
7 Correct 725 ms 96240 KB Output is correct
8 Correct 751 ms 96232 KB Output is correct
9 Correct 778 ms 96256 KB Output is correct
10 Correct 812 ms 96248 KB Output is correct
11 Correct 755 ms 96352 KB Output is correct
12 Correct 752 ms 96316 KB Output is correct
13 Correct 844 ms 96248 KB Output is correct
14 Correct 361 ms 95864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 444 KB Output is correct
8 Correct 1 ms 444 KB Output is correct
9 Correct 1 ms 580 KB Output is correct
10 Correct 1 ms 576 KB Output is correct
11 Correct 2 ms 576 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 580 KB Output is correct
23 Correct 1 ms 580 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 6 ms 1708 KB Output is correct
31 Correct 7 ms 1836 KB Output is correct
32 Correct 11 ms 2260 KB Output is correct
33 Correct 8 ms 2228 KB Output is correct
34 Correct 9 ms 2228 KB Output is correct
35 Correct 3 ms 1616 KB Output is correct
36 Correct 3 ms 1620 KB Output is correct
37 Correct 3 ms 1620 KB Output is correct
38 Correct 7 ms 2816 KB Output is correct
39 Correct 7 ms 2640 KB Output is correct
40 Correct 7 ms 2672 KB Output is correct
41 Correct 3 ms 2172 KB Output is correct
42 Correct 3 ms 2132 KB Output is correct
43 Correct 3 ms 2004 KB Output is correct
44 Correct 8 ms 2516 KB Output is correct
45 Correct 8 ms 2492 KB Output is correct
46 Correct 7 ms 2516 KB Output is correct
47 Correct 3 ms 1748 KB Output is correct
48 Correct 3 ms 1740 KB Output is correct
49 Correct 3 ms 1876 KB Output is correct
50 Correct 7 ms 2328 KB Output is correct
51 Correct 6 ms 2260 KB Output is correct
52 Correct 6 ms 2248 KB Output is correct
53 Correct 6 ms 2260 KB Output is correct
54 Correct 6 ms 2248 KB Output is correct
55 Correct 6 ms 2260 KB Output is correct
56 Correct 5 ms 2132 KB Output is correct
57 Correct 1 ms 980 KB Output is correct
58 Correct 11 ms 2240 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 596 KB Output is correct
61 Correct 5 ms 2132 KB Output is correct
62 Correct 503 ms 100016 KB Output is correct
63 Correct 335 ms 76984 KB Output is correct
64 Correct 524 ms 109984 KB Output is correct
65 Correct 652 ms 122212 KB Output is correct
66 Correct 636 ms 129052 KB Output is correct
67 Correct 661 ms 132264 KB Output is correct
68 Correct 656 ms 127888 KB Output is correct
69 Correct 674 ms 121936 KB Output is correct
70 Correct 136 ms 84872 KB Output is correct
71 Correct 134 ms 84712 KB Output is correct
72 Correct 132 ms 84888 KB Output is correct
73 Correct 53 ms 37692 KB Output is correct
74 Correct 972 ms 135632 KB Output is correct
75 Correct 356 ms 95872 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 445 ms 46340 KB Output is correct
78 Correct 804 ms 68848 KB Output is correct
79 Correct 572 ms 56912 KB Output is correct
80 Correct 167 ms 54816 KB Output is correct
81 Correct 241 ms 69520 KB Output is correct
82 Correct 379 ms 80444 KB Output is correct
83 Correct 538 ms 82508 KB Output is correct
84 Correct 738 ms 83748 KB Output is correct
85 Correct 872 ms 84144 KB Output is correct
86 Correct 969 ms 84324 KB Output is correct
87 Correct 1138 ms 84700 KB Output is correct
88 Correct 1233 ms 85732 KB Output is correct
89 Correct 1405 ms 88352 KB Output is correct
90 Correct 1390 ms 98584 KB Output is correct
91 Correct 850 ms 86204 KB Output is correct
92 Correct 792 ms 86304 KB Output is correct
93 Correct 860 ms 86308 KB Output is correct
94 Correct 151 ms 56372 KB Output is correct
95 Correct 195 ms 72092 KB Output is correct
96 Correct 262 ms 79860 KB Output is correct
97 Correct 363 ms 83992 KB Output is correct
98 Correct 392 ms 85812 KB Output is correct
99 Correct 480 ms 87064 KB Output is correct
100 Correct 591 ms 87340 KB Output is correct
101 Correct 738 ms 87564 KB Output is correct
102 Correct 813 ms 87948 KB Output is correct
103 Correct 969 ms 87952 KB Output is correct
104 Correct 1024 ms 88048 KB Output is correct
105 Correct 1125 ms 88236 KB Output is correct
106 Correct 1171 ms 89524 KB Output is correct
107 Correct 1264 ms 91148 KB Output is correct
108 Correct 1316 ms 94800 KB Output is correct
109 Correct 1325 ms 102536 KB Output is correct
110 Correct 1 ms 280 KB Output is correct
111 Correct 1 ms 596 KB Output is correct
112 Correct 6 ms 2180 KB Output is correct
113 Correct 593 ms 79440 KB Output is correct
114 Correct 607 ms 81048 KB Output is correct
115 Correct 738 ms 91868 KB Output is correct
116 Correct 725 ms 96240 KB Output is correct
117 Correct 751 ms 96232 KB Output is correct
118 Correct 778 ms 96256 KB Output is correct
119 Correct 812 ms 96248 KB Output is correct
120 Correct 755 ms 96352 KB Output is correct
121 Correct 752 ms 96316 KB Output is correct
122 Correct 844 ms 96248 KB Output is correct
123 Correct 361 ms 95864 KB Output is correct
124 Correct 811 ms 85176 KB Output is correct
125 Correct 617 ms 67708 KB Output is correct
126 Correct 957 ms 95436 KB Output is correct
127 Correct 951 ms 95400 KB Output is correct
128 Correct 932 ms 95424 KB Output is correct
129 Correct 932 ms 95428 KB Output is correct
130 Correct 955 ms 95404 KB Output is correct
131 Correct 726 ms 120368 KB Output is correct
132 Correct 751 ms 121896 KB Output is correct
133 Correct 735 ms 133292 KB Output is correct
134 Correct 868 ms 99236 KB Output is correct
135 Correct 837 ms 99016 KB Output is correct
136 Correct 858 ms 99100 KB Output is correct
137 Correct 669 ms 100472 KB Output is correct
138 Correct 671 ms 101900 KB Output is correct
139 Correct 634 ms 99696 KB Output is correct
140 Correct 691 ms 101008 KB Output is correct
141 Correct 656 ms 99888 KB Output is correct
142 Correct 641 ms 99672 KB Output is correct
143 Correct 57 ms 32316 KB Output is correct
144 Correct 1298 ms 98700 KB Output is correct