// That's How much we have in common
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
#define int ll
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const int inf = 2'000'000'000;
const ll Log = 30;
int n, m, C, L, Q;
int A[N], B[N];
int nx[N], w[N], fs[N], df[N];
int V[N], T[N], ans[N];
vector<int> GI[N], TR[N], QR[N];
int slv[N];
int mk[N], incyc[N], st[N], fn[N], Tm = 1;
ll dep[N], deptr[N], al[N];
vector<int> vis, cyc;
void DFS(int u, ll d = 0){
slv[u] = 1;
st[u] = Tm ++;
dep[u] = d;
vis.pb(u);
for(auto adj : GI[u]){
if(!slv[adj])
DFS(adj, d + w[adj]);
}
fn[u] = Tm;
}
int fen[N];
void Add(int idx, int d){
for(; idx < N; idx += (idx & -idx))
fen[idx] += d;
}
int Get(int idx){
int res = 0;
for(; idx; idx -= (idx & -idx))
res += fen[idx];
return res;
}
vector<int> UN;
int ID(ll x){
return upper_bound(all(UN), x) - UN.begin();
}
void Solve(int rt){
//debug(rt);
vis.clear();
DFS(rt, 0);
cyc.clear();
int nw = rt;
do {
cyc.pb(nw);
incyc[nw] = 1;
nw = nx[nw];
} while(nw != rt);
ll CL = 0;
for(auto x : cyc) CL += w[x];
for(int i = 1; i < (int) cyc.size(); i++) al[cyc[i]] = al[cyc[i - 1]] + w[cyc[i - 1]];
//debug(CL);
vector<pll> E;
for(auto nd : vis){
for(auto ap : TR[nd]){
deptr[ap] = dep[nd] + df[ap];
E.pb({deptr[ap], -st[nd]});
}
if(nd != rt){
for(auto qr : QR[nd])
E.pb({T[qr] + dep[nd], qr});
}
}
sort(all(E));
for(auto ev : E){
if(ev.S < 0){
Add(-ev.S, 1);
} else {
ans[ev.S] += Get(fn[V[ev.S]] - 1) - Get(st[V[ev.S]] - 1);
}
}
// Clear
for(auto ev : E)
if(ev.S < 0)
Add(-ev.S, -1);
/////////////////////////////
E.clear();
UN.clear();
for(auto nd : vis){
for(auto ap : TR[nd]){
deptr[ap] = dep[nd] + df[ap];
E.pb({deptr[ap], -1});
UN.pb(deptr[ap] % CL);
}
if(incyc[nd]){
for(auto qr : QR[nd])
E.pb({T[qr] - al[nd], qr});
}
}
sort(all(UN)); UN.resize(unique(all(UN)) - UN.begin()); //UN.pb(CL);
assert(UN.size() < N);
sort(all(E));
ll Sum = 0, On;
for(auto ev : E){
if(ev.S < 0){
Sum += ev.F / CL;
Add(ID(ev.F % CL), 1);
} else {
On = Get(UN.size());
if(On == 0) continue;
ans[ev.S] += (On * (ev.F / CL)) - Sum;
ans[ev.S] += Get(ID(ev.F % CL));
}
}
for(auto ev : E){
if(ev.S < 0){
Add(ID(ev.F % CL), -1);
}
}
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> L >> C;
for(int i = 0; i < n; i++) cin >> A[i];
A[n] = inf;
for(int i = 0; i < m; i++) cin >> B[i];
cin >> Q;
for(int i = 0; i < Q; i++){ cin >> V[i] >> T[i]; V[i] --; }
for(int i = 0; i < Q; i++) QR[V[i]].pb(i);
for(int i = 0; i < n; i++){
int wh = (((A[i] - C) % L) + L) % L;
if(wh < A[0]) nx[i] = n - 1;
else
nx[i] = (upper_bound(A, A + n, wh) - A) - 1;
w[i] = C + ((wh - A[nx[i]] + L) % L);
GI[nx[i]].pb(i);
}
for(int i = 0; i < m; i++){
int wh = B[i];
if(wh < A[0]) fs[i] = n - 1;
else
fs[i] = (upper_bound(A, A + n, wh) - A) - 1;
df[i] = (wh - A[fs[i]] + L) % L;
TR[fs[i]].pb(i);
}
/*
cerr << "! ";
for(int i = 0; i < m; i++) cerr << df[i] << ' ';
cerr << '\n';
*/
for(int i = 0; i < n; i++){
if(slv[i]) continue;
int nw = i;
mk[nw] = 1;
int src = -1;
while(src == -1){
nw = nx[nw];
if(mk[nw]) src = nw;
mk[nw] = 1;
}
Solve(src);
}
for(int i = 0; i < Q; i++) cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14848 KB |
Output is correct |
2 |
Correct |
14 ms |
15232 KB |
Output is correct |
3 |
Correct |
14 ms |
15328 KB |
Output is correct |
4 |
Correct |
14 ms |
15488 KB |
Output is correct |
5 |
Correct |
14 ms |
15488 KB |
Output is correct |
6 |
Correct |
15 ms |
15616 KB |
Output is correct |
7 |
Correct |
15 ms |
15616 KB |
Output is correct |
8 |
Correct |
18 ms |
15360 KB |
Output is correct |
9 |
Correct |
14 ms |
15288 KB |
Output is correct |
10 |
Correct |
16 ms |
15360 KB |
Output is correct |
11 |
Correct |
14 ms |
15264 KB |
Output is correct |
12 |
Correct |
15 ms |
15616 KB |
Output is correct |
13 |
Correct |
16 ms |
15616 KB |
Output is correct |
14 |
Correct |
15 ms |
15488 KB |
Output is correct |
15 |
Correct |
14 ms |
15488 KB |
Output is correct |
16 |
Correct |
14 ms |
15488 KB |
Output is correct |
17 |
Correct |
14 ms |
15488 KB |
Output is correct |
18 |
Correct |
14 ms |
15360 KB |
Output is correct |
19 |
Correct |
14 ms |
15392 KB |
Output is correct |
20 |
Correct |
14 ms |
15392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
24636 KB |
Output is correct |
2 |
Correct |
279 ms |
50560 KB |
Output is correct |
3 |
Correct |
254 ms |
52984 KB |
Output is correct |
4 |
Correct |
205 ms |
51748 KB |
Output is correct |
5 |
Correct |
267 ms |
65260 KB |
Output is correct |
6 |
Correct |
267 ms |
63596 KB |
Output is correct |
7 |
Correct |
238 ms |
49564 KB |
Output is correct |
8 |
Correct |
236 ms |
49504 KB |
Output is correct |
9 |
Correct |
313 ms |
69852 KB |
Output is correct |
10 |
Correct |
235 ms |
65604 KB |
Output is correct |
11 |
Correct |
414 ms |
69840 KB |
Output is correct |
12 |
Correct |
400 ms |
69812 KB |
Output is correct |
13 |
Correct |
424 ms |
69856 KB |
Output is correct |
14 |
Correct |
306 ms |
65464 KB |
Output is correct |
15 |
Correct |
306 ms |
64040 KB |
Output is correct |
16 |
Correct |
255 ms |
59488 KB |
Output is correct |
17 |
Correct |
253 ms |
58108 KB |
Output is correct |
18 |
Correct |
164 ms |
36632 KB |
Output is correct |
19 |
Correct |
177 ms |
36356 KB |
Output is correct |
20 |
Correct |
207 ms |
58240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14848 KB |
Output is correct |
2 |
Correct |
14 ms |
15232 KB |
Output is correct |
3 |
Correct |
14 ms |
15328 KB |
Output is correct |
4 |
Correct |
14 ms |
15488 KB |
Output is correct |
5 |
Correct |
14 ms |
15488 KB |
Output is correct |
6 |
Correct |
15 ms |
15616 KB |
Output is correct |
7 |
Correct |
15 ms |
15616 KB |
Output is correct |
8 |
Correct |
18 ms |
15360 KB |
Output is correct |
9 |
Correct |
14 ms |
15288 KB |
Output is correct |
10 |
Correct |
16 ms |
15360 KB |
Output is correct |
11 |
Correct |
14 ms |
15264 KB |
Output is correct |
12 |
Correct |
15 ms |
15616 KB |
Output is correct |
13 |
Correct |
16 ms |
15616 KB |
Output is correct |
14 |
Correct |
15 ms |
15488 KB |
Output is correct |
15 |
Correct |
14 ms |
15488 KB |
Output is correct |
16 |
Correct |
14 ms |
15488 KB |
Output is correct |
17 |
Correct |
14 ms |
15488 KB |
Output is correct |
18 |
Correct |
14 ms |
15360 KB |
Output is correct |
19 |
Correct |
14 ms |
15392 KB |
Output is correct |
20 |
Correct |
14 ms |
15392 KB |
Output is correct |
21 |
Correct |
136 ms |
24636 KB |
Output is correct |
22 |
Correct |
279 ms |
50560 KB |
Output is correct |
23 |
Correct |
254 ms |
52984 KB |
Output is correct |
24 |
Correct |
205 ms |
51748 KB |
Output is correct |
25 |
Correct |
267 ms |
65260 KB |
Output is correct |
26 |
Correct |
267 ms |
63596 KB |
Output is correct |
27 |
Correct |
238 ms |
49564 KB |
Output is correct |
28 |
Correct |
236 ms |
49504 KB |
Output is correct |
29 |
Correct |
313 ms |
69852 KB |
Output is correct |
30 |
Correct |
235 ms |
65604 KB |
Output is correct |
31 |
Correct |
414 ms |
69840 KB |
Output is correct |
32 |
Correct |
400 ms |
69812 KB |
Output is correct |
33 |
Correct |
424 ms |
69856 KB |
Output is correct |
34 |
Correct |
306 ms |
65464 KB |
Output is correct |
35 |
Correct |
306 ms |
64040 KB |
Output is correct |
36 |
Correct |
255 ms |
59488 KB |
Output is correct |
37 |
Correct |
253 ms |
58108 KB |
Output is correct |
38 |
Correct |
164 ms |
36632 KB |
Output is correct |
39 |
Correct |
177 ms |
36356 KB |
Output is correct |
40 |
Correct |
207 ms |
58240 KB |
Output is correct |
41 |
Correct |
381 ms |
73108 KB |
Output is correct |
42 |
Correct |
312 ms |
61660 KB |
Output is correct |
43 |
Correct |
168 ms |
48800 KB |
Output is correct |
44 |
Correct |
302 ms |
67652 KB |
Output is correct |
45 |
Correct |
328 ms |
81488 KB |
Output is correct |
46 |
Correct |
330 ms |
82004 KB |
Output is correct |
47 |
Correct |
345 ms |
82384 KB |
Output is correct |
48 |
Correct |
324 ms |
81272 KB |
Output is correct |
49 |
Correct |
324 ms |
81448 KB |
Output is correct |
50 |
Correct |
347 ms |
66636 KB |
Output is correct |
51 |
Correct |
328 ms |
65744 KB |
Output is correct |
52 |
Correct |
428 ms |
86988 KB |
Output is correct |
53 |
Correct |
458 ms |
86220 KB |
Output is correct |
54 |
Correct |
432 ms |
86860 KB |
Output is correct |
55 |
Correct |
502 ms |
86912 KB |
Output is correct |
56 |
Correct |
381 ms |
76752 KB |
Output is correct |
57 |
Correct |
378 ms |
76752 KB |
Output is correct |
58 |
Correct |
367 ms |
78416 KB |
Output is correct |
59 |
Correct |
344 ms |
75876 KB |
Output is correct |
60 |
Correct |
350 ms |
76276 KB |
Output is correct |
61 |
Correct |
373 ms |
76632 KB |
Output is correct |
62 |
Correct |
442 ms |
82336 KB |
Output is correct |
63 |
Correct |
245 ms |
52692 KB |
Output is correct |
64 |
Correct |
243 ms |
52820 KB |
Output is correct |
65 |
Correct |
260 ms |
53204 KB |
Output is correct |
66 |
Correct |
253 ms |
52572 KB |
Output is correct |
67 |
Correct |
247 ms |
52932 KB |
Output is correct |
68 |
Correct |
269 ms |
52180 KB |
Output is correct |
69 |
Correct |
352 ms |
76788 KB |
Output is correct |
70 |
Correct |
314 ms |
73332 KB |
Output is correct |
71 |
Correct |
336 ms |
74336 KB |
Output is correct |
72 |
Correct |
337 ms |
74324 KB |
Output is correct |