#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
#define int long long
int N,M,L,C,Q;
int A[maxn],B[maxn],T[maxn];
int dd[maxn],deg[maxn],child[maxn],son[maxn];
int nxt[maxn],dist[maxn],sum[maxn];
vector<int> edge[maxn],f[maxn];
int ans[maxn];
vector<int> qq[maxn];
struct BIT{
int sz;
vector<int> com,bit;
void init(){
for(int i=1;i<=sz;i++) bit[i]=0;
}
void reset(){
com.clear();
}
void add(int x){
com.push_back(x);
}
void build(){
sort(com.begin(),com.end());
com.erase(unique(com.begin(),com.end()),com.end());
sz=(int)com.size();
bit.assign(sz+1,0);
}
int get(int x){
return upper_bound(com.begin(),com.end(),x)-com.begin();
}
void update(int x,int val){
x=get(x);
for(int i=x;i<=sz;i+=(i&(-i))) bit[i]+=val;
}
int query(int x){
x=get(x);
int res=0;
for(int i=x;i>=1;i-=(i&(-i))) res+=bit[i];
return res;
}
};
struct BIT2{
int sz,ss[maxn];
vector<int> com[maxn],bit[maxn];
void init(){
for(int i=1;i<=sz;i++) for(int j=1;j<=ss[i];j++) bit[i][j]=0;
}
void reset(){
for(int i=1;i<=sz;i++) com[i].clear();
}
void add(int x,int y){
for(int i=x;i<=sz;i+=(i&(-i))) com[i].push_back(y);
}
int get(int x,int y){
return upper_bound(com[x].begin(),com[x].end(),y)-com[x].begin();
}
void build(){
for(int i=1;i<=sz;i++){
sort(com[i].begin(),com[i].end());
com[i].erase(unique(com[i].begin(),com[i].end()),com[i].end());
ss[i]=(int)com[i].size();
bit[i].assign(ss[i]+1,0);
}
}
void update(int x,int y,int val){
for(int i=x;i<=sz;i+=(i&(-i))){
int ny=get(i,y);
for(int j=ny;j<=ss[i];j+=(j&(-j))) bit[i][j]+=val;
}
}
int query(int x,int y){
int res=0;
for(int i=x;i>=1;i-=(i&(-i))){
int ny=get(i,y);
for(int j=ny;j>=1;j-=(j&(-j))) res+=bit[i][j];
}
return res;
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> N >> M >> L >> C;
for(int i=1;i<=N;i++) cin >> A[i];
for(int i=1;i<=M;i++) cin >> B[i];
cin >> Q;
for(int i=1;i<=Q;i++){
int v;cin >> v >> T[i];
qq[v].push_back(i);
}
for(int i=1;i<=M;i++){
int p=(lower_bound(A+1,A+N+1,B[i])-A+2*N-2)%N+1;
f[p].push_back((B[i]-A[p]+L)%L);
}
for(int i=1;i<=N;i++){
int val=A[i]+((A[i]-A[1])<(C%L))*L-(C%L);
nxt[i]=upper_bound(A+1,A+N+1,val)-A-1;
dist[i]=val-A[nxt[i]]+C;deg[nxt[i]]++;
}
queue<int> q;
for(int i=1;i<=N;i++) if(!deg[i]) q.push(i);
while(!q.empty()){
int u=q.front();q.pop();
int v=nxt[u];
edge[v].push_back(u);
if(!(--deg[v])) q.push(v);
}
BIT cnt,cnt0;
BIT2 cnt2;
function<void(int)> pre_dfs = [&](int u){
child[u]=1;
for(int &x:f[u]) x+=dd[u],cnt.add(x);
for(int v:edge[u]){
dd[v]=dd[u]+dist[v];
pre_dfs(v);child[u]+=child[v];
if(child[son[u]]<child[v]) son[u]=v;
}
};
function<void(int,int)> dfs = [&](int u,int t){
for(int v:edge[u]) if(v!=son[u]) dfs(v,0);
if(son[u]) dfs(son[u],1),swap(f[u],f[son[u]]);
else for(int x:f[u]) cnt.update(x,1);
for(int v:edge[u]) for(int x:f[v]) cnt.update(x,1),f[u].push_back(x);
if(!deg[u]){
for(int id:qq[u]) ans[id]=cnt.query(dd[u]+T[id]);
}
if(!t) for(int x:f[u]) cnt.update(x,-1);
/*
for(int v:edge[u]) dfs(v,0);
cnt.init();
for(int v:edge[u]) for(int x:f[v]) f[u].push_back(x);
for(int x:f[u]) cnt.update(x,1);
if(!deg[u]){
for(int id:qq[u]) ans[id]=cnt.query(dd[u]+T[id]);
}
*/
};
for(int i=1;i<=N;i++) if(deg[i]){
cnt.reset();
pre_dfs(i);
cnt.build();
dfs(i,1);
}
for(int i=1;i<=N;i++){
if(!deg[i]) continue;
int u=i;
vector<int> cycle;
while(deg[u]) cycle.push_back(u),deg[u]=0,u=nxt[u];
int sz=(int)cycle.size();
for(int i=1;i<sz;i++) sum[i]=sum[i-1]+dist[cycle[i-1]];
int total=sum[sz-1]+dist[cycle.back()];
/*
for(int i=0;i<sz;i++){
u=cycle[i];
for(int id:qq[u]){
for(int j=0;j<=i;j++) for(int x:f[cycle[j]]){
int val=x+sum[i]-sum[j];
if(val<=T[id]) ans[id]+=(T[id]-val)/total+1;
}
for(int j=i+1;j<sz;j++) for(int x:f[cycle[j]]){
int val=x+sum[i]-sum[j]+total;
if(val<=T[id]) ans[id]+=(T[id]-val)/total+1;
}
}
}
*/
cnt.reset();cnt0.reset();
for(int i=0;i<sz;i++){
for(int x:f[cycle[i]]){
int val=x-sum[i];
int d=(val%total+total)%total;
int c=(val-d)/total;
cnt.add(c);cnt0.add(c);
}
}
cnt.build();cnt0.build();
cnt2.reset();
cnt2.sz=cnt.sz;
for(int i=0;i<sz;i++){
for(int x:f[cycle[i]]){
int val=x-sum[i];
int d=(val%total+total)%total;
int c=(val-d)/total;
cnt2.add(cnt.get(c),d);
}
}
cnt2.build();
for(int i=0;i<sz;i++){
u=cycle[i];
for(int x:f[u]){
int val=x-sum[i];
int d=(val%total+total)%total;
int c=(val-d)/total;
cnt2.update(cnt.get(c),d,1);
cnt0.update(c,1);
cnt.update(c,c);
}
for(int id:qq[u]){
int val=T[id]-sum[i];
int b=(val%total+total)%total;
int a=(val-b)/total;
ans[id]+=cnt2.query(cnt.get(a),b)+cnt0.query(a)*a-cnt.query(a);
}
}
cnt.init();
cnt0.init();
cnt2.init();
for(int i=sz-1;i>=0;i--){
u=cycle[i];
for(int id:qq[u]){
int val=T[id]-sum[i];
int b=(val%total+total)%total;
int a=(val-b)/total-1;
ans[id]+=cnt2.query(cnt.get(a),b)+cnt0.query(a)*a-cnt.query(a);
}
for(int x:f[u]){
int val=x-sum[i];
int d=(val%total+total)%total;
int c=(val-d)/total;
cnt2.update(cnt.get(c),d,1);
cnt0.update(c,1);
cnt.update(c,c);
}
}
}
for(int i=1;i<=Q;i++) cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
25688 KB |
Output is correct |
2 |
Correct |
14 ms |
25948 KB |
Output is correct |
3 |
Correct |
15 ms |
25908 KB |
Output is correct |
4 |
Correct |
16 ms |
26204 KB |
Output is correct |
5 |
Correct |
18 ms |
26972 KB |
Output is correct |
6 |
Correct |
19 ms |
26972 KB |
Output is correct |
7 |
Correct |
19 ms |
26900 KB |
Output is correct |
8 |
Correct |
17 ms |
26204 KB |
Output is correct |
9 |
Correct |
15 ms |
26204 KB |
Output is correct |
10 |
Correct |
15 ms |
26184 KB |
Output is correct |
11 |
Correct |
15 ms |
26156 KB |
Output is correct |
12 |
Correct |
15 ms |
25948 KB |
Output is correct |
13 |
Correct |
16 ms |
26204 KB |
Output is correct |
14 |
Correct |
15 ms |
26204 KB |
Output is correct |
15 |
Correct |
19 ms |
26716 KB |
Output is correct |
16 |
Correct |
20 ms |
26712 KB |
Output is correct |
17 |
Correct |
18 ms |
26716 KB |
Output is correct |
18 |
Correct |
18 ms |
26616 KB |
Output is correct |
19 |
Correct |
16 ms |
26712 KB |
Output is correct |
20 |
Correct |
17 ms |
26716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
33736 KB |
Output is correct |
2 |
Correct |
174 ms |
49492 KB |
Output is correct |
3 |
Correct |
109 ms |
41040 KB |
Output is correct |
4 |
Correct |
112 ms |
40900 KB |
Output is correct |
5 |
Correct |
164 ms |
89744 KB |
Output is correct |
6 |
Correct |
137 ms |
89428 KB |
Output is correct |
7 |
Correct |
111 ms |
51396 KB |
Output is correct |
8 |
Correct |
108 ms |
51392 KB |
Output is correct |
9 |
Correct |
150 ms |
52128 KB |
Output is correct |
10 |
Correct |
98 ms |
49288 KB |
Output is correct |
11 |
Correct |
175 ms |
51108 KB |
Output is correct |
12 |
Correct |
176 ms |
51104 KB |
Output is correct |
13 |
Correct |
188 ms |
51104 KB |
Output is correct |
14 |
Correct |
104 ms |
48244 KB |
Output is correct |
15 |
Correct |
153 ms |
51228 KB |
Output is correct |
16 |
Correct |
159 ms |
74064 KB |
Output is correct |
17 |
Correct |
145 ms |
73468 KB |
Output is correct |
18 |
Correct |
89 ms |
45516 KB |
Output is correct |
19 |
Correct |
85 ms |
45008 KB |
Output is correct |
20 |
Correct |
134 ms |
57684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
25688 KB |
Output is correct |
2 |
Correct |
14 ms |
25948 KB |
Output is correct |
3 |
Correct |
15 ms |
25908 KB |
Output is correct |
4 |
Correct |
16 ms |
26204 KB |
Output is correct |
5 |
Correct |
18 ms |
26972 KB |
Output is correct |
6 |
Correct |
19 ms |
26972 KB |
Output is correct |
7 |
Correct |
19 ms |
26900 KB |
Output is correct |
8 |
Correct |
17 ms |
26204 KB |
Output is correct |
9 |
Correct |
15 ms |
26204 KB |
Output is correct |
10 |
Correct |
15 ms |
26184 KB |
Output is correct |
11 |
Correct |
15 ms |
26156 KB |
Output is correct |
12 |
Correct |
15 ms |
25948 KB |
Output is correct |
13 |
Correct |
16 ms |
26204 KB |
Output is correct |
14 |
Correct |
15 ms |
26204 KB |
Output is correct |
15 |
Correct |
19 ms |
26716 KB |
Output is correct |
16 |
Correct |
20 ms |
26712 KB |
Output is correct |
17 |
Correct |
18 ms |
26716 KB |
Output is correct |
18 |
Correct |
18 ms |
26616 KB |
Output is correct |
19 |
Correct |
16 ms |
26712 KB |
Output is correct |
20 |
Correct |
17 ms |
26716 KB |
Output is correct |
21 |
Correct |
76 ms |
33736 KB |
Output is correct |
22 |
Correct |
174 ms |
49492 KB |
Output is correct |
23 |
Correct |
109 ms |
41040 KB |
Output is correct |
24 |
Correct |
112 ms |
40900 KB |
Output is correct |
25 |
Correct |
164 ms |
89744 KB |
Output is correct |
26 |
Correct |
137 ms |
89428 KB |
Output is correct |
27 |
Correct |
111 ms |
51396 KB |
Output is correct |
28 |
Correct |
108 ms |
51392 KB |
Output is correct |
29 |
Correct |
150 ms |
52128 KB |
Output is correct |
30 |
Correct |
98 ms |
49288 KB |
Output is correct |
31 |
Correct |
175 ms |
51108 KB |
Output is correct |
32 |
Correct |
176 ms |
51104 KB |
Output is correct |
33 |
Correct |
188 ms |
51104 KB |
Output is correct |
34 |
Correct |
104 ms |
48244 KB |
Output is correct |
35 |
Correct |
153 ms |
51228 KB |
Output is correct |
36 |
Correct |
159 ms |
74064 KB |
Output is correct |
37 |
Correct |
145 ms |
73468 KB |
Output is correct |
38 |
Correct |
89 ms |
45516 KB |
Output is correct |
39 |
Correct |
85 ms |
45008 KB |
Output is correct |
40 |
Correct |
134 ms |
57684 KB |
Output is correct |
41 |
Correct |
353 ms |
76436 KB |
Output is correct |
42 |
Correct |
170 ms |
52188 KB |
Output is correct |
43 |
Correct |
92 ms |
44484 KB |
Output is correct |
44 |
Correct |
195 ms |
57540 KB |
Output is correct |
45 |
Correct |
450 ms |
135244 KB |
Output is correct |
46 |
Correct |
498 ms |
135952 KB |
Output is correct |
47 |
Correct |
507 ms |
137656 KB |
Output is correct |
48 |
Correct |
289 ms |
111900 KB |
Output is correct |
49 |
Correct |
349 ms |
121020 KB |
Output is correct |
50 |
Correct |
207 ms |
67456 KB |
Output is correct |
51 |
Correct |
213 ms |
66744 KB |
Output is correct |
52 |
Correct |
258 ms |
68300 KB |
Output is correct |
53 |
Correct |
256 ms |
64640 KB |
Output is correct |
54 |
Correct |
288 ms |
68548 KB |
Output is correct |
55 |
Correct |
238 ms |
63452 KB |
Output is correct |
56 |
Correct |
594 ms |
121532 KB |
Output is correct |
57 |
Correct |
606 ms |
122260 KB |
Output is correct |
58 |
Correct |
579 ms |
123840 KB |
Output is correct |
59 |
Correct |
313 ms |
94656 KB |
Output is correct |
60 |
Correct |
324 ms |
98100 KB |
Output is correct |
61 |
Correct |
357 ms |
100496 KB |
Output is correct |
62 |
Correct |
274 ms |
61128 KB |
Output is correct |
63 |
Correct |
383 ms |
83396 KB |
Output is correct |
64 |
Correct |
379 ms |
83652 KB |
Output is correct |
65 |
Correct |
421 ms |
83892 KB |
Output is correct |
66 |
Correct |
184 ms |
61524 KB |
Output is correct |
67 |
Correct |
215 ms |
62392 KB |
Output is correct |
68 |
Correct |
190 ms |
60220 KB |
Output is correct |
69 |
Correct |
341 ms |
79168 KB |
Output is correct |
70 |
Correct |
323 ms |
76440 KB |
Output is correct |
71 |
Correct |
438 ms |
80192 KB |
Output is correct |
72 |
Correct |
377 ms |
80528 KB |
Output is correct |