답안 #432207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
432207 2021-06-18T03:03:49 Z oolimry 수확 (JOI20_harvest) C++17
100 / 100
1561 ms 212340 KB
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define tern(cond, a, b) (cond ? a : b)
typedef long long lint;
typedef pair<lint,lint> ii;
 
lint n, m, L, T;
const int BUFF = 250000;
const int ROOT = 500000;
 
vector<lint> person;
lint apple[500005];
lint point[500005];
bool vis[500005];
 
inline lint getdis(int i){
	lint dis = person[i%BUFF] - person[point[i%BUFF]%BUFF];
	
	if(dis < T){
		dis += (T/L)*L;
	}
	while(dis < T) dis += L;
	return dis;
}
 
lint inf = 1e18 + 1e17;
map<lint,lint> infs;
 
vector<ii> adj[500005];
lint cyclesz[500005];
lint dis[500005];
lint low[500005];
lint high[500005];
lint maxval[500005];
lint total[500005];
lint rep[500005];
 
struct query{
	lint u, t, id, ans;
};
vector<query> queries;
 
vector<lint> stuff[500005];
lint toinf[500005];
lint sumreps[500005];
vector<lint> allreps;
vector<lint> mods[500005];
 
lint ppcnt = 0;
void dfs(lint u, int p = -1){
	low[u] = high[u] = ppcnt++;
	for(ii e : adj[u]){
		lint v = e.first, w = e.second;
		if(v == p) continue;
		dis[v] = dis[u] + w;
		
		if(u == ROOT){
			rep[v] = v;
			allreps.push_back(v);
		}
		else rep[v] = rep[u];
		
		dfs(v, u);
		high[u] = max(high[u], high[v]);
	}
}
 
void dfs2(lint u, int p = -1){
	for(ii e : adj[u]){
		lint v = e.first;
		if(v == p) continue;
		dfs2(v, u);
		maxval[u] = max(maxval[u], maxval[v]);
		total[u] += total[v];
	}
}
 
const int N = (1<<19);
vector<lint> tree[2*N];
 
inline void addshit(int u, lint t){
	t += dis[u];
	//show2(u, t);
	maxval[u] = max(maxval[u], t);
	stuff[rep[u]].push_back(t);
	total[u]++;
	for(int i = low[u] + N;i > 0;i >>= 1) tree[i].push_back(t);
}
inline void getready(){
	for(int i = 0;i < 2*N;i++) sort(all(tree[i]));
}
inline lint get(vector<lint> &v, lint t){
	return (lint)(upper_bound(all(v), t) - v.begin());
}
inline lint shitquery(int u, lint t){
	lint res = 0;
	for(int l = low[u]+N, r = high[u]+N+1;l < r;l >>= 1, r >>= 1){
		if(l&1) res += get(tree[l++], t);
		if(r&1) res += get(tree[--r], t);
	}
	return res;
}
 
