#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[500001],adjrev[500001];
ll vis[500001];
ll dist[500001];
ll cnt;
vector<vector<pair<ll,ll>>> compart;
vector<vector<ll>> pref;
vector<ll> len;
ll num[500001];
ll insub[500001];
bool inloop[500001];
ll root;
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){
vis[u]=1;
num[u]=cnt;
if(u>n){
insub[u]=1;
compart[cnt].push_back({d%len[cnt],d/len[cnt]});
}
for(auto [v,w]:adjrev[u]){
if(v==root) continue;
//cout<<u<<' '<<v<<' '<<root<<ntr;
dfs1(v,d+w,u);
insub[u]+=insub[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;
//if(i==j) continue;
ll v=((a[i]-a[pos])%l+l)%l;
ll cost=(c-v+l-1)/l*l+v;
// cout<<i<<' '<<pos<<' '<<cost<<ntr;
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});
// cout<<i+n<<' '<<v<<' '<<cost<<ntr;
}
compart.push_back({});
pref.push_back({});
len.push_back(0);
for(int i=1;i<=m;i++){
if(vis[i+n]) continue;
root=0,dfs(i+n);
cnt++;
compart.push_back({});
pref.push_back({});
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());
pref[cnt].push_back(compart[cnt][0].second);
for(int i=1;i<compart[cnt].size();i++){
pref[cnt].push_back(pref[cnt].back()+compart[cnt][i].second);
}
}
// for(int i=1;i<=n+m;i++){
// cout<<insub[i]<<' ';
// }
// cout<<ntr;
cin>>q;
for(int i=1;i<=q;i++){
ll u,t;
cin>>u>>t;
t-=dist[u];
if(!inloop[u]){
cout<<insub[u]<<ntr;
continue;
}
else{
ll ans=insub[u]*(dist[u]>0);
ll p=t/len[num[u]];
ll q=t%len[num[u]];
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+=(compart[num[u]].size())*p-pref[num[u]].back();
ans+=pos+1;
cout<<ans<<ntr;
}
}
}
Compilation message
harvest.cpp: In function 'int main()':
harvest.cpp:95:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=1;i<compart[cnt].size();i++){
| ~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33360 KB |
Output is correct |
2 |
Correct |
7 ms |
33872 KB |
Output is correct |
3 |
Correct |
7 ms |
33616 KB |
Output is correct |
4 |
Incorrect |
7 ms |
31924 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
36936 KB |
Output is correct |
2 |
Correct |
107 ms |
54628 KB |
Output is correct |
3 |
Correct |
86 ms |
52040 KB |
Output is correct |
4 |
Correct |
118 ms |
59464 KB |
Output is correct |
5 |
Correct |
123 ms |
70728 KB |
Output is correct |
6 |
Correct |
112 ms |
70936 KB |
Output is correct |
7 |
Correct |
80 ms |
49344 KB |
Output is correct |
8 |
Correct |
85 ms |
47032 KB |
Output is correct |
9 |
Correct |
136 ms |
79804 KB |
Output is correct |
10 |
Correct |
120 ms |
72776 KB |
Output is correct |
11 |
Correct |
185 ms |
71752 KB |
Output is correct |
12 |
Correct |
200 ms |
78672 KB |
Output is correct |
13 |
Correct |
197 ms |
78664 KB |
Output is correct |
14 |
Correct |
148 ms |
71752 KB |
Output is correct |
15 |
Correct |
120 ms |
63356 KB |
Output is correct |
16 |
Correct |
104 ms |
67912 KB |
Output is correct |
17 |
Correct |
104 ms |
67664 KB |
Output is correct |
18 |
Correct |
60 ms |
45504 KB |
Output is correct |
19 |
Correct |
59 ms |
45704 KB |
Output is correct |
20 |
Correct |
95 ms |
57388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33360 KB |
Output is correct |
2 |
Correct |
7 ms |
33872 KB |
Output is correct |
3 |
Correct |
7 ms |
33616 KB |
Output is correct |
4 |
Incorrect |
7 ms |
31924 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |