#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
// #define int long long
#define int __int128_t
#define ll __int128_t
#define lll __int128_t
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
// using ll = long long;
// using lll = __int128_t;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> PBDS_TREE;
const int MXN = 200005;
int n, m, L, adf, a[MXN], b[MXN], q, qid[MXN];
ll qt[MXN];
int p[MXN], pt[MXN];
ll ans[MXN];
vector<pii> edge[MXN];
vector<int> qry[MXN];
bitset<MXN> vis;
PBDS_TREE tr[MXN];
// vector<int> tr[MXN];
ll lz[MXN];
istream &operator>>(istream &ss, __int128_t &x) {
long long y;
ss >> y;
x = y;
return ss;
}
ostream &operator<<(ostream &ss, __int128_t x) {
long long y = x;
ss << y;
return ss;
}
struct VSET {
bitset<MXN> b;
vector<int> v;
void init() {
b.reset();
v.clear();
}
void insert(int x) {
if (b[x]) return;
b[x] = true;
v.push_back(x);
}
void clear() {
for (auto &i : v) b[i] = false;
v.clear();
}
} sr;
struct BIT {
int n;
lll val[MXN];
void init(int _n) {
n = _n;
fill(val + 1, val + n + 1, 0);
}
void modify(int id, lll v) {
for (id++; id <= n; id += (id & -id)) val[id] += v;
}
lll query(int id) {
ll ans = 0;
for (; id > 0; id -= (id & -id)) ans += val[id];
return ans;
}
} B;
void PUT_TREE() {
FOR(i, 0, m) {
int pp = lower_bound(a, a + n, b[i]) - a - 1;
if (pp == -1) pp += n;
int x = (b[i] - a[pp] + L) % L;
debug(i, pp, x);
tr[pp].insert(x);
// tr[pp].push_back(x);
}
}
void GET_P() {
vector<pii> v;
FOR(i, 0, n) v.push_back(mp(a[i], i));
FOR(i, 0, n) v.push_back(mp(a[i] + L, i));
FOR(i, 0, n) {
auto pp = prev(lower_bound(v.begin(), v.end(), mp(a[i] + L - adf % L, MXN)));
p[i] = pp -> sc;
pt[i] = adf / L * L + (a[i] + L - pp -> fs);
debug(i, p[i], pt[i]);
edge[p[i]].push_back(mp(pt[i], i));
}
}
void CALC_CYC(vector<ll> &v, vector<pair<ll, int>> &vq, ll C) {
int n = v.size(), m = vq.size();
// int sum = 0;
vector<int> p(1, 0), pc(1, 0);
vector<pii> dist;
FOR(i, 0, n) {
p.push_back(p.back() + v[i]);
pc.push_back(pc.back() + C - v[i] % C);
// sum += C - (v[i] % C);
// sss += v[i];
dist.push_back(mp(v[i] % C, -i - 1));
}
FOR(i, 0, m) {
dist.push_back(mp(vq[i].fs % C, i));
}
sort(dist.begin(), dist.end());
B.init(n);
for (auto [t, id] : dist) {
if (id < 0) {
id = -id - 1;
// sum -= C;
B.modify(id, -C);
} else {
auto [Q, aid] = vq[id];
int s = upper_bound(v.begin(), v.end(), Q) - v.begin();
// ans[aid] += (n * Q - sss - (sum + n * t)) / C + n;
ans[aid] += (s * Q - p[s] - (pc[s] + B.query(s) + s * t)) / C + s;
}
}
}
namespace JELLY {
void MERGE(int rt, int id, ll w) {
if (tr[rt].size() >= tr[id].size()) {
for (auto i : tr[id]) tr[rt].insert(i + lz[id] + w - lz[rt]);
} else {
lz[id] += w;
for (auto i : tr[rt]) tr[id].insert(i + lz[rt] - lz[id]);
tr[rt].swap(tr[id]);
swap(lz[rt], lz[id]);
}
tr[id].clear();
}
void GET_SR() {
vector<int> ind(n), ban(n);
queue<int> q;
FOR(i, 0, n) ind[p[i]]++;
FOR(i, 0, n) if (ind[i] == 0) q.push(i);
while (q.size()) {
int id = q.front();
q.pop();
ban[id] = true;
if (--ind[p[id]] == 0) q.push(p[id]);
}
FOR(i, 0, n) {
if (ban[i]) continue;
sr.insert(i);
int now = i;
while (!ban[now]) {
ban[now] = true;
now = p[now];
}
}
}
void CALC_MERGE(int id, bool f = true) {
vis[id] = true;
for (auto &[w, i] : edge[id]) {
if (vis[i]) continue;
CALC_MERGE(i);
MERGE(id, i, w);
}
if (f) {
for (auto &i : qry[id]) {
ll Q = qt[i] - lz[id];
// for (auto j : tr[id]) {
// if (j <= Q) ans[i]++;
// }
int x = tr[id].order_of_key(Q + 1);
debug(i, x);
ans[i] += x;
}
}
}
void SOLVE(int rt) {
// debug(rt);
CALC_MERGE(rt, false);
vector<ll> v;
for (auto i : tr[rt]) v.push_back(i + lz[rt]);
vector<pair<ll, int>> vq;
vector<int> cyc;
cyc.push_back(rt);
while (p[cyc.back()] != rt) cyc.push_back(p[cyc.back()]);
int acc = 0;
FOR(i, 0, cyc.size()) {
debug(cyc[i]);
for (auto &j : qry[cyc[i]]) {
debug(j);
// vq.push_back(mp(qt[j] - acc, j));
if (qt[j] - acc >= 0) vq.push_back(mp(qt[j] - acc, j));
}
acc += pt[cyc[i]];
}
CALC_CYC(v, vq, acc);
}
}
void miku() {
cin >> n >> m >> L >> adf;
FOR(i, 0, n) cin >> a[i];
FOR(i, 0, m) cin >> b[i];
cin >> q;
FOR(i, 0, q) {
cin >> qid[i] >> qt[i];
qid[i]--;
qry[qid[i]].push_back(i);
}
PUT_TREE();
GET_P();
JELLY::GET_SR();
for (auto &i : sr.v) {
debug(i);
JELLY::SOLVE(i);
}
FOR(i, 0, q) {
assert(ans[i] <= LLONG_MAX);
cout << (long long) ans[i] << '\n';
}
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
Compilation message
harvest.cpp: In function 'void PUT_TREE()':
harvest.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 39
| ^~
harvest.cpp:94:9: note: in expansion of macro 'debug'
94 | debug(i, pp, x);
| ^~~~~
harvest.cpp: In function 'void GET_P()':
harvest.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 39
| ^~
harvest.cpp:108:9: note: in expansion of macro 'debug'
108 | debug(i, p[i], pt[i]);
| ^~~~~
harvest.cpp: In function 'void JELLY::CALC_MERGE(__int128, bool)':
harvest.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 39
| ^~
harvest.cpp:191:17: note: in expansion of macro 'debug'
191 | debug(i, x);
| ^~~~~
harvest.cpp: In function 'void JELLY::SOLVE(__int128)':
harvest.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 39
| ^~
harvest.cpp:207:13: note: in expansion of macro 'debug'
207 | debug(cyc[i]);
| ^~~~~
harvest.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 39
| ^~
harvest.cpp:209:17: note: in expansion of macro 'debug'
209 | debug(j);
| ^~~~~
harvest.cpp: In function 'void miku()':
harvest.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 39
| ^~
harvest.cpp:233:9: note: in expansion of macro 'debug'
233 | debug(i);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
49244 KB |
Output is correct |
2 |
Correct |
19 ms |
49440 KB |
Output is correct |
3 |
Correct |
21 ms |
50208 KB |
Output is correct |
4 |
Correct |
19 ms |
49952 KB |
Output is correct |
5 |
Correct |
19 ms |
50248 KB |
Output is correct |
6 |
Correct |
19 ms |
50208 KB |
Output is correct |
7 |
Correct |
19 ms |
50212 KB |
Output is correct |
8 |
Correct |
18 ms |
49952 KB |
Output is correct |
9 |
Correct |
19 ms |
49952 KB |
Output is correct |
10 |
Correct |
19 ms |
49952 KB |
Output is correct |
11 |
Correct |
18 ms |
49956 KB |
Output is correct |
12 |
Correct |
19 ms |
50272 KB |
Output is correct |
13 |
Correct |
20 ms |
50644 KB |
Output is correct |
14 |
Correct |
19 ms |
49968 KB |
Output is correct |
15 |
Correct |
20 ms |
50212 KB |
Output is correct |
16 |
Correct |
21 ms |
50212 KB |
Output is correct |
17 |
Correct |
23 ms |
50312 KB |
Output is correct |
18 |
Correct |
26 ms |
50016 KB |
Output is correct |
19 |
Correct |
21 ms |
49948 KB |
Output is correct |
20 |
Correct |
22 ms |
49980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
78708 KB |
Output is correct |
2 |
Correct |
198 ms |
84140 KB |
Output is correct |
3 |
Correct |
222 ms |
86500 KB |
Output is correct |
4 |
Correct |
184 ms |
108208 KB |
Output is correct |
5 |
Correct |
190 ms |
105660 KB |
Output is correct |
6 |
Correct |
193 ms |
105532 KB |
Output is correct |
7 |
Correct |
156 ms |
92776 KB |
Output is correct |
8 |
Correct |
151 ms |
94020 KB |
Output is correct |
9 |
Correct |
255 ms |
130992 KB |
Output is correct |
10 |
Correct |
182 ms |
124908 KB |
Output is correct |
11 |
Correct |
323 ms |
128556 KB |
Output is correct |
12 |
Correct |
317 ms |
127840 KB |
Output is correct |
13 |
Correct |
376 ms |
127404 KB |
Output is correct |
14 |
Correct |
249 ms |
125912 KB |
Output is correct |
15 |
Correct |
247 ms |
104760 KB |
Output is correct |
16 |
Correct |
181 ms |
94364 KB |
Output is correct |
17 |
Correct |
178 ms |
94092 KB |
Output is correct |
18 |
Correct |
117 ms |
72776 KB |
Output is correct |
19 |
Correct |
111 ms |
72384 KB |
Output is correct |
20 |
Correct |
156 ms |
87736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
49244 KB |
Output is correct |
2 |
Correct |
19 ms |
49440 KB |
Output is correct |
3 |
Correct |
21 ms |
50208 KB |
Output is correct |
4 |
Correct |
19 ms |
49952 KB |
Output is correct |
5 |
Correct |
19 ms |
50248 KB |
Output is correct |
6 |
Correct |
19 ms |
50208 KB |
Output is correct |
7 |
Correct |
19 ms |
50212 KB |
Output is correct |
8 |
Correct |
18 ms |
49952 KB |
Output is correct |
9 |
Correct |
19 ms |
49952 KB |
Output is correct |
10 |
Correct |
19 ms |
49952 KB |
Output is correct |
11 |
Correct |
18 ms |
49956 KB |
Output is correct |
12 |
Correct |
19 ms |
50272 KB |
Output is correct |
13 |
Correct |
20 ms |
50644 KB |
Output is correct |
14 |
Correct |
19 ms |
49968 KB |
Output is correct |
15 |
Correct |
20 ms |
50212 KB |
Output is correct |
16 |
Correct |
21 ms |
50212 KB |
Output is correct |
17 |
Correct |
23 ms |
50312 KB |
Output is correct |
18 |
Correct |
26 ms |
50016 KB |
Output is correct |
19 |
Correct |
21 ms |
49948 KB |
Output is correct |
20 |
Correct |
22 ms |
49980 KB |
Output is correct |
21 |
Correct |
115 ms |
78708 KB |
Output is correct |
22 |
Correct |
198 ms |
84140 KB |
Output is correct |
23 |
Correct |
222 ms |
86500 KB |
Output is correct |
24 |
Correct |
184 ms |
108208 KB |
Output is correct |
25 |
Correct |
190 ms |
105660 KB |
Output is correct |
26 |
Correct |
193 ms |
105532 KB |
Output is correct |
27 |
Correct |
156 ms |
92776 KB |
Output is correct |
28 |
Correct |
151 ms |
94020 KB |
Output is correct |
29 |
Correct |
255 ms |
130992 KB |
Output is correct |
30 |
Correct |
182 ms |
124908 KB |
Output is correct |
31 |
Correct |
323 ms |
128556 KB |
Output is correct |
32 |
Correct |
317 ms |
127840 KB |
Output is correct |
33 |
Correct |
376 ms |
127404 KB |
Output is correct |
34 |
Correct |
249 ms |
125912 KB |
Output is correct |
35 |
Correct |
247 ms |
104760 KB |
Output is correct |
36 |
Correct |
181 ms |
94364 KB |
Output is correct |
37 |
Correct |
178 ms |
94092 KB |
Output is correct |
38 |
Correct |
117 ms |
72776 KB |
Output is correct |
39 |
Correct |
111 ms |
72384 KB |
Output is correct |
40 |
Correct |
156 ms |
87736 KB |
Output is correct |
41 |
Correct |
409 ms |
132584 KB |
Output is correct |
42 |
Correct |
249 ms |
107768 KB |
Output is correct |
43 |
Correct |
158 ms |
105588 KB |
Output is correct |
44 |
Correct |
325 ms |
151712 KB |
Output is correct |
45 |
Correct |
297 ms |
151136 KB |
Output is correct |
46 |
Correct |
295 ms |
148264 KB |
Output is correct |
47 |
Correct |
304 ms |
154008 KB |
Output is correct |
48 |
Correct |
317 ms |
149108 KB |
Output is correct |
49 |
Correct |
315 ms |
148828 KB |
Output is correct |
50 |
Correct |
288 ms |
125924 KB |
Output is correct |
51 |
Correct |
294 ms |
120988 KB |
Output is correct |
52 |
Correct |
391 ms |
166352 KB |
Output is correct |
53 |
Correct |
369 ms |
172964 KB |
Output is correct |
54 |
Correct |
381 ms |
166064 KB |
Output is correct |
55 |
Correct |
441 ms |
152240 KB |
Output is correct |
56 |
Correct |
340 ms |
145560 KB |
Output is correct |
57 |
Correct |
339 ms |
149252 KB |
Output is correct |
58 |
Correct |
346 ms |
146124 KB |
Output is correct |
59 |
Correct |
340 ms |
148824 KB |
Output is correct |
60 |
Correct |
353 ms |
146240 KB |
Output is correct |
61 |
Correct |
339 ms |
146216 KB |
Output is correct |
62 |
Correct |
377 ms |
139016 KB |
Output is correct |
63 |
Correct |
218 ms |
118132 KB |
Output is correct |
64 |
Correct |
238 ms |
119964 KB |
Output is correct |
65 |
Correct |
239 ms |
117076 KB |
Output is correct |
66 |
Correct |
287 ms |
117520 KB |
Output is correct |
67 |
Correct |
276 ms |
119476 KB |
Output is correct |
68 |
Correct |
277 ms |
118900 KB |
Output is correct |
69 |
Correct |
373 ms |
133996 KB |
Output is correct |
70 |
Correct |
345 ms |
128628 KB |
Output is correct |
71 |
Correct |
346 ms |
129812 KB |
Output is correct |
72 |
Correct |
360 ms |
127396 KB |
Output is correct |