vector<lint> disshit;
inline int getshit1(lint i){
	int pos = lower_bound(all(disshit), i) - disshit.begin() + 1;
	assert(i == disshit[pos-1]);
	return pos;
}

 
vector<lint> history;
lint ft[1400005];
inline void unshitupdate(lint i){
	i = getshit1(i);
	if(i == 0) return;
	history.push_back(i);
	for(;i <= 1400001;i += i&(-i)){
		ft[i]++;
	}
}
inline lint unshitquery(lint i){
	i = getshit1(i);
	int res = 0;
	for(;i > 0;i -= i&(-i)) res += ft[i];
	return res;
}
inline void undoall(){
	while(not history.empty()){
		int i = history.back();
		history.pop_back();
		if(i == 0) continue;
		for(;i <= 1400001;i += i&(-i)){
			ft[i] = 0;
		}
	}
}
 
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	
	memset(point, -1, sizeof(point));
	cin >> n >> m >> L >> T;
	
	person.resize(n);
	for(int i = 0;i < n;i++) cin >> person[i];
	for(int i = 0;i < m;i++) cin >> apple[i];
	
	for(int i = 0;i < n;i++){
		auto it = upper_bound(all(person), person[i] - (T%L));
		if(it == person.begin()) it = upper_bound(all(person), person[i] - (T%L) + L);
		
		it--;
		point[i] = it - person.begin();
		point[i+BUFF] = point[i]+BUFF;
	}
	
	for(int i = 0;i < n;i++){
		set<int> cur;
		int u = i;
		int cycle = -1;
		
		while(true){
			if(cur.find(u) != cur.end()) cycle = u;
			cur.insert(u);
			if(vis[u]) break;
			vis[u] = true;
			u = point[u];
		}
		
		if(cycle == -1) continue;
		
		
		lint totaldis = 0;
		u = cycle;
		vector<int> nodes;
		while(true){
			nodes.push_back(u);
			totaldis += getdis(u);
			u = point[u];
			if(u == cycle) break;
		}
		
		for(int x : nodes){
			cyclesz[x] = totaldis;
			cyclesz[x+BUFF] = totaldis;
		}
		
		point[cycle] += BUFF;
		point[cycle+BUFF] = ROOT;
	}
	
	
	for(int i = 0;i <= ROOT;i++){
		if(point[i] == -1) continue;
		
		
		if(point[i] == ROOT) adj[ROOT].push_back(ii(i, 0));
		else{
			lint dis = getdis(i);
			adj[point[i]].push_back(ii(i, dis));
			
		}
	}
	
	dfs(ROOT);
	
	for(int i = 0;i < m;i++){
		auto it = lower_bound(all(person), apple[i]);
		if(it == person.begin()) it = person.end();
		it--;
		int x = it - person.begin();
		
		
		lint t = apple[i] - person[x];
		if(t < 0) t += L;
		addshit(x, t);
	}
	
	getready();
	dfs2(ROOT);
	
	for(lint r : allreps){
		//show(r);
		for(lint x : stuff[r]){
			lint MM = cyclesz[r];
			lint I = inf;
			I /= MM;
			I *= MM;
			infs[MM] = I;
			
			disshit.push_back(x % MM);
			
			lint hm = (I-x)/MM + 1;
			
			sumreps[r] += hm;
			//show(hm);
			mods[r].push_back(x%MM);
			
		}
		sort(all(mods[r]));
		sort(all(stuff[r]));
	}
	
	//show(inf);
	int Q; cin >> Q;
	for(int q = 0;q < Q;q++){
		lint u, t; cin >> u >> t;
		u--;
		queries.push_back({u,t,q,0});
		if(cyclesz[u+BUFF] != 0){
			lint r = rep[u+BUFF];
			
			lint MM = cyclesz[r];
			disshit.push_back((t+dis[u+BUFF])%MM);
		}
	}
		
	sort(all(disshit));
	disshit.erase(unique(all(disshit)), disshit.end());
	
	sort(all(queries), [&](query a, query b){
		if(rep[a.u] == rep[b.u]) return a.t < b.t;
		else return rep[a.u] < rep[b.u];
	});
	
	lint prevrep = -1;
	lint sumofshit = 0;
	lint shitcnt = 0;
	int ptr = 0;
	
	for(query &q : queries){
		lint u = q.u, t = q.t;
		lint ans = 0;
		
		if(rep[u] != prevrep){
			prevrep = rep[u];
			undoall();
			sumofshit = 0;
			shitcnt = 0;
			ptr = 0;
		}
 
		ans += shitquery(u, t+dis[u]);
		
		if(cyclesz[u+BUFF] != 0){
			t += dis[u+BUFF];
			lint r = rep[u+BUFF];
			
			lint MM = cyclesz[r];
			
			
			while(ptr < sz(stuff[r])){
				if(stuff[r][ptr] > t) break;
				else{
					sumofshit += (infs[MM]-stuff[r][ptr])/MM + 1;
					//shitmods.push_back(stuff[r][ptr] % MM); ///change this
					unshitupdate(stuff[r][ptr] % MM);
					ptr++;
					shitcnt++;
				}
				//sort(all(shitmods));
			}
			
			ans += sumofshit;
			lint fulls = (infs[MM]-(t+1))/MM;
			fulls *= shitcnt;
			
			if(fulls >= 0){
				ans -= fulls;
				ans -= (shitcnt - unshitquery(t%MM)); ///change this
			}
		}
 
		
		q.ans = ans;
	}
	
	sort(all(queries), [&](query a, query b){ return a.id < b.id; });
	for(query q : queries) cout << q.ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 64452 KB Output is correct
