Submission #213255

# Submission time Handle Problem Language Result Execution time Memory
213255 2020-03-25T11:20:30 Z iefnah06 Harvest (JOI20_harvest) C++14
100 / 100
967 ms 138328 KB
#ifndef LOCAL
#define DEBUG
#endif
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;

namespace std {

template<class Fun>
class y_combinator_result {
	Fun fun_;
public:
	template<class T>
	explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

	template<class ...Args>
	decltype(auto) operator()(Args &&...args) {
		return fun_(std::ref(*this), std::forward<Args>(args)...);
	}
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
	return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

} // namespace std

namespace __gnu_pbds {
template <typename K, typename V=null_type>
using order_statistic_tree = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
}
using __gnu_pbds::order_statistic_tree;

using ll = long long;

int main() {
	ios::sync_with_stdio(0), cin.tie(0);

	int N, M, L, C; cin >> N >> M >> L >> C;
	auto Xtrans = [&](int x) -> int {
		return L-1-x;
	};
	vector<int> A(N);
	for (int& x : A) {
		cin >> x;
		x = Xtrans(x);
	}
	reverse(A.begin(), A.end());
	vector<int> B(M);
	for (int& x : B) {
		cin >> x;
		x = Xtrans(x);
	}

	vector<int> dest(N);
	vector<int> dist(N);
	for (int i = 0; i < N; i++) {
		int tgt = (A[i] + C) % L;
		auto it = lower_bound(A.begin(), A.end(), tgt);
		if (it == A.end()) {
			dest[i] = 0;
			dist[i] = C + L + A[0] - tgt;
		} else {
			dest[i] = int(distance(A.begin(), it));
			dist[i] = C + *it - tgt;
		}
	}

	vector<bool> vis(N, false);
	vector<bool> isRoot(N, false);
	for (int v = 0; v < N; v++) {
		if (vis[v]) continue;
		int t, h;
		for (t = dest[v], h = dest[dest[v]]; !vis[t] && t != h; t = dest[t], h = dest[dest[h]]) {}

		if (!vis[t]) {
			isRoot[t] = true;
			for (int cur = dest[t]; true; cur = dest[cur]) {
				vis[cur] = true;
				if (cur == t) break;
			}
		}

		for (int cur = v; cur != t; cur = dest[cur]) {
			vis[cur] = true;
		}
	}

	vector<vector<int>> evtsAdd(N);
	for (int i = 0; i < M; i++) {
		auto it = lower_bound(A.begin(), A.end(), B[i]);
		if (it == A.end()) {
			evtsAdd[0].push_back(L + A[0] - B[i]);
		} else {
			evtsAdd[distance(A.begin(), it)].push_back(*it - B[i]);
		}
	}

	int Q; cin >> Q;
	vector<int> V(Q);
	vector<ll> T(Q);
	vector<vector<int>> queriesLoc(N);
	for (int i = 0; i < Q; i++) {
		cin >> V[i] >> T[i];
		V[i]--;
		V[i] = N-1-V[i];
		queriesLoc[V[i]].push_back(i);
	}

	vector<ll> ans(Q);

	vector<vector<int>> ch(N);
	for (int v = 0; v < N; v++) {
		if (!isRoot[v]) {
			ch[dest[v]].push_back(v);
		}
	}
	vector<ll> depth(N);
	for (int v = 0; v < N; v++) {
		if (!isRoot[v]) continue;
		y_combinator([&](auto self, int cur, ll curDepth) -> void {
			depth[cur] = curDepth;
			for (int nxt : ch[cur]) {
				self(nxt, curDepth + dist[nxt]);
			}
		})(v, 0);
	}
	mt19937_64 mt(114514);
	using tree_t = order_statistic_tree<pair<ll, unsigned long long>>;
	vector<tree_t> evtTree(N);
	for (int v = 0; v < N; v++) {
		if (!isRoot[v]) continue;

		int treeInd = y_combinator([&](auto self, int cur) -> int {
			int res = cur;
			for (auto t : evtsAdd[cur]) {
				evtTree[res].insert({t + depth[cur], mt()});
			}

			for (int nxt : ch[cur]) {
				int nres = self(nxt);
				if (evtTree[res].size() < evtTree[nres].size()) swap(res, nres);
				for (auto t : evtTree[nres]) {
					evtTree[res].insert(t);
				}
				evtTree[nres] = {};
			}

			for (int it : queriesLoc[cur]) {
				ans[it] += evtTree[res].order_of_key({T[it] + depth[cur], -1});
			}

			return res;
		})(v);

		ll cycLen = dist[v] + depth[dest[v]];

		vector<pair<ll, int>> toProcess;
		for (auto t : evtTree[treeInd]) {
			toProcess.emplace_back(t.first, -1);
		}
		for (int cur = dest[v]; true; cur = dest[cur]) {
			for (auto it : queriesLoc[cur]) {
				toProcess.emplace_back(T[it] - (cycLen - depth[cur]), it);
			}
			if (cur == v) break;
		}
		sort(toProcess.begin(), toProcess.end());

		tree_t tr;
		ll delta = 0;
		for (auto it : toProcess) {
			ll val = it.first;
			int ind = it.second;
			if (ind == -1) {
				delta -= val / cycLen;
				tr.insert({val % cycLen, mt()});
			} else {
				ans[ind] += delta + ll(tr.size()) * (val / cycLen) + tr.order_of_key({val % cycLen, -1});
			}
		}
	}

	for (ll v : ans) {
		cout << v << '\n';
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 640 KB Output is correct
2 Correct 9 ms 1408 KB Output is correct
3 Correct 11 ms 1536 KB Output is correct
4 Correct 12 ms 1664 KB Output is correct
5 Correct 11 ms 2432 KB Output is correct
6 Correct 11 ms 2432 KB Output is correct
7 Correct 11 ms 2432 KB Output is correct
8 Correct 10 ms 1664 KB Output is correct
9 Correct 11 ms 1664 KB Output is correct
10 Correct 11 ms 1664 KB Output is correct
11 Correct 10 ms 1664 KB Output is correct
12 Correct 11 ms 2432 KB Output is correct
13 Correct 12 ms 2464 KB Output is correct
14 Correct 12 ms 2020 KB Output is correct
15 Correct 11 ms 2048 KB Output is correct
16 Correct 11 ms 2048 KB Output is correct
17 Correct 11 ms 2048 KB Output is correct
18 Correct 11 ms 2048 KB Output is correct
19 Correct 11 ms 2048 KB Output is correct
20 Correct 11 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 13708 KB Output is correct
2 Correct 295 ms 57700 KB Output is correct
3 Correct 237 ms 52728 KB Output is correct
4 Correct 248 ms 53480 KB Output is correct
5 Correct 314 ms 103548 KB Output is correct
6 Correct 317 ms 103476 KB Output is correct
7 Correct 234 ms 53744 KB Output is correct
8 Correct 230 ms 53744 KB Output is correct
9 Correct 358 ms 106728 KB Output is correct
10 Correct 285 ms 103272 KB Output is correct
11 Correct 513 ms 106728 KB Output is correct
12 Correct 462 ms 106728 KB Output is correct
13 Correct 527 ms 106728 KB Output is correct
14 Correct 418 ms 103400 KB Output is correct
15 Correct 354 ms 84144 KB Output is correct
16 Correct 289 ms 81496 KB Output is correct
17 Correct 298 ms 81276 KB Output is correct
18 Correct 170 ms 28156 KB Output is correct
19 Correct 158 ms 28156 KB Output is correct
20 Correct 237 ms 58792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 640 KB Output is correct
2 Correct 9 ms 1408 KB Output is correct
3 Correct 11 ms 1536 KB Output is correct
4 Correct 12 ms 1664 KB Output is correct
5 Correct 11 ms 2432 KB Output is correct
6 Correct 11 ms 2432 KB Output is correct
7 Correct 11 ms 2432 KB Output is correct
8 Correct 10 ms 1664 KB Output is correct
9 Correct 11 ms 1664 KB Output is correct
10 Correct 11 ms 1664 KB Output is correct
11 Correct 10 ms 1664 KB Output is correct
12 Correct 11 ms 2432 KB Output is correct
13 Correct 12 ms 2464 KB Output is correct
14 Correct 12 ms 2020 KB Output is correct
15 Correct 11 ms 2048 KB Output is correct
16 Correct 11 ms 2048 KB Output is correct
17 Correct 11 ms 2048 KB Output is correct
18 Correct 11 ms 2048 KB Output is correct
19 Correct 11 ms 2048 KB Output is correct
20 Correct 11 ms 2048 KB Output is correct
21 Correct 165 ms 13708 KB Output is correct
22 Correct 295 ms 57700 KB Output is correct
23 Correct 237 ms 52728 KB Output is correct
24 Correct 248 ms 53480 KB Output is correct
25 Correct 314 ms 103548 KB Output is correct
26 Correct 317 ms 103476 KB Output is correct
27 Correct 234 ms 53744 KB Output is correct
28 Correct 230 ms 53744 KB Output is correct
29 Correct 358 ms 106728 KB Output is correct
30 Correct 285 ms 103272 KB Output is correct
31 Correct 513 ms 106728 KB Output is correct
32 Correct 462 ms 106728 KB Output is correct
33 Correct 527 ms 106728 KB Output is correct
34 Correct 418 ms 103400 KB Output is correct
35 Correct 354 ms 84144 KB Output is correct
36 Correct 289 ms 81496 KB Output is correct
37 Correct 298 ms 81276 KB Output is correct
38 Correct 170 ms 28156 KB Output is correct
39 Correct 158 ms 28156 KB Output is correct
40 Correct 237 ms 58792 KB Output is correct
41 Correct 701 ms 88180 KB Output is correct
42 Correct 342 ms 68088 KB Output is correct
43 Correct 209 ms 51688 KB Output is correct
44 Correct 681 ms 82004 KB Output is correct
45 Correct 530 ms 134500 KB Output is correct
46 Correct 531 ms 135268 KB Output is correct
47 Correct 520 ms 136036 KB Output is correct
48 Correct 582 ms 133472 KB Output is correct
49 Correct 539 ms 133616 KB Output is correct
50 Correct 619 ms 85988 KB Output is correct
51 Correct 611 ms 85220 KB Output is correct
52 Correct 839 ms 138328 KB Output is correct
53 Correct 967 ms 137556 KB Output is correct
54 Correct 905 ms 138328 KB Output is correct
55 Correct 941 ms 138328 KB Output is correct
56 Correct 599 ms 112608 KB Output is correct
57 Correct 575 ms 113504 KB Output is correct
58 Correct 634 ms 114272 KB Output is correct
59 Correct 614 ms 111204 KB Output is correct
60 Correct 551 ms 111460 KB Output is correct
61 Correct 600 ms 111456 KB Output is correct
62 Correct 776 ms 108188 KB Output is correct
63 Correct 355 ms 57188 KB Output is correct
64 Correct 362 ms 57320 KB Output is correct
65 Correct 385 ms 57572 KB Output is correct
66 Correct 520 ms 57456 KB Output is correct
67 Correct 496 ms 57584 KB Output is correct
68 Correct 515 ms 56872 KB Output is correct
69 Correct 743 ms 92136 KB Output is correct
70 Correct 748 ms 88928 KB Output is correct
71 Correct 660 ms 90216 KB Output is correct
72 Correct 641 ms 90632 KB Output is correct