#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ar array
const int mxN=2e5;
int n, m, a[mxN], b[mxN], nxt[mxN], f[mxN], q;
ll l, c, ans[mxN], l2, lz[mxN], s1[mxN];
vector<ar<ll, 2>> d[mxN];
bool vis[mxN], ic[mxN];
vector<int> cyc, adj[mxN];
oset<pair<ll, int>> *le[mxN];
vector<ll> lf[mxN], vx;
void dfs(int u) {
vis[u]=1;
for(int v : adj[u]) {
if(ic[v])
continue;
dfs(v);
lz[v]+=(a[v]-c%l+l-a[u]+l)%l+c;
if(le[v]->size()>le[u]->size()) {
swap(le[v], le[u]);
swap(lz[v], lz[u]);
}
for(pair<ll, int> li : *le[v])
le[u]->insert(make_pair(li.first+lz[v]-lz[u], li.second));
}
if(!ic[u])
for(ar<ll, 2> di : d[u])
ans[di[1]]=le[u]->order_of_key(make_pair(di[0]+1-lz[u], 0));
}
ll fd(ll a, ll b) {
return a/b-(a%b<0);
}
ll fm(ll a, ll b) {
ll c=a%b;
return c<0?c+b:c;
}
int f1[mxN+1], ni;
ll f2[mxN+1];
oset<pair<ll, int>> f3[mxN+1];
void initds() {
sort(vx.begin(), vx.end());
memset(f1+1, 0, 4*vx.size());
memset(f2+1, 0, 8*vx.size());
for(int i=1; i<=vx.size(); ++i)
f3[i].clear();
}
void ins(ll x) {
int p=lower_bound(vx.begin(), vx.end(), x)-vx.begin();
for(++p; p<=vx.size(); p+=p&-p) {
++f1[p];
f2[p]+=fd(x, l2);
f3[p].insert(make_pair(fm(x, l2), ni++));
}
}
ll qry(ll x) {
int p=upper_bound(vx.begin(), vx.end(), x)-vx.begin();
ll r=0;
for(; p; p-=p&-p) {
r+=fd(x, l2)*f1[p];
r-=f2[p];
r+=f3[p].order_of_key(make_pair(fm(x, l2)+1, 0));
}
return r;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> l >> c;
for(int i=0; i<n; ++i)
cin >> a[i];
for(int i=0; i<m; ++i)
cin >> b[i];
for(int i=0; i<n; ++i) {
nxt[i]=upper_bound(a, a+n, a[i]-c%l)-a-1;
if(nxt[i]<0)
nxt[i]=upper_bound(a, a+n, a[i]-c%l+l)-a-1;
adj[nxt[i]].push_back(i);
le[i]=new oset<pair<ll, int>>();
}
for(int i=0; i<m; ++i) {
f[i]=b[i]<a[0]?n-1:lower_bound(a, a+n, b[i])-a-1;
le[f[i]]->insert(make_pair((b[i]-a[f[i]]+l)%l, i));
}
cin >> q;
for(int i=0, v; i<q; ++i) {
ll t;
cin >> v >> t, --v;
d[v].push_back({t, i});
}
for(int i=0; i<n; ++i) {
if(vis[i])
continue;
int u=i;
while(!vis[u]) {
vis[u]=1;
u=nxt[u];
}
l2=0;
while(!ic[u]) {
ic[u]=1;
cyc.push_back(u);
l2+=(a[u]-c%l+l-a[nxt[u]]+l)%l+c;
u=nxt[u];
}
ll s1i=0;
for(int ci : cyc) {
dfs(ci);
s1[ci]=s1i;
for(pair<ll, int> li : *le[ci])
lf[ci].push_back(li.first+lz[ci]);
s1i+=(a[ci]-c%l+l-a[nxt[ci]]+l)%l+c;
for(ll li : lf[ci])
vx.push_back(li-s1[ci]);
}
initds();
for(int ci : cyc) {
for(ll li : lf[ci])
ins(li-s1[ci]);
for(ar<ll, 2> di : d[ci])
ans[di[1]]+=qry(di[0]-s1[ci]);
}
vx.clear();
reverse(cyc.begin(), cyc.end());
s1i=-(a[cyc[0]]-c%l+l-a[nxt[cyc[0]]]+l)%l+c;
for(int ci : cyc) {
s1i+=(a[ci]-c%l+l-a[nxt[ci]]+l)%l+c;
s1[ci]=s1i;
for(ll li : lf[ci])
vx.push_back(li+s1[ci]);
}
initds();
for(int ci : cyc) {
for(ar<ll, 2> di : d[ci])
ans[di[1]]+=qry(di[0]-l2+s1[ci]);
for(ll li : lf[ci])
ins(li+s1[ci]);
}
vx.clear();
cyc.clear();
}
for(int i=0; i<q; ++i)
cout << ans[i] << "\n";
}
Compilation message
harvest.cpp: In function 'void initds()':
harvest.cpp:57:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i=1; i<=vx.size(); ++i)
| ~^~~~~~~~~~~
harvest.cpp: In function 'void ins(long long int)':
harvest.cpp:63:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(++p; p<=vx.size(); p+=p&-p) {
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
33528 KB |
Output is correct |
2 |
Correct |
39 ms |
34304 KB |
Output is correct |
3 |
Correct |
52 ms |
35448 KB |
Output is correct |
4 |
Correct |
45 ms |
35840 KB |
Output is correct |
5 |
Correct |
42 ms |
35964 KB |
Output is correct |
6 |
Correct |
42 ms |
35968 KB |
Output is correct |
7 |
Correct |
45 ms |
35960 KB |
Output is correct |
8 |
Correct |
43 ms |
35576 KB |
Output is correct |
9 |
Correct |
41 ms |
35584 KB |
Output is correct |
10 |
Correct |
45 ms |
35576 KB |
Output is correct |
11 |
Correct |
42 ms |
35452 KB |
Output is correct |
12 |
Correct |
39 ms |
35516 KB |
Output is correct |
13 |
Correct |
40 ms |
35576 KB |
Output is correct |
14 |
Correct |
39 ms |
34936 KB |
Output is correct |
15 |
Correct |
42 ms |
35832 KB |
Output is correct |
16 |
Correct |
43 ms |
35840 KB |
Output is correct |
17 |
Correct |
43 ms |
35968 KB |
Output is correct |
18 |
Correct |
43 ms |
35832 KB |
Output is correct |
19 |
Correct |
49 ms |
35704 KB |
Output is correct |
20 |
Correct |
42 ms |
35704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
47076 KB |
Output is correct |
2 |
Correct |
271 ms |
81784 KB |
Output is correct |
3 |
Correct |
256 ms |
81016 KB |
Output is correct |
4 |
Correct |
287 ms |
82792 KB |
Output is correct |
5 |
Correct |
275 ms |
104692 KB |
Output is correct |
6 |
Correct |
288 ms |
104824 KB |
Output is correct |
7 |
Correct |
248 ms |
76688 KB |
Output is correct |
8 |
Correct |
234 ms |
76788 KB |
Output is correct |
9 |
Correct |
398 ms |
85512 KB |
Output is correct |
10 |
Correct |
366 ms |
83040 KB |
Output is correct |
11 |
Correct |
477 ms |
84332 KB |
Output is correct |
12 |
Correct |
447 ms |
84460 KB |
Output is correct |
13 |
Correct |
489 ms |
84456 KB |
Output is correct |
14 |
Correct |
374 ms |
82016 KB |
Output is correct |
15 |
Correct |
363 ms |
84236 KB |
Output is correct |
16 |
Correct |
273 ms |
93688 KB |
Output is correct |
17 |
Correct |
278 ms |
93432 KB |
Output is correct |
18 |
Correct |
184 ms |
57848 KB |
Output is correct |
19 |
Correct |
192 ms |
57592 KB |
Output is correct |
20 |
Correct |
238 ms |
81912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
33528 KB |
Output is correct |
2 |
Correct |
39 ms |
34304 KB |
Output is correct |
3 |
Correct |
52 ms |
35448 KB |
Output is correct |
4 |
Correct |
45 ms |
35840 KB |
Output is correct |
5 |
Correct |
42 ms |
35964 KB |
Output is correct |
6 |
Correct |
42 ms |
35968 KB |
Output is correct |
7 |
Correct |
45 ms |
35960 KB |
Output is correct |
8 |
Correct |
43 ms |
35576 KB |
Output is correct |
9 |
Correct |
41 ms |
35584 KB |
Output is correct |
10 |
Correct |
45 ms |
35576 KB |
Output is correct |
11 |
Correct |
42 ms |
35452 KB |
Output is correct |
12 |
Correct |
39 ms |
35516 KB |
Output is correct |
13 |
Correct |
40 ms |
35576 KB |
Output is correct |
14 |
Correct |
39 ms |
34936 KB |
Output is correct |
15 |
Correct |
42 ms |
35832 KB |
Output is correct |
16 |
Correct |
43 ms |
35840 KB |
Output is correct |
17 |
Correct |
43 ms |
35968 KB |
Output is correct |
18 |
Correct |
43 ms |
35832 KB |
Output is correct |
19 |
Correct |
49 ms |
35704 KB |
Output is correct |
20 |
Correct |
42 ms |
35704 KB |
Output is correct |
21 |
Correct |
212 ms |
47076 KB |
Output is correct |
22 |
Correct |
271 ms |
81784 KB |
Output is correct |
23 |
Correct |
256 ms |
81016 KB |
Output is correct |
24 |
Correct |
287 ms |
82792 KB |
Output is correct |
25 |
Correct |
275 ms |
104692 KB |
Output is correct |
26 |
Correct |
288 ms |
104824 KB |
Output is correct |
27 |
Correct |
248 ms |
76688 KB |
Output is correct |
28 |
Correct |
234 ms |
76788 KB |
Output is correct |
29 |
Correct |
398 ms |
85512 KB |
Output is correct |
30 |
Correct |
366 ms |
83040 KB |
Output is correct |
31 |
Correct |
477 ms |
84332 KB |
Output is correct |
32 |
Correct |
447 ms |
84460 KB |
Output is correct |
33 |
Correct |
489 ms |
84456 KB |
Output is correct |
34 |
Correct |
374 ms |
82016 KB |
Output is correct |
35 |
Correct |
363 ms |
84236 KB |
Output is correct |
36 |
Correct |
273 ms |
93688 KB |
Output is correct |
37 |
Correct |
278 ms |
93432 KB |
Output is correct |
38 |
Correct |
184 ms |
57848 KB |
Output is correct |
39 |
Correct |
192 ms |
57592 KB |
Output is correct |
40 |
Correct |
238 ms |
81912 KB |
Output is correct |
41 |
Correct |
1986 ms |
249912 KB |
Output is correct |
42 |
Correct |
349 ms |
97404 KB |
Output is correct |
43 |
Correct |
390 ms |
79580 KB |
Output is correct |
44 |
Correct |
3510 ms |
215572 KB |
Output is correct |
45 |
Correct |
1664 ms |
250408 KB |
Output is correct |
46 |
Correct |
1734 ms |
251124 KB |
Output is correct |
47 |
Correct |
1742 ms |
252608 KB |
Output is correct |
48 |
Correct |
1836 ms |
252032 KB |
Output is correct |
49 |
Correct |
1743 ms |
252144 KB |
Output is correct |
50 |
Correct |
1756 ms |
223776 KB |
Output is correct |
51 |
Correct |
1817 ms |
222808 KB |
Output is correct |
52 |
Correct |
2920 ms |
221096 KB |
Output is correct |
53 |
Correct |
2067 ms |
222368 KB |
Output is correct |
54 |
Correct |
3066 ms |
221072 KB |
Output is correct |
55 |
Correct |
1886 ms |
220252 KB |
Output is correct |
56 |
Correct |
1720 ms |
245540 KB |
Output is correct |
57 |
Correct |
1679 ms |
246628 KB |
Output is correct |
58 |
Correct |
1735 ms |
248252 KB |
Output is correct |
59 |
Correct |
1700 ms |
247264 KB |
Output is correct |
60 |
Correct |
1654 ms |
247088 KB |
Output is correct |
61 |
Correct |
1700 ms |
247140 KB |
Output is correct |
62 |
Correct |
2149 ms |
159720 KB |
Output is correct |
63 |
Correct |
1692 ms |
203904 KB |
Output is correct |
64 |
Correct |
1635 ms |
204068 KB |
Output is correct |
65 |
Correct |
1759 ms |
204644 KB |
Output is correct |
66 |
Correct |
1906 ms |
202532 KB |
Output is correct |
67 |
Correct |
1845 ms |
203276 KB |
Output is correct |
68 |
Correct |
1810 ms |
201744 KB |
Output is correct |
69 |
Correct |
1861 ms |
248680 KB |
Output is correct |
70 |
Correct |
1788 ms |
243180 KB |
Output is correct |
71 |
Correct |
1843 ms |
250844 KB |
Output is correct |
72 |
Correct |
1839 ms |
253636 KB |
Output is correct |