Submission #242350

# Submission time Handle Problem Language Result Execution time Memory
242350 2020-06-27T09:17:32 Z super_j6 Harvest (JOI20_harvest) C++14
100 / 100
842 ms 73928 KB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<ll, int>
#define pii pair<ll, pi>
#define f first
#define s second

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 

typedef tree<pi, null_type,less<pi>, rb_tree_tag,
	tree_order_statistics_node_update> oset;
	
struct omset{
	oset s;
	map<ll, int> mp;
	
	void clr(){
		s.clear();
		mp.clear();
	}
	
	void add(ll x){
		s.insert({x, mp[x]++});
	}
	
	int qry(ll x){
		return s.order_of_key({x + 1, -1});
	}
	
	int qry(ll a, ll b){
		return qry(b) - qry(a - 1);
	}
};

const int mxn = 200001;
ll n, m, k, w, q;
ll a[mxn], b[mxn];
ll rt[mxn], vis[mxn], p[mxn], d[mxn], l[mxn], r[mxn];
vector<pii> qr[mxn];
vector<int> g[mxn];
omset tre;
ll ans[mxn];

ll dst(int x, int y){
	return w + (k + a[y] - (a[x] + w) % k) % k;
}

int dfs(int c){
	r[c] = l[c];
	for(int i : g[c]){
		if(~rt[i]) continue;
		d[i] = d[c] + dst(c, i);
		rt[i] = rt[c];
		l[i] = r[c] + 1;
		r[c] = dfs(i);
	}
	return r[c];
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m >> k >> w;
	
	for(int i = 0; i < n; i++) cin >> a[i];
	for(int i = 0; i < m; i++) cin >> b[i];
	
	//init graph
	for(int i = 0; i < n; i++){
		rt[i] = -1;
		p[i] = (n + upper_bound(a, a + n, (k + (a[i] - w) % k) % k) - a - 1) % n;
		g[p[i]].push_back(i);
	}
	
	for(int i = 0; i < n; i++){
		if(!~rt[i]){
			int j;
			for(j = i; !vis[j]; j = p[j]) vis[j] = 1;
			for(int l = i; l != j; l = p[l]) vis[l] = 0;
			
			rt[j] = j;
			dfs(j);
		}
	}
	//end init graph
	
	cin >> q;
	
	//init queries
	for(int i = 0; i < q; i++){
		ll x, y;
		cin >> x >> y;
		x--;
		qr[rt[x]].push_back({d[x] + y, {i, x}});
	}
	
	for(int i = 0; i < m; i++){
		int x = (n + upper_bound(a, a + n, b[i]) - a - 1) % n;
		qr[rt[x]].push_back({d[x] + (k + b[i] - a[x]) % k, {-1, x}});
	}
	//end init queries
	
	for(int i = 0; i < n; i++){
		if(rt[i] != i) continue;
		tre.clr();
		sort(qr[i].begin(), qr[i].end());
		
		//solve tree
		ll len = d[p[i]] + dst(p[i], i);
		for(pii &j : qr[i]){
			if(!~j.s.f){
				tre.add(l[j.s.s]);
			}else{
				ans[j.s.f] = tre.qry(l[j.s.s], r[j.s.s]);
				j.f -= len;
			}
		}
		
		tre.clr();
		sort(qr[i].begin(), qr[i].end());
		
		//solve cycle
		ll dt = 0;
		for(pii j : qr[i]){
			if(!~j.s.f){
				dt -= j.f / len;
				tre.add(j.f % len);
			}else if(vis[j.s.s]){
				ans[j.s.f] += dt + tre.qry(len) * (j.f / len) + tre.qry(j.f % len);
			}
		}
	}
	
	for(int i = 0; i < q; i++) cout << ans[i] << endl;

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9984 KB Output is correct
2 Correct 13 ms 10240 KB Output is correct
3 Correct 15 ms 10556 KB Output is correct
4 Correct 16 ms 10560 KB Output is correct
5 Correct 15 ms 10684 KB Output is correct
6 Correct 16 ms 10684 KB Output is correct
7 Correct 16 ms 10812 KB Output is correct
8 Correct 16 ms 10572 KB Output is correct
9 Correct 15 ms 10572 KB Output is correct
10 Correct 15 ms 10572 KB Output is correct
11 Correct 15 ms 10572 KB Output is correct
12 Correct 16 ms 10684 KB Output is correct
13 Correct 16 ms 10684 KB Output is correct
14 Correct 17 ms 10496 KB Output is correct
15 Correct 15 ms 10684 KB Output is correct
16 Correct 15 ms 10684 KB Output is correct
17 Correct 16 ms 10684 KB Output is correct
18 Correct 15 ms 10560 KB Output is correct
19 Correct 16 ms 10688 KB Output is correct
20 Correct 15 ms 10688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 20056 KB Output is correct
2 Correct 263 ms 32232 KB Output is correct
3 Correct 235 ms 32376 KB Output is correct
4 Correct 254 ms 33272 KB Output is correct
5 Correct 226 ms 40920 KB Output is correct
6 Correct 225 ms 41068 KB Output is correct
7 Correct 225 ms 27616 KB Output is correct
8 Correct 254 ms 27488 KB Output is correct
9 Correct 274 ms 42712 KB Output is correct
10 Correct 244 ms 42712 KB Output is correct
11 Correct 353 ms 41664 KB Output is correct
12 Correct 356 ms 41812 KB Output is correct
13 Correct 390 ms 41692 KB Output is correct
14 Correct 308 ms 41688 KB Output is correct
15 Correct 284 ms 39356 KB Output is correct
16 Correct 239 ms 36824 KB Output is correct
17 Correct 239 ms 36696 KB Output is correct
18 Correct 167 ms 21340 KB Output is correct
19 Correct 149 ms 21340 KB Output is correct
20 Correct 182 ms 31192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9984 KB Output is correct
2 Correct 13 ms 10240 KB Output is correct
3 Correct 15 ms 10556 KB Output is correct
4 Correct 16 ms 10560 KB Output is correct
5 Correct 15 ms 10684 KB Output is correct
6 Correct 16 ms 10684 KB Output is correct
7 Correct 16 ms 10812 KB Output is correct
8 Correct 16 ms 10572 KB Output is correct
9 Correct 15 ms 10572 KB Output is correct
10 Correct 15 ms 10572 KB Output is correct
11 Correct 15 ms 10572 KB Output is correct
12 Correct 16 ms 10684 KB Output is correct
13 Correct 16 ms 10684 KB Output is correct
14 Correct 17 ms 10496 KB Output is correct
15 Correct 15 ms 10684 KB Output is correct
16 Correct 15 ms 10684 KB Output is correct
17 Correct 16 ms 10684 KB Output is correct
18 Correct 15 ms 10560 KB Output is correct
19 Correct 16 ms 10688 KB Output is correct
20 Correct 15 ms 10688 KB Output is correct
21 Correct 190 ms 20056 KB Output is correct
22 Correct 263 ms 32232 KB Output is correct
23 Correct 235 ms 32376 KB Output is correct
24 Correct 254 ms 33272 KB Output is correct
25 Correct 226 ms 40920 KB Output is correct
26 Correct 225 ms 41068 KB Output is correct
27 Correct 225 ms 27616 KB Output is correct
28 Correct 254 ms 27488 KB Output is correct
29 Correct 274 ms 42712 KB Output is correct
30 Correct 244 ms 42712 KB Output is correct
31 Correct 353 ms 41664 KB Output is correct
32 Correct 356 ms 41812 KB Output is correct
33 Correct 390 ms 41692 KB Output is correct
34 Correct 308 ms 41688 KB Output is correct
35 Correct 284 ms 39356 KB Output is correct
36 Correct 239 ms 36824 KB Output is correct
37 Correct 239 ms 36696 KB Output is correct
38 Correct 167 ms 21340 KB Output is correct
39 Correct 149 ms 21340 KB Output is correct
40 Correct 182 ms 31192 KB Output is correct
41 Correct 594 ms 62024 KB Output is correct
42 Correct 312 ms 40952 KB Output is correct
43 Correct 202 ms 31704 KB Output is correct
44 Correct 620 ms 62792 KB Output is correct
45 Correct 513 ms 71240 KB Output is correct
46 Correct 507 ms 70856 KB Output is correct
47 Correct 534 ms 71368 KB Output is correct
48 Correct 457 ms 71240 KB Output is correct
49 Correct 444 ms 71240 KB Output is correct
50 Correct 670 ms 58012 KB Output is correct
51 Correct 558 ms 57936 KB Output is correct
52 Correct 656 ms 72112 KB Output is correct
53 Correct 842 ms 73928 KB Output is correct
54 Correct 668 ms 72140 KB Output is correct
55 Correct 633 ms 71240 KB Output is correct
56 Correct 580 ms 68040 KB Output is correct
57 Correct 547 ms 67216 KB Output is correct
58 Correct 565 ms 68848 KB Output is correct
59 Correct 454 ms 68168 KB Output is correct
60 Correct 484 ms 68296 KB Output is correct
61 Correct 418 ms 68188 KB Output is correct
62 Correct 746 ms 57560 KB Output is correct
63 Correct 427 ms 51916 KB Output is correct
64 Correct 441 ms 52040 KB Output is correct
65 Correct 430 ms 52424 KB Output is correct
66 Correct 469 ms 51920 KB Output is correct
67 Correct 474 ms 52040 KB Output is correct
68 Correct 477 ms 51912 KB Output is correct
69 Correct 512 ms 62120 KB Output is correct
70 Correct 532 ms 62288 KB Output is correct
71 Correct 533 ms 63148 KB Output is correct
72 Correct 552 ms 63176 KB Output is correct