#include<bits/stdc++.h>
#define ll long long
#define ntr "\n"
#define mod (ll)(1e9+7)
#define taskname "temp"
#define frep freopen(taskname".inp","r",stdin); freopen(taskname".out","w",stdout);
using namespace std;
ll a[200001],b[200001];
ll n,m,l,c,q;
vector<pair<ll,ll>> adj[400001],adjrev[400001];
ll vis[400001];
ll dist[200001];
ll distrev[400001];
ll pos[200001];
ll sz[400001];
ll euler[400001];
ll fenwick[200001];
ll line[200001];
ll ans[200001];
vector<array<ll,3>> sweep;
ll ones[200001];
ll tin;
ll ord;
ll cnt;
vector<vector<pair<ll,ll>>> compart;
vector<ll> st,ed;
vector<ll> len;
ll num[400001];
ll insub[400001];
bool inloop[400001];
ll root;
void update(ll x,ll val){
for(;x<=n+m;x+=x&-x){
insub[x]+=val;
}
}
ll query(ll x){
ll s=0;
for(;x>0;x-=x&-x){
s+=insub[x];
}
return s;
}
void upd(ll x,ll val,ll type){
if(type){
for(;x<=m;x+=x&-x){
fenwick[x]+=val;
}
}
else{
for(;x<=m;x+=x&-x){
ones[x]+=val;
}
}
}
ll get(ll x,ll type){
ll s=0;
for(;x;x-=x&-x){
if(type) s+=fenwick[x];
else s+=ones[x];
}
return s;
}
void dfs(ll u){
if(root) return ;
if(vis[u]){
root=u;
return ;
}
vis[u]=1;
for(auto [v,w]:adj[u]){
dfs(v);
}
}
void dfs1(ll u=root,ll d=0,ll par=0){
distrev[u]=d;
vis[u]=1;
sz[u]=1;
euler[u]=++tin;
num[u]=cnt;
if(u>n){
compart[cnt].push_back({d%len[cnt],u-n});
line[u-n]=d/len[cnt];
}
for(auto [v,w]:adjrev[u]){
if(v==root) continue;
dfs1(v,d+w,u);
sz[u]+=sz[v];
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//frep;
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];
for(int i=1;i<=n;i++){
ll t=((a[i]-c)%l+l)%l;
ll pos=upper_bound(a+1,a+n+1,t)-a-1;
if(pos==0) pos=n;
ll v=((a[i]-a[pos])%l+l)%l;
ll cost=(c-v+l-1)/l*l+v;
adj[i].push_back({pos,cost});
adjrev[pos].push_back({i,cost});
}
for(int i=1;i<=m;i++){
ll v=upper_bound(a+1,a+n+1,b[i])-a-1;
if(v==0) v=n;
ll cost=((b[i]-a[v])%l+l)%l;
adj[i+n].push_back({v,cost});
adjrev[v].push_back({i+n,cost});
}
compart.push_back({});
len.push_back(0);
st.push_back(-1);
ed.push_back(-1);
for(int i=1;i<=m;i++){
if(vis[i+n]) continue;
root=0,dfs(i+n);
cnt++;
compart.push_back({});
st.push_back(1e18);
ed.push_back(0);
ll p=root;
dist[root]=0;
while(true){
inloop[p]=1;
auto [v,w]=adj[p][0];
if(v==root){
len.push_back(dist[p]+w);
break;
}
dist[v]=dist[p]+w;
p=v;
}
dfs1();
sort(compart[cnt].begin(),compart[cnt].end());
for(auto i:compart[cnt]){
pos[i.second]=++ord;
st[cnt]=min(st[cnt],ord);
ed[cnt]=max(ed[cnt],ord);
}
}
for(int i=1;i<=m;i++){
sweep.push_back({distrev[i+n],0,i});
}
// for(int i=1;i<=n+m;i++) cout<<euler[i]<<' ';
// cout<<ntr;
cin>>q;
for(int i=1;i<=q;i++){
ll u,t;
cin>>u>>t;
sweep.push_back({t+distrev[u],u+1,i});
}
sort(sweep.begin(),sweep.end());
for(auto i:sweep){
// cout<<i[0]<<' '<<i[1]<<' '<<i[2]<<ntr;
if(i[1]==0){
upd(pos[i[2]],line[i[2]],1);
upd(pos[i[2]],1,0);
update(euler[i[2]+n],1);
// cout<<euler[i[2]+n]<<ntr;
// for(int i=1;i<=n+m;i++){
// cout<<query(i)<<' ';
// }
// cout<<ntr;
}
else{
ll u=i[1]-1;
ll t=i[0]-distrev[u];
t-=dist[u];
ll id=i[2];
if(!inloop[u]){
ans[id]=query(euler[u]+sz[u]-1)-query(euler[u]-1);
}
else{
ans[id]+=(query(euler[u]+sz[u]-1)-query(euler[u]-1))*(dist[u]>0);
// cout<<ans[id]<<ntr;
// cout<<euler[u]+sz[u]-1<<' '<<euler[u]-1<<ntr;
ll p=t/len[num[u]];
ll q=t%len[num[u]];
// cout<<t<<' '<<p<<' '<<q<<ntr;
ll lo=0,hi=compart[num[u]].size()-1,pos=-1;
while(lo<=hi){
ll mid=(lo+hi)/2;
if(compart[num[u]][mid].first<=q) pos=mid,lo=mid+1;
else hi=mid-1;
}
ans[id]+=(get(ed[num[u]],0)-get(st[num[u]]-1,0))*p-(get(ed[num[u]],1)-get(st[num[u]]-1,1));
//cout<<ans[id]<<ntr;
// cout<<(get(ed[num[u]],0)-get(st[num[u]]-1,0))<<' '<<(get(ed[num[u]],1)-get(st[num[u]]-1,1))<<ntr;
if(pos>=0){
ans[id]+=get(st[num[u]]+pos,0)-get(st[num[u]]-1,0);
// cout<<get(st[num[u]]+pos,0)-get(st[num[u]]-1,0)<<ntr;
}
}
}
}
for(int i=1;i<=q;i++){
cout<<ans[i]<<ntr;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
43600 KB |
Output is correct |
2 |
Correct |
9 ms |
44304 KB |
Output is correct |
3 |
Correct |
8 ms |
44228 KB |
Output is correct |
4 |
Correct |
9 ms |
44572 KB |
Output is correct |
5 |
Correct |
9 ms |
44608 KB |
Output is correct |
6 |
Correct |
9 ms |
44608 KB |
Output is correct |
7 |
Correct |
9 ms |
44608 KB |
Output is correct |
8 |
Correct |
9 ms |
44352 KB |
Output is correct |
9 |
Correct |
8 ms |
42304 KB |
Output is correct |
10 |
Correct |
8 ms |
44352 KB |
Output is correct |
11 |
Correct |
9 ms |
44360 KB |
Output is correct |
12 |
Correct |
9 ms |
44608 KB |
Output is correct |
13 |
Correct |
9 ms |
44608 KB |
Output is correct |
14 |
Correct |
9 ms |
44540 KB |
Output is correct |
15 |
Correct |
9 ms |
42560 KB |
Output is correct |
16 |
Correct |
9 ms |
44352 KB |
Output is correct |
17 |
Correct |
9 ms |
44484 KB |
Output is correct |
18 |
Correct |
9 ms |
44532 KB |
Output is correct |
19 |
Correct |
10 ms |
44352 KB |
Output is correct |
20 |
Correct |
10 ms |
44608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
57476 KB |
Output is correct |
2 |
Correct |
140 ms |
75996 KB |
Output is correct |
3 |
Correct |
114 ms |
77488 KB |
Output is correct |
4 |
Correct |
129 ms |
73432 KB |
Output is correct |
5 |
Correct |
154 ms |
93588 KB |
Output is correct |
6 |
Correct |
142 ms |
93660 KB |
Output is correct |
7 |
Correct |
111 ms |
75172 KB |
Output is correct |
8 |
Correct |
119 ms |
77224 KB |
Output is correct |
9 |
Correct |
178 ms |
98272 KB |
Output is correct |
10 |
Correct |
154 ms |
98112 KB |
Output is correct |
11 |
Correct |
259 ms |
97244 KB |
Output is correct |
12 |
Correct |
265 ms |
97244 KB |
Output is correct |
13 |
Correct |
245 ms |
97244 KB |
Output is correct |
14 |
Correct |
229 ms |
97116 KB |
Output is correct |
15 |
Correct |
183 ms |
88936 KB |
Output is correct |
16 |
Correct |
159 ms |
86236 KB |
Output is correct |
17 |
Correct |
157 ms |
86168 KB |
Output is correct |
18 |
Correct |
97 ms |
61736 KB |
Output is correct |
19 |
Correct |
95 ms |
61776 KB |
Output is correct |
20 |
Correct |
134 ms |
76012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
43600 KB |
Output is correct |
2 |
Correct |
9 ms |
44304 KB |
Output is correct |
3 |
Correct |
8 ms |
44228 KB |
Output is correct |
4 |
Correct |
9 ms |
44572 KB |
Output is correct |
5 |
Correct |
9 ms |
44608 KB |
Output is correct |
6 |
Correct |
9 ms |
44608 KB |
Output is correct |
7 |
Correct |
9 ms |
44608 KB |
Output is correct |
8 |
Correct |
9 ms |
44352 KB |
Output is correct |
9 |
Correct |
8 ms |
42304 KB |
Output is correct |
10 |
Correct |
8 ms |
44352 KB |
Output is correct |
11 |
Correct |
9 ms |
44360 KB |
Output is correct |
12 |
Correct |
9 ms |
44608 KB |
Output is correct |
13 |
Correct |
9 ms |
44608 KB |
Output is correct |
14 |
Correct |
9 ms |
44540 KB |
Output is correct |
15 |
Correct |
9 ms |
42560 KB |
Output is correct |
16 |
Correct |
9 ms |
44352 KB |
Output is correct |
17 |
Correct |
9 ms |
44484 KB |
Output is correct |
18 |
Correct |
9 ms |
44532 KB |
Output is correct |
19 |
Correct |
10 ms |
44352 KB |
Output is correct |
20 |
Correct |
10 ms |
44608 KB |
Output is correct |
21 |
Correct |
89 ms |
57476 KB |
Output is correct |
22 |
Correct |
140 ms |
75996 KB |
Output is correct |
23 |
Correct |
114 ms |
77488 KB |
Output is correct |
24 |
Correct |
129 ms |
73432 KB |
Output is correct |
25 |
Correct |
154 ms |
93588 KB |
Output is correct |
26 |
Correct |
142 ms |
93660 KB |
Output is correct |
27 |
Correct |
111 ms |
75172 KB |
Output is correct |
28 |
Correct |
119 ms |
77224 KB |
Output is correct |
29 |
Correct |
178 ms |
98272 KB |
Output is correct |
30 |
Correct |
154 ms |
98112 KB |
Output is correct |
31 |
Correct |
259 ms |
97244 KB |
Output is correct |
32 |
Correct |
265 ms |
97244 KB |
Output is correct |
33 |
Correct |
245 ms |
97244 KB |
Output is correct |
34 |
Correct |
229 ms |
97116 KB |
Output is correct |
35 |
Correct |
183 ms |
88936 KB |
Output is correct |
36 |
Correct |
159 ms |
86236 KB |
Output is correct |
37 |
Correct |
157 ms |
86168 KB |
Output is correct |
38 |
Correct |
97 ms |
61736 KB |
Output is correct |
39 |
Correct |
95 ms |
61776 KB |
Output is correct |
40 |
Correct |
134 ms |
76012 KB |
Output is correct |
41 |
Correct |
253 ms |
100016 KB |
Output is correct |
42 |
Correct |
232 ms |
102008 KB |
Output is correct |
43 |
Correct |
121 ms |
67804 KB |
Output is correct |
44 |
Correct |
205 ms |
99628 KB |
Output is correct |
45 |
Correct |
238 ms |
117932 KB |
Output is correct |
46 |
Correct |
274 ms |
115884 KB |
Output is correct |
47 |
Correct |
267 ms |
116348 KB |
Output is correct |
48 |
Correct |
238 ms |
118188 KB |
Output is correct |
49 |
Correct |
238 ms |
119168 KB |
Output is correct |
50 |
Correct |
203 ms |
98524 KB |
Output is correct |
51 |
Correct |
195 ms |
98524 KB |
Output is correct |
52 |
Correct |
238 ms |
118960 KB |
Output is correct |
53 |
Correct |
245 ms |
120024 KB |
Output is correct |
54 |
Correct |
248 ms |
119020 KB |
Output is correct |
55 |
Correct |
303 ms |
118804 KB |
Output is correct |
56 |
Correct |
223 ms |
108480 KB |
Output is correct |
57 |
Correct |
229 ms |
107040 KB |
Output is correct |
58 |
Correct |
238 ms |
109060 KB |
Output is correct |
59 |
Correct |
199 ms |
108472 KB |
Output is correct |
60 |
Correct |
201 ms |
108784 KB |
Output is correct |
61 |
Correct |
206 ms |
109316 KB |
Output is correct |
62 |
Correct |
302 ms |
109356 KB |
Output is correct |
63 |
Correct |
174 ms |
87600 KB |
Output is correct |
64 |
Correct |
159 ms |
87756 KB |
Output is correct |
65 |
Correct |
165 ms |
87800 KB |
Output is correct |
66 |
Correct |
153 ms |
86760 KB |
Output is correct |
67 |
Correct |
156 ms |
86636 KB |
Output is correct |
68 |
Correct |
142 ms |
86616 KB |
Output is correct |
69 |
Correct |
232 ms |
100272 KB |
Output is correct |
70 |
Correct |
259 ms |
99216 KB |
Output is correct |
71 |
Correct |
220 ms |
98880 KB |
Output is correct |
72 |
Correct |
260 ms |
98992 KB |
Output is correct |