2 Correct 51 ms 65956 KB Output is correct
3 Correct 51 ms 65744 KB Output is correct
4 Correct 52 ms 65692 KB Output is correct
5 Correct 54 ms 65996 KB Output is correct
6 Correct 52 ms 65976 KB Output is correct
7 Correct 54 ms 66048 KB Output is correct
8 Correct 53 ms 65616 KB Output is correct
9 Correct 53 ms 65732 KB Output is correct
10 Correct 55 ms 65628 KB Output is correct
11 Correct 50 ms 65620 KB Output is correct
12 Correct 52 ms 66336 KB Output is correct
13 Correct 55 ms 66500 KB Output is correct
14 Correct 54 ms 66296 KB Output is correct
15 Correct 53 ms 66000 KB Output is correct
16 Correct 58 ms 65896 KB Output is correct
17 Correct 52 ms 65988 KB Output is correct
18 Correct 53 ms 65816 KB Output is correct
19 Correct 51 ms 65812 KB Output is correct
20 Correct 52 ms 65904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 79328 KB Output is correct
2 Correct 391 ms 108852 KB Output is correct
3 Correct 363 ms 115672 KB Output is correct
4 Correct 363 ms 112852 KB Output is correct
5 Correct 504 ms 126948 KB Output is correct
6 Correct 481 ms 126516 KB Output is correct
7 Correct 284 ms 98784 KB Output is correct
8 Correct 319 ms 98732 KB Output is correct
9 Correct 658 ms 154792 KB Output is correct
10 Correct 463 ms 152808 KB Output is correct
11 Correct 992 ms 153712 KB Output is correct
12 Correct 947 ms 153692 KB Output is correct
13 Correct 975 ms 153652 KB Output is correct
14 Correct 736 ms 151624 KB Output is correct
15 Correct 642 ms 135772 KB Output is correct
16 Correct 486 ms 117420 KB Output is correct
17 Correct 436 ms 116944 KB Output is correct
18 Correct 248 ms 83784 KB Output is correct
19 Correct 234 ms 83668 KB Output is correct
20 Correct 332 ms 109312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 64452 KB Output is correct
2 Correct 51 ms 65956 KB Output is correct
3 Correct 51 ms 65744 KB Output is correct
4 Correct 52 ms 65692 KB Output is correct
5 Correct 54 ms 65996 KB Output is correct
6 Correct 52 ms 65976 KB Output is correct
7 Correct 54 ms 66048 KB Output is correct
8 Correct 53 ms 65616 KB Output is correct
9 Correct 53 ms 65732 KB Output is correct
10 Correct 55 ms 65628 KB Output is correct
11 Correct 50 ms 65620 KB Output is correct
12 Correct 52 ms 66336 KB Output is correct
13 Correct 55 ms 66500 KB Output is correct
14 Correct 54 ms 66296 KB Output is correct
15 Correct 53 ms 66000 KB Output is correct
16 Correct 58 ms 65896 KB Output is correct
17 Correct 52 ms 65988 KB Output is correct
18 Correct 53 ms 65816 KB Output is correct
19 Correct 51 ms 65812 KB Output is correct
20 Correct 52 ms 65904 KB Output is correct
21 Correct 254 ms 79328 KB Output is correct
22 Correct 391 ms 108852 KB Output is correct
23 Correct 363 ms 115672 KB Output is correct
24 Correct 363 ms 112852 KB Output is correct
25 Correct 504 ms 126948 KB Output is correct
26 Correct 481 ms 126516 KB Output is correct
27 Correct 284 ms 98784 KB Output is correct
28 Correct 319 ms 98732 KB Output is correct
29 Correct 658 ms 154792 KB Output is correct
30 Correct 463 ms 152808 KB Output is correct
31 Correct 992 ms 153712 KB Output is correct
32 Correct 947 ms 153692 KB Output is correct
33 Correct 975 ms 153652 KB Output is correct
34 Correct 736 ms 151624 KB Output is correct
35 Correct 642 ms 135772 KB Output is correct
36 Correct 486 ms 117420 KB Output is correct
37 Correct 436 ms 116944 KB Output is correct
38 Correct 248 ms 83784 KB Output is correct
39 Correct 234 ms 83668 KB Output is correct
40 Correct 332 ms 109312 KB Output is correct
41 Correct 841 ms 160544 KB Output is correct
42 Correct 682 ms 169380 KB Output is correct
43 Correct 292 ms 109820 KB Output is correct
44 Correct 539 ms 156752 KB Output is correct
45 Correct 836 ms 178184 KB Output is correct
46 Correct 782 ms 175564 KB Output is correct
47 Correct 678 ms 175928 KB Output is correct
48 Correct 696 ms 165272 KB Output is correct
49 Correct 610 ms 168872 KB Output is correct
50 Correct 581 ms 152992 KB Output is correct
51 Correct 530 ms 152860 KB Output is correct
52 Correct 1067 ms 204920 KB Output is correct
53 Correct 1029 ms 212340 KB Output is correct
54 Correct 1056 ms 212208 KB Output is correct
55 Correct 1561 ms 206452 KB Output is correct
56 Correct 841 ms 175968 KB Output is correct
57 Correct 807 ms 174616 KB Output is correct
58 Correct 712 ms 177968 KB Output is correct
59 Correct 661 ms 163936 KB Output is correct
60 Correct 646 ms 164084 KB Output is correct
61 Correct 632 ms 164512 KB Output is correct
62 Correct 1006 ms 194124 KB Output is correct
63 Correct 461 ms 135896 KB Output is correct
64 Correct 464 ms 135708 KB Output is correct
65 Correct 438 ms 136764 KB Output is correct
66 Correct 613 ms 131908 KB Output is correct
67 Correct 527 ms 127972 KB Output is correct
68 Correct 597 ms 130768 KB Output is correct
69 Correct 780 ms 170088 KB Output is correct
70 Correct 691 ms 164812 KB Output is correct
71 Correct 774 ms 167828 KB Output is correct
72 Correct 774 ms 167196 KB Output is correct