Submission #212290

# Submission time Handle Problem Language Result Execution time Memory
212290 2020-03-22T16:40:04 Z ksun48 Harvest (JOI20_harvest) C++14
100 / 100
897 ms 132656 KB
#include <bits/stdc++.h>
#include <bits/extc++.h> /** keep-include */
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

using namespace __gnu_pbds;

template<class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag,
	  tree_order_statistics_node_update>;

using ll = long long;

int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll n, m, l, c;
	cin >> n >> m >> l >> c;
	vector<ll> next_vert(n);
	vector<ll> next_dist(n);
	vector<vector<ll> > when_add(n);
	{
		vector<ll> a(n);
		for(ll& x : a){
			cin >> x;
			x = (l-1) - x;
		}
		reverse(a.begin(), a.end());
		for(ll i = 0; i < n; i++){
			ll res = (((a[i] + c) % l) + l) % l;
			auto r = lower_bound(a.begin(), a.end(), res);
			if(r == a.end()){
				next_dist[i] = c + l + a.front() - res;
				next_vert[i] = 0;
			} else {
				next_dist[i] = c + *r - res;
				next_vert[i] = r - a.begin();
			}
		}
		vector<ll> b(m);
		for(ll& x : b){
			cin >> x;
			x = (l-1) - x;
		}
		for(ll x : b){
			ll res = x;
			auto r = lower_bound(a.begin(), a.end(), res);
			ll d;
			ll v;
			if(r == a.end()){
				d = l + a.front() - res;
				v = 0;
			} else {
				d = *r - res;
				v = r - a.begin();
			}
			when_add[v].push_back(d);
		}
	}

	vector<bool> cycle_start(n, false);
	vector<vector<ll> > children(n);
	vector<ll> root_dist(n);
	{
		vector<ll> seen(n, 0);
		for(ll i = 0; i < n; i++){
			ll cur = i;
			while(seen[cur] == 0){
				seen[cur] = 1;
				cur = next_vert[cur];
			}
			if(seen[cur] == 1){
				cycle_start[cur] = true;
			}
			cur = i;
			while(seen[cur] == 1){
				seen[cur] = 2;
				cur = next_vert[cur];
			}
		}
		for(ll i = 0; i < n; i++){
			if(!cycle_start[i]){
				children[next_vert[i]].push_back(i);
			}
		}
		for(ll i = 0; i < n; i++){
			if(!cycle_start[i]) continue;
			y_combinator([&](auto self, ll v, ll dist) -> void {
				root_dist[v] = dist;
				for(ll w : children[v]){
					self(w, dist + next_dist[w]);
				}
			})(i, 0);
		}
	}

	vector<vector<ll> > queries_loc(n);
	ll q;
	cin >> q;
	vector<ll> qv(q);
	vector<ll> qt(q);
	vector<ll> answer(q, 0);
	for(ll i = 0; i < q; i++){
		cin >> qv[i] >> qt[i];
		qv[i]--;
		qv[i] = n-1-qv[i];
		queries_loc[qv[i]].push_back(i);
	}
	// solve for tree first
	mt19937_64 mt(48);
	vector<Tree<pair<ll, unsigned long long> > > when(n);
	for(ll i = 0; i < n; i++){
		if(!cycle_start[i]) continue;
		ll loc = y_combinator([&](auto self, ll v) -> ll {
			ll cur = v;
			for(ll t : when_add[v]){
				when[v].insert({t + root_dist[v], mt()});
			}
			for(ll w : children[v]){
				ll g = self(w);
				if(when[cur].size() < when[g].size()) swap(cur, g);
				for(auto x : when[g]) when[cur].insert(x);
				when[g] = {};
			}
			for(ll idx : queries_loc[v]){
				answer[idx] += when[cur].order_of_key({qt[idx] + root_dist[v], -1});
			}
			return cur;
		})(i);

		vector<pair<ll, ll> > queries;

		ll len = next_dist[i] + root_dist[next_vert[i]];
		ll cur = i;
		while(true){
			for(ll idx : queries_loc[cur]){
				queries.push_back({qt[idx] - (len - root_dist[cur]), idx});
			}
			cur = next_vert[cur];
			if(cur == i) break;
		}
		for(auto x : when[loc]){
			queries.push_back({x.first, -1});
		}
		sort(queries.begin(), queries.end());
		Tree<pair<ll, unsigned long long> > f;
		ll del = 0;
		for(pair<ll, ll> qq : queries){
			if(qq.second == -1){
				f.insert({qq.first % len, mt()});
				del -= (qq.first / len);
			} else {
				answer[qq.second] += del + (qq.first / len) * (ll)(f.size()) + f.order_of_key({qq.first % len, -1});
			}
		}
	}
	for(ll i = 0; i < q; i++){
		cout << answer[i] << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 512 KB Output is correct
2 Correct 9 ms 1280 KB Output is correct
3 Correct 11 ms 1536 KB Output is correct
4 Correct 11 ms 1664 KB Output is correct
5 Correct 10 ms 2304 KB Output is correct
6 Correct 11 ms 2304 KB Output is correct
7 Correct 12 ms 2304 KB Output is correct
8 Correct 10 ms 1536 KB Output is correct
9 Correct 13 ms 1536 KB Output is correct
10 Correct 11 ms 1536 KB Output is correct
11 Correct 11 ms 1536 KB Output is correct
12 Correct 11 ms 2304 KB Output is correct
13 Correct 13 ms 2304 KB Output is correct
14 Correct 11 ms 1892 KB Output is correct
15 Correct 11 ms 1920 KB Output is correct
16 Correct 12 ms 1920 KB Output is correct
17 Correct 12 ms 1920 KB Output is correct
18 Correct 10 ms 1920 KB Output is correct
19 Correct 11 ms 1920 KB Output is correct
20 Correct 10 ms 1800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 11088 KB Output is correct
2 Correct 332 ms 53544 KB Output is correct
3 Correct 260 ms 47576 KB Output is correct
4 Correct 253 ms 50600 KB Output is correct
5 Correct 312 ms 98628 KB Output is correct
6 Correct 311 ms 98472 KB Output is correct
7 Correct 258 ms 49452 KB Output is correct
8 Correct 260 ms 49328 KB Output is correct
9 Correct 377 ms 103384 KB Output is correct
10 Correct 298 ms 100716 KB Output is correct
11 Correct 565 ms 103468 KB Output is correct
12 Correct 515 ms 103360 KB Output is correct
13 Correct 553 ms 103460 KB Output is correct
14 Correct 450 ms 100696 KB Output is correct
15 Correct 365 ms 78984 KB Output is correct
16 Correct 315 ms 76452 KB Output is correct
17 Correct 308 ms 76196 KB Output is correct
18 Correct 183 ms 25132 KB Output is correct
19 Correct 174 ms 25088 KB Output is correct
20 Correct 278 ms 54212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 512 KB Output is correct
2 Correct 9 ms 1280 KB Output is correct
3 Correct 11 ms 1536 KB Output is correct
4 Correct 11 ms 1664 KB Output is correct
5 Correct 10 ms 2304 KB Output is correct
6 Correct 11 ms 2304 KB Output is correct
7 Correct 12 ms 2304 KB Output is correct
8 Correct 10 ms 1536 KB Output is correct
9 Correct 13 ms 1536 KB Output is correct
10 Correct 11 ms 1536 KB Output is correct
11 Correct 11 ms 1536 KB Output is correct
12 Correct 11 ms 2304 KB Output is correct
13 Correct 13 ms 2304 KB Output is correct
14 Correct 11 ms 1892 KB Output is correct
15 Correct 11 ms 1920 KB Output is correct
16 Correct 12 ms 1920 KB Output is correct
17 Correct 12 ms 1920 KB Output is correct
18 Correct 10 ms 1920 KB Output is correct
19 Correct 11 ms 1920 KB Output is correct
20 Correct 10 ms 1800 KB Output is correct
21 Correct 173 ms 11088 KB Output is correct
22 Correct 332 ms 53544 KB Output is correct
23 Correct 260 ms 47576 KB Output is correct
24 Correct 253 ms 50600 KB Output is correct
25 Correct 312 ms 98628 KB Output is correct
26 Correct 311 ms 98472 KB Output is correct
27 Correct 258 ms 49452 KB Output is correct
28 Correct 260 ms 49328 KB Output is correct
29 Correct 377 ms 103384 KB Output is correct
30 Correct 298 ms 100716 KB Output is correct
31 Correct 565 ms 103468 KB Output is correct
32 Correct 515 ms 103360 KB Output is correct
33 Correct 553 ms 103460 KB Output is correct
34 Correct 450 ms 100696 KB Output is correct
35 Correct 365 ms 78984 KB Output is correct
36 Correct 315 ms 76452 KB Output is correct
37 Correct 308 ms 76196 KB Output is correct
38 Correct 183 ms 25132 KB Output is correct
39 Correct 174 ms 25088 KB Output is correct
40 Correct 278 ms 54212 KB Output is correct
41 Correct 803 ms 83228 KB Output is correct
42 Correct 416 ms 63732 KB Output is correct
43 Correct 208 ms 50628 KB Output is correct
44 Correct 690 ms 77164 KB Output is correct
45 Correct 591 ms 129420 KB Output is correct
46 Correct 545 ms 129444 KB Output is correct
47 Correct 578 ms 129384 KB Output is correct
48 Correct 540 ms 127216 KB Output is correct
49 Correct 541 ms 127544 KB Output is correct
50 Correct 541 ms 80916 KB Output is correct
51 Correct 544 ms 80844 KB Output is correct
52 Correct 737 ms 132656 KB Output is correct
53 Correct 818 ms 132012 KB Output is correct
54 Correct 742 ms 132524 KB Output is correct
55 Correct 897 ms 132520 KB Output is correct
56 Correct 609 ms 107444 KB Output is correct
57 Correct 598 ms 107564 KB Output is correct
58 Correct 599 ms 107556 KB Output is correct
59 Correct 557 ms 104888 KB Output is correct
60 Correct 579 ms 105376 KB Output is correct
61 Correct 567 ms 105488 KB Output is correct
62 Correct 798 ms 101200 KB Output is correct
63 Correct 370 ms 54560 KB Output is correct
64 Correct 398 ms 54580 KB Output is correct
65 Correct 403 ms 54580 KB Output is correct
66 Correct 507 ms 53680 KB Output is correct
67 Correct 461 ms 53652 KB Output is correct
68 Correct 523 ms 53696 KB Output is correct
69 Correct 780 ms 85264 KB Output is correct
70 Correct 681 ms 85532 KB Output is correct
71 Correct 719 ms 86080 KB Output is correct
72 Correct 703 ms 86548 KB Output is correct