#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e5+7;
vector<ll>S[LIM];
vector<pair<ll,ll>>V[LIM], pyt[LIM];
vector<pair<pair<ll,ll>,ll>>M;
ll A[LIM], B[LIM], n, m, L, C;
pair<ll,ll>T[2*LIM];
ll nxt[LIM], kraw[LIM], odw[LIM], odl[LIM], cykl[LIM], pre[LIM], post[LIM], wynik[LIM], lpre=-1;
ll korzen[LIM], tr[4*LIM], suma[LIM], N=1;
void upd(int v, ll x) {
v+=N;
while(v) {
tr[v]+=x;
v/=2;
}
}
ll cnt(int l, int r) {
if(l>r) return 0;
l+=N; r+=N;
ll ans=tr[l];
if(l!=r) ans+=tr[r];
while(l/2!=r/2) {
if(l%2==0) ans+=tr[l+1];
if(r%2==1) ans+=tr[r-1];
l/=2; r/=2;
}
return ans;
}
void DFS(int x) {
pre[x]=++lpre;
for(auto i : V[x]) {
odl[i.st]=odl[x]+i.nd;
korzen[i.st]=korzen[x];
DFS(i.st);
}
post[x]=lpre;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> L >> C;
while(N<n) N*=2;
rep(i, n) cin >> A[i];
rep(i, m) cin >> B[i];
ll l=n-1, d=0;
for(int i=n-1; i>=0; --i) {
while(d<C%L) {
d+=(A[l]-A[(l-1+n)%n]+L)%L;
l=(l-1+n)%n;
}
nxt[i]=l;
kraw[i]=d;
if(d<C) kraw[i]+=((C-d+L-1)/L)*L;
d-=(A[i]-A[(i-1+n)%n]+L)%L;
}
rep(i, n) korzen[i]=-1;
rep(i, n) if(!odw[i]) {
int p=i;
while(!odw[p]) {
odw[p]=i+1;
p=nxt[p];
}
if(odw[p]==i+1) {
korzen[p]=p;
int q=p;
do {
cykl[q]=1;
q=nxt[q];
} while(q!=p);
}
}
rep(i, n) if(korzen[i]!=i) V[nxt[i]].pb({i, kraw[i]});
rep(i, n) if(korzen[i]==i) DFS(i);
int q;
cin >> q;
rep(i, q) {
ll v, t;
cin >> v >> t; --v;
pyt[v].pb({t, i});
}
rep(i, n) T[i]={A[i], i};
rep(i, m) T[n+i]={B[i], n+i};
sort(T, T+n+m);
int lst=0;
rep(i, n+m) if(T[i].nd<n) lst=T[i].nd;
rep(i, n+m) {
if(T[i].nd<n) lst=T[i].nd;
else {
S[korzen[lst]].pb(odl[lst]+(T[i].st-A[lst]+L)%L);
M.pb({{odl[lst]+(T[i].st-A[lst]+L)%L, -1}, lst});
}
}
rep(i, n) if(korzen[i]!=i) {
for(auto j : pyt[i]) M.pb({{odl[i]+j.st, j.nd}, i});
}
sort(all(M));
for(auto i : M) {
if(i.st.nd==-1) {
upd(pre[i.nd], 1);
} else {
wynik[i.st.nd]=cnt(pre[i.nd], post[i.nd]);
}
}
rep(i, n) if(cykl[i]) suma[korzen[i]]+=kraw[i];
rep(i, n) if(cykl[i]) {
ll m=suma[korzen[i]], d=(m-odl[i])%m;
for(auto j : pyt[i]) {
for(auto l : S[korzen[i]]) {
if(l+d<=j.st) wynik[j.nd]+=(j.st-l-d)/m+1;
}
}
}
rep(i, q) cout << wynik[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31576 KB |
Output is correct |
2 |
Correct |
6 ms |
31580 KB |
Output is correct |
3 |
Correct |
26 ms |
31760 KB |
Output is correct |
4 |
Correct |
7 ms |
33884 KB |
Output is correct |
5 |
Correct |
7 ms |
34124 KB |
Output is correct |
6 |
Correct |
7 ms |
34140 KB |
Output is correct |
7 |
Correct |
7 ms |
34144 KB |
Output is correct |
8 |
Correct |
7 ms |
33832 KB |
Output is correct |
9 |
Correct |
6 ms |
31788 KB |
Output is correct |
10 |
Correct |
6 ms |
33836 KB |
Output is correct |
11 |
Correct |
7 ms |
33836 KB |
Output is correct |
12 |
Correct |
12 ms |
34136 KB |
Output is correct |
13 |
Correct |
28 ms |
34260 KB |
Output is correct |
14 |
Correct |
17 ms |
34128 KB |
Output is correct |
15 |
Correct |
7 ms |
32076 KB |
Output is correct |
16 |
Correct |
6 ms |
34136 KB |
Output is correct |
17 |
Correct |
7 ms |
34140 KB |
Output is correct |
18 |
Correct |
7 ms |
33836 KB |
Output is correct |
19 |
Correct |
7 ms |
33924 KB |
Output is correct |
20 |
Correct |
7 ms |
34092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5071 ms |
19024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31576 KB |
Output is correct |
2 |
Correct |
6 ms |
31580 KB |
Output is correct |
3 |
Correct |
26 ms |
31760 KB |
Output is correct |
4 |
Correct |
7 ms |
33884 KB |
Output is correct |
5 |
Correct |
7 ms |
34124 KB |
Output is correct |
6 |
Correct |
7 ms |
34140 KB |
Output is correct |
7 |
Correct |
7 ms |
34144 KB |
Output is correct |
8 |
Correct |
7 ms |
33832 KB |
Output is correct |
9 |
Correct |
6 ms |
31788 KB |
Output is correct |
10 |
Correct |
6 ms |
33836 KB |
Output is correct |
11 |
Correct |
7 ms |
33836 KB |
Output is correct |
12 |
Correct |
12 ms |
34136 KB |
Output is correct |
13 |
Correct |
28 ms |
34260 KB |
Output is correct |
14 |
Correct |
17 ms |
34128 KB |
Output is correct |
15 |
Correct |
7 ms |
32076 KB |
Output is correct |
16 |
Correct |
6 ms |
34136 KB |
Output is correct |
17 |
Correct |
7 ms |
34140 KB |
Output is correct |
18 |
Correct |
7 ms |
33836 KB |
Output is correct |
19 |
Correct |
7 ms |
33924 KB |
Output is correct |
20 |
Correct |
7 ms |
34092 KB |
Output is correct |
21 |
Execution timed out |
5071 ms |
19024 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |