#include<iostream>
#include<deque>
#include<algorithm>
#include<vector>
#include<map>
#include<random>
#include<time.h>
#include<cassert>
#include<chrono>
#include<set>
#include<unordered_set>
#include<array>
using namespace std;
#define ull unsigned long long
#define pb push_back
#define ld long double
#define ll long long
#define all(a) a.begin(), a.end()
#define int long long
mt19937_64 rnd(51);
struct SegTree {
vector<ll> t;
SegTree(int n) {
t.resize(4 * n);
}
void update(int v, int l, int r, int pos, ll val) {
if (l == r) {
t[v] += val;
return;
}
int m = (l + r) / 2;
if (pos <= m) {
update(2 * v, l, m, pos, val);
} else {
update(2 * v + 1, m + 1, r, pos, val);
}
t[v] = t[v * 2] + t[v * 2 + 1];
}
ll get(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (tl == l && tr == r) {
return t[v];
}
int tm = (tl + tr) / 2;
return get(2 * v, tl, tm, l, min(r, tm)) + get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}
};
const int N = 2e5 + 10;
vector<int> mas[N];
vector<pair<int, int>> g[N];
bool on_cycle[N];
pair<int, int> go[N];
ll ans[N];
ll dep[N], sz[N];
vector<pair<ll, int>> query[N];
SegTree Tree(N);
vector<ll> dif;
void zhfs(int v) {
for (auto i : mas[v]) {
dif.pb(i + dep[v]);
}
sz[v] = mas[v].size();
for (auto [u, c] : g[v]) {
if (!on_cycle[u]) {
dep[u] = dep[v] + c;
zhfs(u);
sz[v] += sz[u];
}
}
}
vector<ll> st[N];
void dfs(int v) {
int best = -1;
for (auto [u, c] : g[v]) {
if (!on_cycle[u]) {
if (best == -1 || sz[u] > sz[best]) {
best = u;
}
}
}
for (auto [u, c] : g[v]) {
if (!on_cycle[u] && best != u) {
dfs(u);
for (auto i : st[u]) {
int j = lower_bound(all(dif), i) - dif.begin();
Tree.update(1, 0, N - 1, j, -1);
}
}
}
if (best != -1) {
dfs(best);
swap(st[v], st[best]);
}
for (auto i : mas[v]) {
int j = lower_bound(all(dif), i + dep[v]) - dif.begin();
Tree.update(1, 0, N - 1, j, 1);
st[v].pb(i + dep[v]);
}
for (auto [u, c] : g[v]) {
if (!on_cycle[u]) {
for (int i : st[u]) {
st[v].pb(i);
int j = lower_bound(all(dif), i) - dif.begin();
Tree.update(1, 0, N - 1, j, 1);
}
}
}
if (!on_cycle[v]) {
for (auto [t, i] : query[v]) {
int j = upper_bound(all(dif), t + dep[v]) - dif.begin() - 1;
ans[i] += Tree.get(1, 0, N - 1, 0, j);
}
}
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, l, c;
cin >> n >> m >> l >> c;
int left = (c / l) * l;
c %= l;
vector<int> a(n), b(m);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int v;
ll t;
cin >> v >> t;
v--;
query[v].pb({t, i});
}
int j = 0;
while (j < b.size() && b[j] < a[0]) {
mas[n - 1].pb(b[j] + l - a[n - 1]);
j++;
}
for (int i = 0; i < n; i++) {
while (j < b.size() && (i == n - 1 || a[i + 1] > b[j])) {
mas[i].pb(b[j] - a[i]);
j++;
}
}
assert(j == b.size());
for (int i = 0; i < n; i++) {
a.pb(a[i] + l);
}
j = 0;
for (int i = n; i < 2 * n; i++) {
while (j + 1 < 2 * n && a[i] - a[j + 1] >= c) {
j++;
}
go[i % n] = {j % n, a[i] - a[j] + left};
g[j % n].pb({i % n, a[i] - a[j] + left});
}
vector<bool> used(n);
vector<deque<int>> cycles;
for (int i = 0; i < n; i++) {
if (!used[i]) {
int j = i;
map<int, int> here;
deque<int> arr;
while (!used[j]) {
used[j] = 1;
here[j] = 1;
arr.pb(j);
j = go[j].first;
}
if (here[j]) {
while (arr.front() != j) {
arr.pop_front();
}
cycles.pb(arr);
for (int v : arr) {
on_cycle[v] = 1;
}
}
}
}
for (int i = 0; i < n; i++) {
if (on_cycle[i]) {
dif.clear();
zhfs(i);
sort(all(dif));
dif.erase(unique(all(dif)), dif.end());
dfs(i);
for (int val : st[i]) {
int j = lower_bound(all(dif), val) - dif.begin();
Tree.update(1, 0, N - 1, j, -1);
}
}
}
for (auto cycle : cycles) {
int sz = cycle.size();
vector<ll> pr(sz + 1);
for (int i = 0; i < sz; i++) {
int u = cycle[i], v = cycle[(i + 1) % sz];
pr[i + 1] = pr[i] + go[u].second;
}
vector<ll> u, rem;
map<ll, vector<pair<int, int>>> mp;
for (int i = 0; i < sz; i++) {
int v = cycle[i];
for (auto val : st[v]) {
u.pb(val - pr[i]);
rem.pb((val + pr.back() - pr[i]) % pr.back());
}
for (auto [t, ind] : query[v]) {
mp[t - pr[i]].pb({ind, i});
}
}
sort(all(u));
sort(all(rem));
rem.erase(unique(all(rem)), rem.end());
int sz2 = rem.size();
SegTree cnt2(sz2);
int ind = 0;
ll sum = 0;
for (auto [t, arr] : mp) {
while (ind < u.size() && u[ind] <= t) {
sum += (u[ind] + pr.back()) / pr.back();
int k = lower_bound(all(rem), (u[ind] + pr.back()) % pr.back()) - rem.begin();
assert(rem[k] == (u[ind] + pr.back()) % pr.back());
cnt2.update(1, 0, sz2 - 1, k, 1);
ind++;
}
for (auto [pos, i] : arr) {
int real_t = t + pr[i];
int pos1 = upper_bound(all(rem), (real_t % pr.back()) - pr[i]) - rem.begin() - 1;
int pos2 = lower_bound(all(rem), pr.back() - pr[i]) - rem.begin();
ans[pos] += ind * (real_t / pr.back()) - sum;
ans[pos] -= cnt2.get(1, 0, sz2 - 1, pos2, sz2 - 1);
ans[pos] += cnt2.get(1, 0, sz2 - 1, 0, pos1);
int pos3 = upper_bound(all(rem), real_t % pr.back() + pr.back() - pr[i]) - rem.begin() - 1;
if (0 <= pos2 && pos2 <= pos3 && pos2 < sz2) {
ans[pos] += cnt2.get(1, 0, sz2 - 1, pos2, pos3);
}
}
}
int sz1 = u.size();
SegTree tr(sz1);
for (int i = 0; i < sz; i++) {
int v = cycle[i];
for (auto val : st[v]) {
int j = lower_bound(all(u), val - pr[i]) - u.begin();
tr.update(1, 0, sz1 - 1, j, 1);
}
for (auto [t, ind] : query[v]) {
int k = upper_bound(all(u), t - pr[i]) - u.begin() - 1;
ans[ind] += tr.get(1, 0, sz1 - 1, 0, k);
}
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << endl;
}
return 0;
}
Compilation message
harvest.cpp: In function 'void zhfs(long long int)':
harvest.cpp:72:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for (auto [u, c] : g[v]) {
| ^
harvest.cpp: In function 'void dfs(long long int)':
harvest.cpp:85:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
85 | for (auto [u, c] : g[v]) {
| ^
harvest.cpp:92:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
92 | for (auto [u, c] : g[v]) {
| ^
harvest.cpp:110:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
110 | for (auto [u, c] : g[v]) {
| ^
harvest.cpp:120:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
120 | for (auto [t, i] : query[v]) {
| ^
harvest.cpp: In function 'int main()':
harvest.cpp:154:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | while (j < b.size() && b[j] < a[0]) {
| ~~^~~~~~~~~~
harvest.cpp:159:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | while (j < b.size() && (i == n - 1 || a[i + 1] > b[j])) {
| ~~^~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
from harvest.cpp:8:
harvest.cpp:164:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | assert(j == b.size());
| ~~^~~~~~~~~~~
harvest.cpp:217:25: warning: unused variable 'v' [-Wunused-variable]
217 | int u = cycle[i], v = cycle[(i + 1) % sz];
| ^
harvest.cpp:228:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
228 | for (auto [t, ind] : query[v]) {
| ^
harvest.cpp:239:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
239 | for (auto [t, arr] : mp) {
| ^
harvest.cpp:240:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
240 | while (ind < u.size() && u[ind] <= t) {
| ~~~~^~~~~~~~~~
harvest.cpp:247:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
247 | for (auto [pos, i] : arr) {
| ^
harvest.cpp:268:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
268 | for (auto [t, ind] : query[v]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
29792 KB |
Output is correct |
2 |
Correct |
14 ms |
31672 KB |
Output is correct |
3 |
Correct |
15 ms |
32184 KB |
Output is correct |
4 |
Correct |
14 ms |
30044 KB |
Output is correct |
5 |
Correct |
13 ms |
30608 KB |
Output is correct |
6 |
Correct |
16 ms |
30556 KB |
Output is correct |
7 |
Correct |
14 ms |
30736 KB |
Output is correct |
8 |
Correct |
15 ms |
30044 KB |
Output is correct |
9 |
Correct |
14 ms |
30044 KB |
Output is correct |
10 |
Correct |
14 ms |
30044 KB |
Output is correct |
11 |
Correct |
14 ms |
30128 KB |
Output is correct |
12 |
Correct |
15 ms |
30556 KB |
Output is correct |
13 |
Correct |
17 ms |
30556 KB |
Output is correct |
14 |
Correct |
17 ms |
30300 KB |
Output is correct |
15 |
Correct |
14 ms |
30300 KB |
Output is correct |
16 |
Correct |
16 ms |
30524 KB |
Output is correct |
17 |
Correct |
14 ms |
30300 KB |
Output is correct |
18 |
Correct |
13 ms |
30372 KB |
Output is correct |
19 |
Correct |
14 ms |
30300 KB |
Output is correct |
20 |
Correct |
13 ms |
30300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
502 ms |
58700 KB |
Output is correct |
2 |
Correct |
386 ms |
49276 KB |
Output is correct |
3 |
Correct |
465 ms |
186244 KB |
Output is correct |
4 |
Correct |
650 ms |
210316 KB |
Output is correct |
5 |
Correct |
395 ms |
98616 KB |
Output is correct |
6 |
Correct |
404 ms |
98440 KB |
Output is correct |
7 |
Correct |
354 ms |
51464 KB |
Output is correct |
8 |
Correct |
363 ms |
51640 KB |
Output is correct |
9 |
Correct |
685 ms |
80312 KB |
Output is correct |
10 |
Correct |
497 ms |
78756 KB |
Output is correct |
11 |
Correct |
812 ms |
79112 KB |
Output is correct |
12 |
Correct |
737 ms |
79232 KB |
Output is correct |
13 |
Correct |
800 ms |
79244 KB |
Output is correct |
14 |
Correct |
544 ms |
76964 KB |
Output is correct |
15 |
Correct |
571 ms |
67636 KB |
Output is correct |
16 |
Correct |
383 ms |
72504 KB |
Output is correct |
17 |
Correct |
425 ms |
72328 KB |
Output is correct |
18 |
Correct |
311 ms |
47640 KB |
Output is correct |
19 |
Correct |
305 ms |
47036 KB |
Output is correct |
20 |
Correct |
381 ms |
53304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
29792 KB |
Output is correct |
2 |
Correct |
14 ms |
31672 KB |
Output is correct |
3 |
Correct |
15 ms |
32184 KB |
Output is correct |
4 |
Correct |
14 ms |
30044 KB |
Output is correct |
5 |
Correct |
13 ms |
30608 KB |
Output is correct |
6 |
Correct |
16 ms |
30556 KB |
Output is correct |
7 |
Correct |
14 ms |
30736 KB |
Output is correct |
8 |
Correct |
15 ms |
30044 KB |
Output is correct |
9 |
Correct |
14 ms |
30044 KB |
Output is correct |
10 |
Correct |
14 ms |
30044 KB |
Output is correct |
11 |
Correct |
14 ms |
30128 KB |
Output is correct |
12 |
Correct |
15 ms |
30556 KB |
Output is correct |
13 |
Correct |
17 ms |
30556 KB |
Output is correct |
14 |
Correct |
17 ms |
30300 KB |
Output is correct |
15 |
Correct |
14 ms |
30300 KB |
Output is correct |
16 |
Correct |
16 ms |
30524 KB |
Output is correct |
17 |
Correct |
14 ms |
30300 KB |
Output is correct |
18 |
Correct |
13 ms |
30372 KB |
Output is correct |
19 |
Correct |
14 ms |
30300 KB |
Output is correct |
20 |
Correct |
13 ms |
30300 KB |
Output is correct |
21 |
Correct |
502 ms |
58700 KB |
Output is correct |
22 |
Correct |
386 ms |
49276 KB |
Output is correct |
23 |
Correct |
465 ms |
186244 KB |
Output is correct |
24 |
Correct |
650 ms |
210316 KB |
Output is correct |
25 |
Correct |
395 ms |
98616 KB |
Output is correct |
26 |
Correct |
404 ms |
98440 KB |
Output is correct |
27 |
Correct |
354 ms |
51464 KB |
Output is correct |
28 |
Correct |
363 ms |
51640 KB |
Output is correct |
29 |
Correct |
685 ms |
80312 KB |
Output is correct |
30 |
Correct |
497 ms |
78756 KB |
Output is correct |
31 |
Correct |
812 ms |
79112 KB |
Output is correct |
32 |
Correct |
737 ms |
79232 KB |
Output is correct |
33 |
Correct |
800 ms |
79244 KB |
Output is correct |
34 |
Correct |
544 ms |
76964 KB |
Output is correct |
35 |
Correct |
571 ms |
67636 KB |
Output is correct |
36 |
Correct |
383 ms |
72504 KB |
Output is correct |
37 |
Correct |
425 ms |
72328 KB |
Output is correct |
38 |
Correct |
311 ms |
47640 KB |
Output is correct |
39 |
Correct |
305 ms |
47036 KB |
Output is correct |
40 |
Correct |
381 ms |
53304 KB |
Output is correct |
41 |
Correct |
675 ms |
82408 KB |
Output is correct |
42 |
Correct |
645 ms |
195068 KB |
Output is correct |
43 |
Correct |
635 ms |
204880 KB |
Output is correct |
44 |
Correct |
791 ms |
227136 KB |
Output is correct |
45 |
Correct |
535 ms |
112056 KB |
Output is correct |
46 |
Correct |
529 ms |
118328 KB |
Output is correct |
47 |
Correct |
516 ms |
118888 KB |
Output is correct |
48 |
Correct |
492 ms |
118020 KB |
Output is correct |
49 |
Correct |
505 ms |
118300 KB |
Output is correct |
50 |
Correct |
539 ms |
84020 KB |
Output is correct |
51 |
Correct |
556 ms |
83848 KB |
Output is correct |
52 |
Correct |
834 ms |
106236 KB |
Output is correct |
53 |
Correct |
868 ms |
105124 KB |
Output is correct |
54 |
Correct |
817 ms |
106344 KB |
Output is correct |
55 |
Correct |
697 ms |
106180 KB |
Output is correct |
56 |
Correct |
621 ms |
99988 KB |
Output is correct |
57 |
Correct |
590 ms |
102172 KB |
Output is correct |
58 |
Correct |
593 ms |
101432 KB |
Output is correct |
59 |
Correct |
510 ms |
98868 KB |
Output is correct |
60 |
Correct |
530 ms |
99236 KB |
Output is correct |
61 |
Correct |
551 ms |
98616 KB |
Output is correct |
62 |
Correct |
753 ms |
87616 KB |
Output is correct |
63 |
Correct |
435 ms |
73152 KB |
Output is correct |
64 |
Correct |
450 ms |
73096 KB |
Output is correct |
65 |
Correct |
478 ms |
73924 KB |
Output is correct |
66 |
Correct |
428 ms |
73212 KB |
Output is correct |
67 |
Correct |
419 ms |
73636 KB |
Output is correct |
68 |
Correct |
422 ms |
72044 KB |
Output is correct |
69 |
Correct |
622 ms |
86404 KB |
Output is correct |
70 |
Correct |
618 ms |
83396 KB |
Output is correct |
71 |
Correct |
634 ms |
84532 KB |
Output is correct |
72 |
Correct |
641 ms |
86328 KB |
Output is correct |