1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include<bits/stdc++.h> using namespace std; using ll=int64_t; #define pb push_back #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) ll rd(){ char c;ll s=0; while(c=getchar(),c<48); do s=(s<<1)+(s<<3)+(c^'0'); while(c=getchar(),c>47); return s; } enum{N=200010}; int n,m,q,nxt[N],L,C,C2,A[N*2],B[N],id[N],incir[N]; vector <int> E[N],G[N]; ll H[N],dis[N],H2[N],len[N],ans[N]; void dfs(int u){ for(int i:E[u]) H[++C]=dis[u]+i; vector <int> tmp; for(int v:G[u]) { if(id[v]==id[u]) continue; tmp.pb(v),id[v]=id[u]; dis[v]+=dis[u],dfs(v); } G[u]=tmp; }
struct Node{ ll d; int id; }; vector <Node> Q[N],que; vector <ll> upd; struct BIT{ int s[N],n; void Init(int m){ n=m,memset(s,0,(n+1)<<2); } void Add(int p,int x) { while(p<=n) s[p]+=x,p+=p&-p; } int Que(int p) { int res=0; while(p) res+=s[p],p-=p&-p; return res; } } T,X;
void dfs2(int u){ for(Node &i:Q[u]) { if(incir[u]) { ll d=i.d-(len[id[u]]-dis[u]); if(d>=0) que.pb((Node){d,i.id}); } ans[i.id]-=T.Que(i.d=upper_bound(H+1,H+C+1,dis[u]+i.d)-H-1); } for(int i:E[u]) T.Add(lower_bound(H+1,H+C+1,dis[u]+i)-H,1),upd.pb(dis[u]+i); for(int v:G[u]) dfs2(v); for(Node i:Q[u]) ans[i.id]+=T.Que(i.d); }
int main(){ n=rd(),m=rd(),L=rd(),C=rd(); rep(i,0,n-1) A[i]=rd(),A[i+n]=A[i]+L; rep(i,0,m-1) B[i]=rd(); int C_=C%L,p=0; rep(i,n,n*2-1) { while(p<i && A[i]-A[p+1]>=C_) p++; nxt[i-n]=p%n,G[p%n].pb(i-n); dis[i-n]=C-C_+A[i]-A[p]; } p=0; rep(i,0,m-1) { while(p<n*2-1 && B[i]+L>=A[p+1]) p++; E[p%n].pb(B[i]+L-A[p]); } C=0; rep(i,0,n-1) id[i]=-2; rep(i,0,n-1) if(id[i]==-2) { int u=i; for(;~id[u];u=nxt[u]) id[u]=-1; id[u]=u,len[u]=dis[u],incir[u]=1; for(int v=nxt[u];v!=u;v=nxt[v]) len[u]+=dis[v],incir[v]=1; dfs(u); } sort(H+1,H+C+1),T.Init(C=unique(H+1,H+C+1)-H-1); rep(i,1,q=rd()) { int u=rd()-1; ll d=rd(); Q[u].pb((Node){d,i}); } rep(i,0,n-1) if(id[i]==i) { que.clear(),upd.clear(); dfs2(i); sort(upd.begin(),upd.end()),sort(que.begin(),que.end(),[&](Node x,Node y){ return x.d<y.d; }); C2=0; for(ll x:upd) H2[++C2]=x%len[i]; sort(H2+1,H2+C2+1),X.Init(C2=unique(H2+1,H2+C2+1)-H2-1); auto it=upd.begin(); ll s=0,c=0; for(Node &q:que) { while(it!=upd.end() && *it<=q.d) X.Add(lower_bound(H2+1,H2+C2+1,*it%len[i])-H2,1),s+=*(it++)/len[i],c++; ans[q.id]+=q.d/len[i]*c-s; ans[q.id]+=X.Que(upper_bound(H2+1,H2+C2+1,q.d%len[i])-H2-1); } } rep(i,1,q) printf("%lld\n",ans[i]); }
|