#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<pair<long long,long long>,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> orderedset;
#define int long long
const int INF = 1e18;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,l,c;
cin >> n >> m >> l >> c;
vector<int> employee(n+1);
vector<int> apple(m);
vector<pair<int,int>> adj(n+1);
vector<vector<int>> revadj(n+1);
for(int i=1;i<=n;i++)cin>>employee[i];
for(int&i:apple)cin>>i;
for(int i=1;i<=n;i++){
int modded = c%l;
int iter = 0;
for(int jump=131072;jump;jump/=2){
if(iter+jump>n)continue;
if((l+employee[i]-employee[(n-iter-jump+i)%n +1])%l < modded)iter+=jump;
}
iter++;
if(iter==n+1){
adj[i] = {i,(c/l)*l + l};
continue;
}
adj[i] = {(n-iter+i)%n +1,(c/l)*l + (l+employee[i]-employee[(n-iter+i)%n +1])%l};
revadj[adj[i].first].emplace_back(i);
}
vector<int> depth(n+1);
vector<bool> visited(n+1);
vector<int> heads;
function<void (int)> front_dfs = [&](int x){
if(visited[x])return;
visited[x]=true;
front_dfs(adj[x].first);
if(depth[adj[x].first]==0)heads.emplace_back(x);
depth[x] = depth[adj[x].first]+adj[x].second;
};
for(int i=1;i<=n;i++)if(!visited[i])front_dfs(i);
vector<vector<int>> apples(n+1);
vector<vector<pair<int,int>>> queries(n+1);
for(int&i:apple){
int start = -1;
if(i<employee[1])start = n;
else {
start = upper_bound(employee.begin()+1,employee.end(),i)-employee.begin()-1;
}
apples[start].emplace_back((l+i-employee[start])%l);
}
int q;
cin >> q;
vector<int> ans(q+1);
for(int i=1;i<=q;i++){
int v,t;
cin >> v >> t;
queries[v].emplace_back(t,i);
}
visited = vector<bool>(n+1);
int uid = 0;
function<orderedset(int)> calc_linear_dfs = [&](int x){
// cout << x << endl;
orderedset curr;
if(visited[x])return curr;
visited[x] = true;
for(int&i:revadj[x]){
auto t = calc_linear_dfs(i);
if(curr.size()<t.size())curr.swap(t);
for(auto j:t)curr.insert(j);
}
for(int&i:apples[x])curr.insert({i+depth[x],++uid});
for(auto[t,idx]:queries[x]){
ans[idx] = curr.order_of_key({t+depth[x],INF});
}
return curr;
};
for(int&i:heads){
auto t = calc_linear_dfs(i);
orderedset curr;
int cycle_len = depth[adj[i].first];
int offset = 0;
for(auto [x,y]:t)curr.insert({(x-adj[i].second)%cycle_len,y});
for(auto [x,y]:t)offset-=(x-adj[i].second)/cycle_len;
function<void(int,int)> calc = [&](int x,int len){
for(auto[t,idx]:queries[x]){
t-=len;
if(t<0)continue;
ans[idx]+=curr.size()*(t/cycle_len);
t%=cycle_len;
ans[idx]+=curr.order_of_key({t,INF});
ans[idx]+=offset;
}
if(x==i)return;
calc(adj[x].first,len+adj[x].second);
};
calc(adj[i].first,adj[i].second);
}
for(int i=1;i<=q;i++)cout<<ans[i]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
1372 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
3 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1740 KB |
Output is correct |
7 |
Correct |
3 ms |
1884 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
3 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1884 KB |
Output is correct |
14 |
Correct |
4 ms |
1372 KB |
Output is correct |
15 |
Correct |
4 ms |
1628 KB |
Output is correct |
16 |
Correct |
4 ms |
1652 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
3 ms |
1576 KB |
Output is correct |
19 |
Correct |
3 ms |
1628 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
9400 KB |
Output is correct |
2 |
Correct |
132 ms |
34128 KB |
Output is correct |
3 |
Correct |
138 ms |
36040 KB |
Output is correct |
4 |
Correct |
146 ms |
36508 KB |
Output is correct |
5 |
Correct |
165 ms |
81952 KB |
Output is correct |
6 |
Correct |
160 ms |
88916 KB |
Output is correct |
7 |
Correct |
115 ms |
37056 KB |
Output is correct |
8 |
Correct |
117 ms |
37128 KB |
Output is correct |
9 |
Correct |
197 ms |
90660 KB |
Output is correct |
10 |
Correct |
156 ms |
88800 KB |
Output is correct |
11 |
Correct |
237 ms |
89688 KB |
Output is correct |
12 |
Correct |
250 ms |
89648 KB |
Output is correct |
13 |
Correct |
227 ms |
89684 KB |
Output is correct |
14 |
Correct |
183 ms |
87320 KB |
Output is correct |
15 |
Correct |
166 ms |
67040 KB |
Output is correct |
16 |
Correct |
134 ms |
65348 KB |
Output is correct |
17 |
Correct |
145 ms |
65364 KB |
Output is correct |
18 |
Correct |
65 ms |
24920 KB |
Output is correct |
19 |
Correct |
74 ms |
25000 KB |
Output is correct |
20 |
Correct |
119 ms |
40208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
1372 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
3 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1740 KB |
Output is correct |
7 |
Correct |
3 ms |
1884 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
3 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1884 KB |
Output is correct |
14 |
Correct |
4 ms |
1372 KB |
Output is correct |
15 |
Correct |
4 ms |
1628 KB |
Output is correct |
16 |
Correct |
4 ms |
1652 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
3 ms |
1576 KB |
Output is correct |
19 |
Correct |
3 ms |
1628 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
69 ms |
9400 KB |
Output is correct |
22 |
Correct |
132 ms |
34128 KB |
Output is correct |
23 |
Correct |
138 ms |
36040 KB |
Output is correct |
24 |
Correct |
146 ms |
36508 KB |
Output is correct |
25 |
Correct |
165 ms |
81952 KB |
Output is correct |
26 |
Correct |
160 ms |
88916 KB |
Output is correct |
27 |
Correct |
115 ms |
37056 KB |
Output is correct |
28 |
Correct |
117 ms |
37128 KB |
Output is correct |
29 |
Correct |
197 ms |
90660 KB |
Output is correct |
30 |
Correct |
156 ms |
88800 KB |
Output is correct |
31 |
Correct |
237 ms |
89688 KB |
Output is correct |
32 |
Correct |
250 ms |
89648 KB |
Output is correct |
33 |
Correct |
227 ms |
89684 KB |
Output is correct |
34 |
Correct |
183 ms |
87320 KB |
Output is correct |
35 |
Correct |
166 ms |
67040 KB |
Output is correct |
36 |
Correct |
134 ms |
65348 KB |
Output is correct |
37 |
Correct |
145 ms |
65364 KB |
Output is correct |
38 |
Correct |
65 ms |
24920 KB |
Output is correct |
39 |
Correct |
74 ms |
25000 KB |
Output is correct |
40 |
Correct |
119 ms |
40208 KB |
Output is correct |
41 |
Correct |
328 ms |
69620 KB |
Output is correct |
42 |
Correct |
161 ms |
47044 KB |
Output is correct |
43 |
Correct |
113 ms |
40028 KB |
Output is correct |
44 |
Correct |
345 ms |
69816 KB |
Output is correct |
45 |
Correct |
238 ms |
105812 KB |
Output is correct |
46 |
Correct |
259 ms |
106648 KB |
Output is correct |
47 |
Correct |
251 ms |
108184 KB |
Output is correct |
48 |
Correct |
271 ms |
105440 KB |
Output is correct |
49 |
Correct |
271 ms |
105740 KB |
Output is correct |
50 |
Correct |
293 ms |
67468 KB |
Output is correct |
51 |
Correct |
260 ms |
66764 KB |
Output is correct |
52 |
Correct |
374 ms |
107568 KB |
Output is correct |
53 |
Correct |
391 ms |
108884 KB |
Output is correct |
54 |
Correct |
438 ms |
107528 KB |
Output is correct |
55 |
Correct |
421 ms |
106524 KB |
Output is correct |
56 |
Correct |
299 ms |
88780 KB |
Output is correct |
57 |
Correct |
310 ms |
89684 KB |
Output is correct |
58 |
Correct |
312 ms |
91220 KB |
Output is correct |
59 |
Correct |
332 ms |
87892 KB |
Output is correct |
60 |
Correct |
306 ms |
88336 KB |
Output is correct |
61 |
Correct |
295 ms |
88380 KB |
Output is correct |
62 |
Correct |
383 ms |
80012 KB |
Output is correct |
63 |
Correct |
218 ms |
51852 KB |
Output is correct |
64 |
Correct |
213 ms |
51964 KB |
Output is correct |
65 |
Correct |
216 ms |
52500 KB |
Output is correct |
66 |
Correct |
250 ms |
51756 KB |
Output is correct |
67 |
Correct |
253 ms |
51828 KB |
Output is correct |
68 |
Correct |
271 ms |
51204 KB |
Output is correct |
69 |
Correct |
410 ms |
72436 KB |
Output is correct |
70 |
Correct |
386 ms |
68936 KB |
Output is correct |
71 |
Correct |
332 ms |
70296 KB |
Output is correct |
72 |
Correct |
303 ms |
70532 KB |
Output is correct |