#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=4e5+7;
vector<ll>S[LIM];
vector<pair<ll,ll>>V[LIM], pyt[LIM], pyt2[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) if(n>1) {
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;
}
if(n==1) {
kraw[0]=(C+L-1)/L;
kraw[0]*=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]) {
pyt2[korzen[i]].pb({j.st-d, j.nd});
}
}
rep(i, n) if(korzen[i]==i && S[i].size()) {
ll m=suma[i];
vector<ll>K;
for(auto j : S[i]) K.pb(j%m);
sort(all(K));
sort(all(S[i]));
sort(all(pyt2[i]));
l=0;
ll ilek=0, sumk=0;
N=1;
while(N<K.size()) N*=2;
rep(j, 2*N) tr[j]=0;
for(auto j : pyt2[i]) {
while(l<S[i].size() && S[i][l]<=j.st) {
++ilek;
sumk+=S[i][l]/m;
ll po=0, ko=K.size()-1;
while(po<ko) {
ll sr=(po+ko+1)/2;
if(K[sr]>S[i][l]%m) ko=sr-1; else po=sr;
}
upd(po, 1);
++l;
}
ll po=0, ko=K.size()-1;
while(po<ko) {
ll sr=(po+ko+1)/2;
if(K[sr]>j.st%m) ko=sr-1; else po=sr;
}
if(K[po]<=j.st%m) wynik[j.nd]+=cnt(0, po);
wynik[j.nd]+=ilek*(j.st/m);
wynik[j.nd]-=sumk;
}
}
rep(i, q) cout << wynik[i] << '\n';
}
Compilation message
harvest.cpp: In function 'int main()':
harvest.cpp:131:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | while(N<K.size()) N*=2;
| ~^~~~~~~~~
harvest.cpp:134:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | while(l<S[i].size() && S[i][l]<=j.st) {
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
57436 KB |
Output is correct |
2 |
Correct |
10 ms |
59740 KB |
Output is correct |
3 |
Correct |
11 ms |
59740 KB |
Output is correct |
4 |
Correct |
11 ms |
62044 KB |
Output is correct |
5 |
Correct |
11 ms |
62152 KB |
Output is correct |
6 |
Correct |
13 ms |
62040 KB |
Output is correct |
7 |
Correct |
11 ms |
62044 KB |
Output is correct |
8 |
Correct |
12 ms |
61872 KB |
Output is correct |
9 |
Correct |
13 ms |
59948 KB |
Output is correct |
10 |
Correct |
15 ms |
61996 KB |
Output is correct |
11 |
Correct |
16 ms |
61888 KB |
Output is correct |
12 |
Correct |
12 ms |
62176 KB |
Output is correct |
13 |
Correct |
12 ms |
62040 KB |
Output is correct |
14 |
Correct |
11 ms |
62032 KB |
Output is correct |
15 |
Correct |
10 ms |
59992 KB |
Output is correct |
16 |
Correct |
11 ms |
62044 KB |
Output is correct |
17 |
Correct |
12 ms |
61976 KB |
Output is correct |
18 |
Correct |
10 ms |
61996 KB |
Output is correct |
19 |
Correct |
13 ms |
62092 KB |
Output is correct |
20 |
Correct |
12 ms |
61996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
70488 KB |
Output is correct |
2 |
Correct |
143 ms |
105908 KB |
Output is correct |
3 |
Correct |
111 ms |
99152 KB |
Output is correct |
4 |
Correct |
132 ms |
98984 KB |
Output is correct |
5 |
Correct |
154 ms |
114680 KB |
Output is correct |
6 |
Correct |
171 ms |
114948 KB |
Output is correct |
7 |
Correct |
134 ms |
106168 KB |
Output is correct |
8 |
Correct |
128 ms |
104200 KB |
Output is correct |
9 |
Correct |
212 ms |
124948 KB |
Output is correct |
10 |
Correct |
156 ms |
120024 KB |
Output is correct |
11 |
Correct |
221 ms |
124656 KB |
Output is correct |
12 |
Correct |
225 ms |
125096 KB |
Output is correct |
13 |
Correct |
222 ms |
123632 KB |
Output is correct |
14 |
Correct |
180 ms |
121252 KB |
Output is correct |
15 |
Correct |
200 ms |
117720 KB |
Output is correct |
16 |
Correct |
166 ms |
110072 KB |
Output is correct |
17 |
Correct |
171 ms |
110116 KB |
Output is correct |
18 |
Correct |
112 ms |
84180 KB |
Output is correct |
19 |
Correct |
112 ms |
83816 KB |
Output is correct |
20 |
Correct |
134 ms |
107096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
57436 KB |
Output is correct |
2 |
Correct |
10 ms |
59740 KB |
Output is correct |
3 |
Correct |
11 ms |
59740 KB |
Output is correct |
4 |
Correct |
11 ms |
62044 KB |
Output is correct |
5 |
Correct |
11 ms |
62152 KB |
Output is correct |
6 |
Correct |
13 ms |
62040 KB |
Output is correct |
7 |
Correct |
11 ms |
62044 KB |
Output is correct |
8 |
Correct |
12 ms |
61872 KB |
Output is correct |
9 |
Correct |
13 ms |
59948 KB |
Output is correct |
10 |
Correct |
15 ms |
61996 KB |
Output is correct |
11 |
Correct |
16 ms |
61888 KB |
Output is correct |
12 |
Correct |
12 ms |
62176 KB |
Output is correct |
13 |
Correct |
12 ms |
62040 KB |
Output is correct |
14 |
Correct |
11 ms |
62032 KB |
Output is correct |
15 |
Correct |
10 ms |
59992 KB |
Output is correct |
16 |
Correct |
11 ms |
62044 KB |
Output is correct |
17 |
Correct |
12 ms |
61976 KB |
Output is correct |
18 |
Correct |
10 ms |
61996 KB |
Output is correct |
19 |
Correct |
13 ms |
62092 KB |
Output is correct |
20 |
Correct |
12 ms |
61996 KB |
Output is correct |
21 |
Correct |
95 ms |
70488 KB |
Output is correct |
22 |
Correct |
143 ms |
105908 KB |
Output is correct |
23 |
Correct |
111 ms |
99152 KB |
Output is correct |
24 |
Correct |
132 ms |
98984 KB |
Output is correct |
25 |
Correct |
154 ms |
114680 KB |
Output is correct |
26 |
Correct |
171 ms |
114948 KB |
Output is correct |
27 |
Correct |
134 ms |
106168 KB |
Output is correct |
28 |
Correct |
128 ms |
104200 KB |
Output is correct |
29 |
Correct |
212 ms |
124948 KB |
Output is correct |
30 |
Correct |
156 ms |
120024 KB |
Output is correct |
31 |
Correct |
221 ms |
124656 KB |
Output is correct |
32 |
Correct |
225 ms |
125096 KB |
Output is correct |
33 |
Correct |
222 ms |
123632 KB |
Output is correct |
34 |
Correct |
180 ms |
121252 KB |
Output is correct |
35 |
Correct |
200 ms |
117720 KB |
Output is correct |
36 |
Correct |
166 ms |
110072 KB |
Output is correct |
37 |
Correct |
171 ms |
110116 KB |
Output is correct |
38 |
Correct |
112 ms |
84180 KB |
Output is correct |
39 |
Correct |
112 ms |
83816 KB |
Output is correct |
40 |
Correct |
134 ms |
107096 KB |
Output is correct |
41 |
Correct |
206 ms |
119800 KB |
Output is correct |
42 |
Correct |
181 ms |
110588 KB |
Output is correct |
43 |
Correct |
95 ms |
94536 KB |
Output is correct |
44 |
Correct |
175 ms |
112596 KB |
Output is correct |
45 |
Correct |
254 ms |
126716 KB |
Output is correct |
46 |
Correct |
218 ms |
128996 KB |
Output is correct |
47 |
Correct |
201 ms |
129980 KB |
Output is correct |
48 |
Correct |
228 ms |
128932 KB |
Output is correct |
49 |
Correct |
218 ms |
127224 KB |
Output is correct |
50 |
Correct |
191 ms |
113240 KB |
Output is correct |
51 |
Correct |
212 ms |
114788 KB |
Output is correct |
52 |
Correct |
269 ms |
132080 KB |
Output is correct |
53 |
Correct |
283 ms |
133904 KB |
Output is correct |
54 |
Correct |
275 ms |
132728 KB |
Output is correct |
55 |
Correct |
296 ms |
130912 KB |
Output is correct |
56 |
Correct |
243 ms |
120916 KB |
Output is correct |
57 |
Correct |
216 ms |
123244 KB |
Output is correct |
58 |
Correct |
247 ms |
125880 KB |
Output is correct |
59 |
Correct |
219 ms |
123612 KB |
Output is correct |
60 |
Correct |
217 ms |
126068 KB |
Output is correct |
61 |
Correct |
200 ms |
123324 KB |
Output is correct |
62 |
Correct |
283 ms |
130024 KB |
Output is correct |
63 |
Correct |
170 ms |
100680 KB |
Output is correct |
64 |
Correct |
167 ms |
100396 KB |
Output is correct |
65 |
Correct |
164 ms |
104260 KB |
Output is correct |
66 |
Correct |
158 ms |
101928 KB |
Output is correct |
67 |
Correct |
157 ms |
104632 KB |
Output is correct |
68 |
Correct |
163 ms |
101164 KB |
Output is correct |
69 |
Correct |
198 ms |
123108 KB |
Output is correct |
70 |
Correct |
192 ms |
119104 KB |
Output is correct |
71 |
Correct |
195 ms |
119924 KB |
Output is correct |
72 |
Correct |
189 ms |
120440 KB |
Output is correct |