#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
const ll MAXN = 2e5+100;
struct BIT{
vector <ll> val;
ll n;
void init(ll N){
n=N;
val.resize(n+1);
}
void upd(ll i,ll v){
for (;i <= n;i += i &-i)val[i]+=v;
}
ll get(ll i){
ll res = 0;
for (;i > 0;i -= i &-i)res+=val[i];
return res;
}
ll query(ll l,ll r){
return get(r)-get(l-1);
}
};
ll n,m,L,C;
ll p[MAXN],dist[MAXN];
bool cycle[MAXN];
ll in[MAXN];
pll c[MAXN];
ll a[MAXN],b[MAXN];
ll out[MAXN],timeDFS;
ll cycle_id[MAXN];
ll sz[MAXN];
BIT s[MAXN];
BIT s2[MAXN];
ll cnt[MAXN];
ll pre[MAXN];
ll dist_root[MAXN];
ll cycle_length[MAXN];
vector <ll> all_md[MAXN];
ll cycle_root[MAXN];
ll ans[MAXN];
ll root[MAXN];
vector <ll> g[MAXN];
void dfs(ll u){
in[u] = ++timeDFS;
for (auto v:g[u]){
dist[v] += dist[u];
root[v] = root[u];
dfs(v);
}
out[u] = timeDFS;
}
struct query{
ll t,v,id;
};
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);
cin>>n>>m>>L>>C;
for (ll i = 1;i <= n;i ++)cin>>a[i];
for (ll i = 1;i <= m;i ++)cin>>b[i];
for (ll i = 1;i <= n;i ++){
ll r = a[i] - C;
ll md = (r % L + L) % L;
auto tmp = upper_bound(a+1,a+1+n,md)-a;
tmp--;
if (tmp==0)tmp=n;
p[i] = tmp;
dist[i] = C + (md - a[tmp] + L)%L;
// cout<<i<<' '<<p[i]<<' '<<dist[i]<<endl;
}
for (ll i = 1;i <= n;i ++){
if (!in[i]){
ll cur = i;
vector <ll> all;
while (!in[cur]){
all.push_back(cur);
in[cur] = 1;
cur = p[cur];
}
if (in[cur] == 1){
for (ll j = 0;j < sz(all);j ++){
if (all[j] == cur){
for (ll k = j;k < sz(all);k ++){
cycle[all[k]] = 1;
sz[cur]++;
cycle_root[all[k]] = cur;
dist_root[all[k]] = cycle_length[cur];
cycle_length[cur] += dist[all[k]];
cycle_id[all[k]] = k-j+1;
}
s[cur].init(sz[cur]);
for (ll k = 0;k < j;k ++)g[all[k+1]].push_back(all[k]);
break;
}
}
}
else{
all.push_back(cur);
for (ll j = 0;j + 1 < sz(all);j ++){
g[all[j+1]].push_back(all[j]);
}
}
for (auto x:all)in[x] = 2;
}
}
for (ll i = 1;i <= n;i ++){
if (cycle[i]){root[i] = i;dist[i] = 0;dfs(i);}
}
// cout<<"SUS "<<dist[n]<<endl;
for (ll i = 1,ptr = 1;i <= m;i ++){
while (ptr <= n && a[ptr] <= b[i])ptr++;
if (ptr==1)c[i].se = n;
else c[i].se = ptr-1;
c[i].fi = dist[c[i].se] + (b[i] - a[c[i].se] + L) % L;
// cout<<c[i].fi<<' '<<c[i].se<<'\n';
}
ll q;
cin>>q;
vector <query> a1,a2;
for (ll i = 1;i <= q;i ++){
ll t,v;
cin>>v>>t;
if (cycle[v]){
a1.push_back({t,v,i});
}
else{
a2.push_back({t,v,i});
}
}
sort(a2.begin(),a2.end(),[](query x,query y){return x.t+dist[x.v] < y.t + dist[y.v];});
sort(c+1,c+1+m);
{
BIT tmp;
tmp.init(n);
for (ll i = 0,ptr = 1;i < sz(a2);i ++){
// cout<<i<<endl;
while (ptr<=m && c[ptr].fi <= a2[i].t + dist[a2[i].v]){
tmp.upd(in[c[ptr].se],1);
ptr++;
}
ans[a2[i].id] = tmp.query(in[a2[i].v],out[a2[i].v]);
}
}
vector <pll> rem;
vector <pll> in_cycle;
for (ll i = 1;i <= m;i ++){
// cout<<i<<endl;
ll r = cycle_root[root[c[i].se]];
pll tmp = MP(c[i].fi + cycle_length[r] - dist_root[root[c[i].se]],root[c[i].se]);
// cout<<tmp.fi<<' '<<tmp.se<<'\n';
in_cycle.push_back(tmp);
all_md[r].push_back(tmp.fi % cycle_length[r]);
rem.push_back(MP(c[i].fi-dist_root[tmp.se],tmp.se));
}
// return 0;
for (ll i =1 ;i <= n;i ++){
if (i == cycle_root[i]){
sort(all_md[i].begin(),all_md[i].end());
all_md[i].resize(unique(all_md[i].begin(),all_md[i].end())-all_md[i].begin());
s2[i].init(sz(all_md[i]));
}
}
sort(rem.begin(),rem.end());
sort(in_cycle.begin(),in_cycle.end());
sort(a1.begin(),a1.end(),[](query x,query y){return x.t - dist_root[x.v] < y.t - dist_root[y.v];});
// for (ll i = 1;i <= n;i ++){
// cout<<i<<' '<<cycle[i]<<' '<<cycle_root[i]<<endl;
// }
for (ll i = 0,ptr = 0,ptr2 = 0;i < sz(a1);i ++){
// cout<<i<<endl;
while (ptr < sz(rem) && rem[ptr].fi <= a1[i].t - dist_root[a1[i].v]){
// cout<<rem[ptr].se<<' '<<ptr<<endl;
s[cycle_root[rem[ptr].se]].upd(cycle_id[rem[ptr].se],1);
ptr++;
}
while (ptr2 < sz(in_cycle) && in_cycle[ptr2].fi <= a1[i].t-dist_root[a1[i].v]){
ll r = cycle_root[in_cycle[ptr2].se];
cnt[r]++;
pre[r] -= in_cycle[ptr2].fi / cycle_length[r];
s2[r].upd(lower_bound(all_md[r].begin(),all_md[r].end(),in_cycle[ptr2].fi%cycle_length[r]) - all_md[r].begin() + 1, +1);
ptr2++;
}
ll r = cycle_root[a1[i].v];
// cout<<i<<' '<<a1[i].v<<' '<<r<<' '<<cycle_length[r]<<' '<<sz(s[r].val)<<' '<<pre[r]<<endl;
ll res = (a1[i].t-dist_root[a1[i].v]) / cycle_length[r] * cnt[r] + pre[r]
+s2[r].get(upper_bound(all_md[r].begin(),all_md[r].end(),(a1[i].t-dist_root[a1[i].v]) % cycle_length[r])-all_md[r].begin())
+s[r].get(cycle_id[a1[i].v]);
ans[a1[i].id] = res;
}
for (ll i = 1;i <= q;i ++)cout<<ans[i]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
41816 KB |
Output is correct |
2 |
Correct |
7 ms |
42076 KB |
Output is correct |
3 |
Correct |
6 ms |
42076 KB |
Output is correct |
4 |
Correct |
6 ms |
40028 KB |
Output is correct |
5 |
Correct |
6 ms |
40284 KB |
Output is correct |
6 |
Correct |
6 ms |
40284 KB |
Output is correct |
7 |
Correct |
6 ms |
40284 KB |
Output is correct |
8 |
Correct |
7 ms |
39864 KB |
Output is correct |
9 |
Correct |
6 ms |
39880 KB |
Output is correct |
10 |
Correct |
7 ms |
39952 KB |
Output is correct |
11 |
Correct |
6 ms |
39976 KB |
Output is correct |
12 |
Correct |
6 ms |
40028 KB |
Output is correct |
13 |
Correct |
7 ms |
40028 KB |
Output is correct |
14 |
Correct |
8 ms |
42164 KB |
Output is correct |
15 |
Correct |
7 ms |
40028 KB |
Output is correct |
16 |
Correct |
6 ms |
40044 KB |
Output is correct |
17 |
Correct |
6 ms |
40132 KB |
Output is correct |
18 |
Correct |
8 ms |
40028 KB |
Output is correct |
19 |
Correct |
6 ms |
40216 KB |
Output is correct |
20 |
Correct |
6 ms |
40024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
54964 KB |
Output is correct |
2 |
Correct |
106 ms |
62912 KB |
Output is correct |
3 |
Correct |
113 ms |
71732 KB |
Output is correct |
4 |
Correct |
119 ms |
74420 KB |
Output is correct |
5 |
Correct |
99 ms |
73672 KB |
Output is correct |
6 |
Correct |
104 ms |
74524 KB |
Output is correct |
7 |
Correct |
93 ms |
58812 KB |
Output is correct |
8 |
Correct |
88 ms |
60672 KB |
Output is correct |
9 |
Correct |
104 ms |
66024 KB |
Output is correct |
10 |
Correct |
82 ms |
65768 KB |
Output is correct |
11 |
Correct |
102 ms |
64672 KB |
Output is correct |
12 |
Correct |
108 ms |
64372 KB |
Output is correct |
13 |
Correct |
105 ms |
64528 KB |
Output is correct |
14 |
Correct |
83 ms |
65004 KB |
Output is correct |
15 |
Correct |
102 ms |
64952 KB |
Output is correct |
16 |
Correct |
94 ms |
65788 KB |
Output is correct |
17 |
Correct |
95 ms |
67636 KB |
Output is correct |
18 |
Correct |
68 ms |
54128 KB |
Output is correct |
19 |
Correct |
67 ms |
55756 KB |
Output is correct |
20 |
Correct |
91 ms |
61040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
41816 KB |
Output is correct |
2 |
Correct |
7 ms |
42076 KB |
Output is correct |
3 |
Correct |
6 ms |
42076 KB |
Output is correct |
4 |
Correct |
6 ms |
40028 KB |
Output is correct |
5 |
Correct |
6 ms |
40284 KB |
Output is correct |
6 |
Correct |
6 ms |
40284 KB |
Output is correct |
7 |
Correct |
6 ms |
40284 KB |
Output is correct |
8 |
Correct |
7 ms |
39864 KB |
Output is correct |
9 |
Correct |
6 ms |
39880 KB |
Output is correct |
10 |
Correct |
7 ms |
39952 KB |
Output is correct |
11 |
Correct |
6 ms |
39976 KB |
Output is correct |
12 |
Correct |
6 ms |
40028 KB |
Output is correct |
13 |
Correct |
7 ms |
40028 KB |
Output is correct |
14 |
Correct |
8 ms |
42164 KB |
Output is correct |
15 |
Correct |
7 ms |
40028 KB |
Output is correct |
16 |
Correct |
6 ms |
40044 KB |
Output is correct |
17 |
Correct |
6 ms |
40132 KB |
Output is correct |
18 |
Correct |
8 ms |
40028 KB |
Output is correct |
19 |
Correct |
6 ms |
40216 KB |
Output is correct |
20 |
Correct |
6 ms |
40024 KB |
Output is correct |
21 |
Correct |
69 ms |
54964 KB |
Output is correct |
22 |
Correct |
106 ms |
62912 KB |
Output is correct |
23 |
Correct |
113 ms |
71732 KB |
Output is correct |
24 |
Correct |
119 ms |
74420 KB |
Output is correct |
25 |
Correct |
99 ms |
73672 KB |
Output is correct |
26 |
Correct |
104 ms |
74524 KB |
Output is correct |
27 |
Correct |
93 ms |
58812 KB |
Output is correct |
28 |
Correct |
88 ms |
60672 KB |
Output is correct |
29 |
Correct |
104 ms |
66024 KB |
Output is correct |
30 |
Correct |
82 ms |
65768 KB |
Output is correct |
31 |
Correct |
102 ms |
64672 KB |
Output is correct |
32 |
Correct |
108 ms |
64372 KB |
Output is correct |
33 |
Correct |
105 ms |
64528 KB |
Output is correct |
34 |
Correct |
83 ms |
65004 KB |
Output is correct |
35 |
Correct |
102 ms |
64952 KB |
Output is correct |
36 |
Correct |
94 ms |
65788 KB |
Output is correct |
37 |
Correct |
95 ms |
67636 KB |
Output is correct |
38 |
Correct |
68 ms |
54128 KB |
Output is correct |
39 |
Correct |
67 ms |
55756 KB |
Output is correct |
40 |
Correct |
91 ms |
61040 KB |
Output is correct |
41 |
Correct |
148 ms |
77324 KB |
Output is correct |
42 |
Correct |
143 ms |
85172 KB |
Output is correct |
43 |
Correct |
87 ms |
70356 KB |
Output is correct |
44 |
Correct |
127 ms |
87728 KB |
Output is correct |
45 |
Correct |
118 ms |
86996 KB |
Output is correct |
46 |
Correct |
117 ms |
87364 KB |
Output is correct |
47 |
Correct |
119 ms |
89640 KB |
Output is correct |
48 |
Correct |
114 ms |
88464 KB |
Output is correct |
49 |
Correct |
111 ms |
91280 KB |
Output is correct |
50 |
Correct |
109 ms |
73148 KB |
Output is correct |
51 |
Correct |
113 ms |
72376 KB |
Output is correct |
52 |
Correct |
163 ms |
78320 KB |
Output is correct |
53 |
Correct |
163 ms |
80356 KB |
Output is correct |
54 |
Correct |
165 ms |
78192 KB |
Output is correct |
55 |
Correct |
166 ms |
79092 KB |
Output is correct |
56 |
Correct |
132 ms |
82592 KB |
Output is correct |
57 |
Correct |
115 ms |
82272 KB |
Output is correct |
58 |
Correct |
126 ms |
84968 KB |
Output is correct |
59 |
Correct |
115 ms |
84652 KB |
Output is correct |
60 |
Correct |
111 ms |
85408 KB |
Output is correct |
61 |
Correct |
116 ms |
86168 KB |
Output is correct |
62 |
Correct |
184 ms |
80876 KB |
Output is correct |
63 |
Correct |
101 ms |
70252 KB |
Output is correct |
64 |
Correct |
97 ms |
70120 KB |
Output is correct |
65 |
Correct |
99 ms |
68716 KB |
Output is correct |
66 |
Correct |
105 ms |
71412 KB |
Output is correct |
67 |
Correct |
97 ms |
69728 KB |
Output is correct |
68 |
Correct |
107 ms |
70808 KB |
Output is correct |
69 |
Correct |
153 ms |
78776 KB |
Output is correct |
70 |
Correct |
137 ms |
76212 KB |
Output is correct |
71 |
Correct |
147 ms |
78520 KB |
Output is correct |
72 |
Correct |
138 ms |
76732 KB |
Output is correct |