// M
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template < typename T >
using ordered_set = tree < T , null_type , less < T > , rb_tree_tag , tree_order_statistics_node_update >;
typedef long long ll;
const int N = 200195;
int n, m, q, L, regenC, FNL, ts, A[N], B[N], M[N], Nxt[N];
ll T[N], RS[N], Lnxt[N], lz[N], D[N], F[N];
vector < int > Q[N], In[N], V[N];
vector < pair < ll , int > > VST[N];
ordered_set < pair < ll , int > > ST[N], TS[N];
void DFS(int v)
{
M[v] = 1;
for (int u : In[v])
{
if (!M[u])
DFS(u);
if (u == v)
continue;
lz[u] += Lnxt[u];
if (ST[v].size() < ST[u].size())
ST[v].swap(ST[u]), swap(lz[v], lz[u]);
for (auto a : ST[u])
ST[v].insert(make_pair(a.first + lz[u] - lz[v], a.second));
ST[u].clear(); lz[u] = 0;
}
for (auto a : VST[v])
ST[v].insert(make_pair(a.first - lz[v], a.second));
for (int i : Q[v])
RS[i] += ST[v].order_of_key({T[i] - lz[v], N});
}
inline void AddFen(int i, ll val)
{
for (i ++; i < FNL; i += i & - i)
F[i] += val;
}
inline ll GetFen(int i)
{
ll rt = 0;
for (i ++; i; i -= i & - i)
rt += F[i];
return rt;
}
inline void AddRem(int i, ll val)
{
for (i ++; i < FNL; i += i & - i)
TS[i].insert({val, ts ++});
}
inline ll GetRem(int i, ll le, ll ri)
{
ll rt = 0;
for (i ++; i; i -= i & - i)
rt += TS[i].order_of_key({ri, -1}) - TS[i].order_of_key({le, -1});
return rt;
}
int main()
{
scanf("%d%d%d%d", &n, &m, &L, ®enC);
for (int i = 1; i <= n; i ++)
scanf("%d", &A[i]);
for (int i = 1; i <= m; i ++)
scanf("%d", &B[i]);
scanf("%d", &q);
for (int i = 1; i <= q; i ++)
{
int v;
scanf("%d%lld", &v, &T[i]);
Q[v].push_back(i);
}
// Building function graph ...
for (int i = 1; i <= n; i ++)
{
ll c = regenC / L;
ll r = regenC % L;
Lnxt[i] += (ll)c * L;
if (A[1] <= A[i] - r)
Nxt[i] = upper_bound(A + 1, A + n + 1, A[i] - r) - A - 1, Lnxt[i] += A[i] - A[Nxt[i]];
else
Nxt[i] = upper_bound(A + 1, A + n + 1, A[i] - r + L) - A - 1, Lnxt[i] += L + A[i] - A[Nxt[i]];
}
for (int i = 1, ptr = 1; i <= m; i ++)
{
if (B[i] < A[1])
VST[n].push_back({B[i] + L - A[n], i});
else
{
while (ptr < n && A[ptr + 1] <= B[i])
ptr ++;
VST[ptr].push_back({B[i] - A[ptr], i});
}
}
for (int i = 1; i <= n; i ++)
In[Nxt[i]].push_back(i);
for (int i = 1; i <= n; i ++)
if (!M[i])
DFS(i);
for (int i = 1; i <= n; i ++)
if (ST[i].size()) // Head of a circle
{
vector < int > C = {i};
int nw = Nxt[i];
while (nw != i)
C.push_back(nw), nw = Nxt[nw];
int k = (int)C.size();
C.push_back(i);
vector < ll > vec;
for (auto a : ST[i])
vec.push_back(a.first + lz[i]);
sort(vec.begin(), vec.end()); // Redundant
FNL = max((int)vec.size(), k) + 10;
D[0] = 0;
for (int j = 1; j <= k; j ++)
D[j] = D[j - 1] + Lnxt[C[j - 1]];
ll CL = D[k];
for (int j = 0; j < (int)vec.size(); j ++)
{
AddFen(j, vec[j] / CL);
AddRem(j, vec[j] % CL);
int lb = lower_bound(D + 1, D + k + 1, CL - vec[j] % CL) - D;
assert(lb <= k);
V[lb].push_back(j);
}
for (int j = 1; j <= k; j ++)
{
for (int id : V[j])
AddFen(id, 1LL);
for (int id : Q[C[j]])
{
int lb = upper_bound(vec.begin(), vec.end(), T[id] - D[j]) - vec.begin();
RS[id] += (T[id] / CL) * (ll)lb;
RS[id] -= GetFen(lb - 1);
ll le = 0, ri = T[id] % CL;
le = (le - D[j] % CL + CL) % CL;
ri = (ri - D[j] % CL + CL) % CL;
ri ++;
if (le < ri)
RS[id] += GetRem(lb - 1, le, ri);
else
RS[id] += GetRem(lb - 1, 0, ri) + GetRem(lb - 1, le, CL);
}
}
for (int j = 0; j <= FNL; j ++)
{
ts = 0;
F[j] = D[j] = 0;
TS[j].clear();
V[j].clear();
}
}
for (int i = 1; i <= q; i ++)
printf("%lld\n", RS[i]);
return 0;
}
Compilation message
harvest.cpp: In function 'int main()':
harvest.cpp:63:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
63 | scanf("%d%d%d%d", &n, &m, &L, ®enC);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harvest.cpp:65:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~~~~
harvest.cpp:67:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
67 | scanf("%d", &B[i]);
| ~~~~~^~~~~~~~~~~~~
harvest.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
harvest.cpp:72:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
72 | scanf("%d%lld", &v, &T[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
56952 KB |
Output is correct |
2 |
Correct |
55 ms |
57596 KB |
Output is correct |
3 |
Correct |
65 ms |
58616 KB |
Output is correct |
4 |
Correct |
74 ms |
58628 KB |
Output is correct |
5 |
Correct |
70 ms |
58684 KB |
Output is correct |
6 |
Correct |
63 ms |
58616 KB |
Output is correct |
7 |
Correct |
67 ms |
58792 KB |
Output is correct |
8 |
Correct |
62 ms |
58616 KB |
Output is correct |
9 |
Correct |
72 ms |
58616 KB |
Output is correct |
10 |
Correct |
65 ms |
58616 KB |
Output is correct |
11 |
Correct |
66 ms |
58744 KB |
Output is correct |
12 |
Correct |
66 ms |
59128 KB |
Output is correct |
13 |
Correct |
68 ms |
59128 KB |
Output is correct |
14 |
Correct |
64 ms |
58360 KB |
Output is correct |
15 |
Correct |
71 ms |
58872 KB |
Output is correct |
16 |
Correct |
72 ms |
58616 KB |
Output is correct |
17 |
Correct |
70 ms |
58744 KB |
Output is correct |
18 |
Correct |
69 ms |
58872 KB |
Output is correct |
19 |
Correct |
70 ms |
58828 KB |
Output is correct |
20 |
Correct |
66 ms |
58616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
69744 KB |
Output is correct |
2 |
Correct |
322 ms |
82296 KB |
Output is correct |
3 |
Correct |
293 ms |
81708 KB |
Output is correct |
4 |
Correct |
343 ms |
82288 KB |
Output is correct |
5 |
Correct |
329 ms |
84960 KB |
Output is correct |
6 |
Correct |
342 ms |
84984 KB |
Output is correct |
7 |
Correct |
319 ms |
78952 KB |
Output is correct |
8 |
Correct |
285 ms |
78952 KB |
Output is correct |
9 |
Correct |
535 ms |
116188 KB |
Output is correct |
10 |
Correct |
455 ms |
112768 KB |
Output is correct |
11 |
Correct |
689 ms |
115276 KB |
Output is correct |
12 |
Correct |
643 ms |
115268 KB |
Output is correct |
13 |
Correct |
693 ms |
115360 KB |
Output is correct |
14 |
Correct |
523 ms |
111956 KB |
Output is correct |
15 |
Correct |
493 ms |
100912 KB |
Output is correct |
16 |
Correct |
329 ms |
92152 KB |
Output is correct |
17 |
Correct |
318 ms |
91804 KB |
Output is correct |
18 |
Correct |
235 ms |
72316 KB |
Output is correct |
19 |
Correct |
234 ms |
73160 KB |
Output is correct |
20 |
Correct |
296 ms |
83832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
56952 KB |
Output is correct |
2 |
Correct |
55 ms |
57596 KB |
Output is correct |
3 |
Correct |
65 ms |
58616 KB |
Output is correct |
4 |
Correct |
74 ms |
58628 KB |
Output is correct |
5 |
Correct |
70 ms |
58684 KB |
Output is correct |
6 |
Correct |
63 ms |
58616 KB |
Output is correct |
7 |
Correct |
67 ms |
58792 KB |
Output is correct |
8 |
Correct |
62 ms |
58616 KB |
Output is correct |
9 |
Correct |
72 ms |
58616 KB |
Output is correct |
10 |
Correct |
65 ms |
58616 KB |
Output is correct |
11 |
Correct |
66 ms |
58744 KB |
Output is correct |
12 |
Correct |
66 ms |
59128 KB |
Output is correct |
13 |
Correct |
68 ms |
59128 KB |
Output is correct |
14 |
Correct |
64 ms |
58360 KB |
Output is correct |
15 |
Correct |
71 ms |
58872 KB |
Output is correct |
16 |
Correct |
72 ms |
58616 KB |
Output is correct |
17 |
Correct |
70 ms |
58744 KB |
Output is correct |
18 |
Correct |
69 ms |
58872 KB |
Output is correct |
19 |
Correct |
70 ms |
58828 KB |
Output is correct |
20 |
Correct |
66 ms |
58616 KB |
Output is correct |
21 |
Correct |
275 ms |
69744 KB |
Output is correct |
22 |
Correct |
322 ms |
82296 KB |
Output is correct |
23 |
Correct |
293 ms |
81708 KB |
Output is correct |
24 |
Correct |
343 ms |
82288 KB |
Output is correct |
25 |
Correct |
329 ms |
84960 KB |
Output is correct |
26 |
Correct |
342 ms |
84984 KB |
Output is correct |
27 |
Correct |
319 ms |
78952 KB |
Output is correct |
28 |
Correct |
285 ms |
78952 KB |
Output is correct |
29 |
Correct |
535 ms |
116188 KB |
Output is correct |
30 |
Correct |
455 ms |
112768 KB |
Output is correct |
31 |
Correct |
689 ms |
115276 KB |
Output is correct |
32 |
Correct |
643 ms |
115268 KB |
Output is correct |
33 |
Correct |
693 ms |
115360 KB |
Output is correct |
34 |
Correct |
523 ms |
111956 KB |
Output is correct |
35 |
Correct |
493 ms |
100912 KB |
Output is correct |
36 |
Correct |
329 ms |
92152 KB |
Output is correct |
37 |
Correct |
318 ms |
91804 KB |
Output is correct |
38 |
Correct |
235 ms |
72316 KB |
Output is correct |
39 |
Correct |
234 ms |
73160 KB |
Output is correct |
40 |
Correct |
296 ms |
83832 KB |
Output is correct |
41 |
Correct |
1226 ms |
220896 KB |
Output is correct |
42 |
Correct |
412 ms |
98932 KB |
Output is correct |
43 |
Correct |
239 ms |
78964 KB |
Output is correct |
44 |
Correct |
1259 ms |
216316 KB |
Output is correct |
45 |
Correct |
1058 ms |
222312 KB |
Output is correct |
46 |
Correct |
1099 ms |
230124 KB |
Output is correct |
47 |
Correct |
1072 ms |
233144 KB |
Output is correct |
48 |
Correct |
1063 ms |
222436 KB |
Output is correct |
49 |
Correct |
1032 ms |
223468 KB |
Output is correct |
50 |
Correct |
1148 ms |
217688 KB |
Output is correct |
51 |
Correct |
1144 ms |
216900 KB |
Output is correct |
52 |
Correct |
1948 ms |
252280 KB |
Output is correct |
53 |
Correct |
1610 ms |
251832 KB |
Output is correct |
54 |
Correct |
2052 ms |
252100 KB |
Output is correct |
55 |
Correct |
1308 ms |
252088 KB |
Output is correct |
56 |
Correct |
1096 ms |
222516 KB |
Output is correct |
57 |
Correct |
1113 ms |
230436 KB |
Output is correct |
58 |
Correct |
1165 ms |
224124 KB |
Output is correct |
59 |
Correct |
1083 ms |
222052 KB |
Output is correct |
60 |
Correct |
1057 ms |
222764 KB |
Output is correct |
61 |
Correct |
1054 ms |
222948 KB |
Output is correct |
62 |
Correct |
1493 ms |
178288 KB |
Output is correct |
63 |
Correct |
960 ms |
211112 KB |
Output is correct |
64 |
Correct |
954 ms |
209964 KB |
Output is correct |
65 |
Correct |
986 ms |
209164 KB |
Output is correct |
66 |
Correct |
1049 ms |
211044 KB |
Output is correct |
67 |
Correct |
1016 ms |
208328 KB |
Output is correct |
68 |
Correct |
1014 ms |
209388 KB |
Output is correct |
69 |
Correct |
1129 ms |
224628 KB |
Output is correct |
70 |
Correct |
1082 ms |
221296 KB |
Output is correct |
71 |
Correct |
1118 ms |
222576 KB |
Output is correct |
72 |
Correct |
1192 ms |
222248 KB |
Output is correct |