/*
5 3 20 6
0 4 8 12 16
2 11 14
9
4 1932
2 93787
1 89
5 98124798
1 2684
1 137598
3 2
3 8375
4 237
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = int64_t;
using ull = uint64_t;
template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios_base::sync_with_stdio(false);
int N, M; ll L, C;
cin >> N >> M >> L >> C;
vector<ll> A(N);
for (int i = 0; i < N; ++i) cin >> A[i], A[i] = L - 1 - A[i]; // for convenience
reverse(A.begin(), A.end());
vector<int> nxt(N);
vector<ll> dist(N);
{
for (int v = 0; v < N; ++v) {
ll newA = (A[v] + C) % L;
int z = lower_bound(A.begin(), A.end(), newA) - A.begin();
if (z == N) {
nxt[v] = 0;
dist[v] = C + L + A[0] - newA;
} else {
nxt[v] = z;
dist[v] = C + A[z] - newA;
}
}
}
vector<vector<ll>> adds(N);
while (M--) {
ll B;
cin >> B;
B = L - 1 - B;
int z = lower_bound(A.begin(), A.end(), B) - A.begin();
if (z == N) {
adds[0].emplace_back(L + A[0] - B);
} else {
adds[z].emplace_back(A[z] - B);
}
}
int Q;
cin >> Q;
vector<vector<pair<ll, int>>> qs(N);
vector<ll> ans(Q);
for (int i = 0; i < Q; ++i) {
int v; ll T;
cin >> v >> T;
v = N - v;
qs[v].emplace_back(T, i);
}
vector<bool> isRoot(N);
vector<int> visited(N, 0);
for (int i = 0; i < N; ++i) {
int v = i;
while (visited[v] == 0) {
visited[v] = 1;
v = nxt[v];
}
if (visited[v] == 1) isRoot[v] = true;
v = i;
while (visited[v] == 1) {
visited[v] = 2;
v = nxt[v];
}
}
vector<vector<int>> adj(N);
for (int v = 0; v < N; ++v) {
if (!isRoot[v]) {
adj[nxt[v]].emplace_back(v);
}
}
vector<ll> distToRoot(N);
vector<ordered_set<pair<ll, ull>>> vals(N);
for (int i = 0; i < N; ++i) {
if (!isRoot[i]) continue;
function<void(int)> dfs1 = [&](int v) {
for (int u : adj[v]) {
distToRoot[u] = distToRoot[v] + dist[u];
dfs1(u);
}
};
dfs1(i);
function<void(int)> dfs2 = [&](int v) {
for (auto z : adds[v]) {
vals[v].insert({distToRoot[v] + z, rng()});
}
for (int u : adj[v]) {
dfs2(u);
if (int(vals[v].size()) < int(vals[u].size())) {
vals[v].swap(vals[u]);
}
for (auto z : vals[u]) {
vals[v].insert(z);
}
vals[u] = {};
}
for (auto q : qs[v]) {
ans[q.second] += vals[v].order_of_key({q.first + distToRoot[v], -1});
}
};
dfs2(i);
vector<pair<ll, int>> cur_qs;
ll cycleLen = distToRoot[nxt[i]] + dist[i];
int v = i;
while (true) {
for (auto q : qs[v]) {
cur_qs.emplace_back(q.first - (cycleLen - distToRoot[v]), q.second); // must pass through removed edge
}
v = nxt[v];
if (v == i) break;
}
for (auto z : vals[i]) cur_qs.emplace_back(z.first, -1);
sort(cur_qs.begin(), cur_qs.end());
ordered_set<pair<ll, ull>> st;
ll subt = 0;
for (auto q : cur_qs) {
if (q.second == -1) {
st.insert({q.first % cycleLen, rng()});
subt += (q.first / cycleLen);
} else {
ans[q.second] += (ll(q.first / cycleLen) * ll(st.size()) + st.order_of_key({q.first % cycleLen, -1})) - subt;
}
}
}
for (int i = 0; i < Q; ++i) cout << ans[i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
640 KB |
Output is correct |
2 |
Correct |
9 ms |
1280 KB |
Output is correct |
3 |
Correct |
10 ms |
1536 KB |
Output is correct |
4 |
Correct |
11 ms |
1664 KB |
Output is correct |
5 |
Correct |
10 ms |
2304 KB |
Output is correct |
6 |
Correct |
10 ms |
2304 KB |
Output is correct |
7 |
Correct |
12 ms |
2304 KB |
Output is correct |
8 |
Correct |
10 ms |
1536 KB |
Output is correct |
9 |
Correct |
10 ms |
1568 KB |
Output is correct |
10 |
Correct |
10 ms |
1536 KB |
Output is correct |
11 |
Correct |
10 ms |
1536 KB |
Output is correct |
12 |
Correct |
11 ms |
2304 KB |
Output is correct |
13 |
Correct |
12 ms |
2304 KB |
Output is correct |
14 |
Correct |
11 ms |
1864 KB |
Output is correct |
15 |
Correct |
11 ms |
1920 KB |
Output is correct |
16 |
Correct |
11 ms |
1920 KB |
Output is correct |
17 |
Correct |
11 ms |
1920 KB |
Output is correct |
18 |
Correct |
10 ms |
1920 KB |
Output is correct |
19 |
Correct |
10 ms |
1920 KB |
Output is correct |
20 |
Correct |
10 ms |
1920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
160 ms |
11396 KB |
Output is correct |
2 |
Correct |
291 ms |
52836 KB |
Output is correct |
3 |
Correct |
250 ms |
54128 KB |
Output is correct |
4 |
Correct |
254 ms |
57604 KB |
Output is correct |
5 |
Correct |
290 ms |
104848 KB |
Output is correct |
6 |
Correct |
295 ms |
104880 KB |
Output is correct |
7 |
Correct |
234 ms |
55260 KB |
Output is correct |
8 |
Correct |
233 ms |
55024 KB |
Output is correct |
9 |
Correct |
347 ms |
107988 KB |
Output is correct |
10 |
Correct |
286 ms |
107524 KB |
Output is correct |
11 |
Correct |
523 ms |
108012 KB |
Output is correct |
12 |
Correct |
442 ms |
107880 KB |
Output is correct |
13 |
Correct |
521 ms |
107964 KB |
Output is correct |
14 |
Correct |
423 ms |
107652 KB |
Output is correct |
15 |
Correct |
348 ms |
85424 KB |
Output is correct |
16 |
Correct |
282 ms |
82820 KB |
Output is correct |
17 |
Correct |
273 ms |
82556 KB |
Output is correct |
18 |
Correct |
152 ms |
29312 KB |
Output is correct |
19 |
Correct |
162 ms |
29308 KB |
Output is correct |
20 |
Correct |
247 ms |
58692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
640 KB |
Output is correct |
2 |
Correct |
9 ms |
1280 KB |
Output is correct |
3 |
Correct |
10 ms |
1536 KB |
Output is correct |
4 |
Correct |
11 ms |
1664 KB |
Output is correct |
5 |
Correct |
10 ms |
2304 KB |
Output is correct |
6 |
Correct |
10 ms |
2304 KB |
Output is correct |
7 |
Correct |
12 ms |
2304 KB |
Output is correct |
8 |
Correct |
10 ms |
1536 KB |
Output is correct |
9 |
Correct |
10 ms |
1568 KB |
Output is correct |
10 |
Correct |
10 ms |
1536 KB |
Output is correct |
11 |
Correct |
10 ms |
1536 KB |
Output is correct |
12 |
Correct |
11 ms |
2304 KB |
Output is correct |
13 |
Correct |
12 ms |
2304 KB |
Output is correct |
14 |
Correct |
11 ms |
1864 KB |
Output is correct |
15 |
Correct |
11 ms |
1920 KB |
Output is correct |
16 |
Correct |
11 ms |
1920 KB |
Output is correct |
17 |
Correct |
11 ms |
1920 KB |
Output is correct |
18 |
Correct |
10 ms |
1920 KB |
Output is correct |
19 |
Correct |
10 ms |
1920 KB |
Output is correct |
20 |
Correct |
10 ms |
1920 KB |
Output is correct |
21 |
Correct |
160 ms |
11396 KB |
Output is correct |
22 |
Correct |
291 ms |
52836 KB |
Output is correct |
23 |
Correct |
250 ms |
54128 KB |
Output is correct |
24 |
Correct |
254 ms |
57604 KB |
Output is correct |
25 |
Correct |
290 ms |
104848 KB |
Output is correct |
26 |
Correct |
295 ms |
104880 KB |
Output is correct |
27 |
Correct |
234 ms |
55260 KB |
Output is correct |
28 |
Correct |
233 ms |
55024 KB |
Output is correct |
29 |
Correct |
347 ms |
107988 KB |
Output is correct |
30 |
Correct |
286 ms |
107524 KB |
Output is correct |
31 |
Correct |
523 ms |
108012 KB |
Output is correct |
32 |
Correct |
442 ms |
107880 KB |
Output is correct |
33 |
Correct |
521 ms |
107964 KB |
Output is correct |
34 |
Correct |
423 ms |
107652 KB |
Output is correct |
35 |
Correct |
348 ms |
85424 KB |
Output is correct |
36 |
Correct |
282 ms |
82820 KB |
Output is correct |
37 |
Correct |
273 ms |
82556 KB |
Output is correct |
38 |
Correct |
152 ms |
29312 KB |
Output is correct |
39 |
Correct |
162 ms |
29308 KB |
Output is correct |
40 |
Correct |
247 ms |
58692 KB |
Output is correct |
41 |
Correct |
674 ms |
89236 KB |
Output is correct |
42 |
Correct |
325 ms |
69112 KB |
Output is correct |
43 |
Correct |
223 ms |
55960 KB |
Output is correct |
44 |
Correct |
624 ms |
84436 KB |
Output is correct |
45 |
Correct |
479 ms |
135288 KB |
Output is correct |
46 |
Correct |
489 ms |
136164 KB |
Output is correct |
47 |
Correct |
498 ms |
136928 KB |
Output is correct |
48 |
Correct |
514 ms |
134880 KB |
Output is correct |
49 |
Correct |
536 ms |
135152 KB |
Output is correct |
50 |
Correct |
539 ms |
86700 KB |
Output is correct |
51 |
Correct |
498 ms |
86108 KB |
Output is correct |
52 |
Correct |
711 ms |
139092 KB |
Output is correct |
53 |
Correct |
772 ms |
138680 KB |
Output is correct |
54 |
Correct |
692 ms |
139092 KB |
Output is correct |
55 |
Correct |
818 ms |
139092 KB |
Output is correct |
56 |
Correct |
539 ms |
113376 KB |
Output is correct |
57 |
Correct |
520 ms |
114280 KB |
Output is correct |
58 |
Correct |
547 ms |
115036 KB |
Output is correct |
59 |
Correct |
549 ms |
112460 KB |
Output is correct |
60 |
Correct |
560 ms |
113072 KB |
Output is correct |
61 |
Correct |
530 ms |
113248 KB |
Output is correct |
62 |
Correct |
668 ms |
108960 KB |
Output is correct |
63 |
Correct |
347 ms |
58596 KB |
Output is correct |
64 |
Correct |
352 ms |
58728 KB |
Output is correct |
65 |
Correct |
373 ms |
58852 KB |
Output is correct |
66 |
Correct |
447 ms |
58880 KB |
Output is correct |
67 |
Correct |
463 ms |
58696 KB |
Output is correct |
68 |
Correct |
468 ms |
58092 KB |
Output is correct |
69 |
Correct |
695 ms |
91872 KB |
Output is correct |
70 |
Correct |
636 ms |
88564 KB |
Output is correct |
71 |
Correct |
609 ms |
89836 KB |
Output is correct |
72 |
Correct |
576 ms |
90372 KB |
Output is correct |