#include <algorithm>
#include <iostream>
#include <numeric>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i=0; i<n; ++i)
#define rrep(i, n) for (int i=1; i<=n; ++i)
#define all(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll=long long; using pp=pair<int,int>;
using vi=vector<int>; using vll=vector<ll>;
const int maxn = int(2e5) + 10;
int n;
struct MergeSortTree {
const static int M = 262144;
vll t[M<<1];
void add_point(int x, ll y) { for (x+=M; x; x/=2) t[x].pb(y); }
void init() {
for (int i=1; i<M; ++i) sort(all(t[M+i]));
for (int i=M-1; 1<=i; --i) {
auto &v = t[i], &vl = t[i*2], &vr = t[i*2+1];
if (vl.empty()) v = vr; else if (vr.empty()) v = vl;
else v.resize(vl.size()+vr.size()), merge(all(vl), all(vr), v.begin());
}
}
int rect(int l, int r, ll u) {
int ret = 0;
auto qv = [&](vll &v) { ret += (upper_bound(all(v), u)-v.begin()); };
for (l+=M, r+=M; l<=r; l/=2, r/=2) {
if (l%2==1) qv(t[l++]);
if (r%2==0) qv(t[r--]);
}
return ret;
}
int rect(int l, int r, ll d, ll u) {
int ret = 0;
auto qv = [&](vll &v) { ret += (upper_bound(all(v), u)-lower_bound(all(v), d)); };
for (l+=M, r+=M; l<=r; l/=2, r/=2) {
if (l%2==1) qv(t[l++]);
if (r%2==0) qv(t[r--]);
}
return ret;
}
};
int nxt[maxn], nxtd[maxn];
vi apples[maxn];
namespace Step1 {
int m, L, C;
int person[maxn], apple[maxn];
void In() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> L >> C;
rrep(i, n) cin >> person[i];
rrep(i, m) cin >> apple[i];
}
pp GetNxt(int pos) {
int i = int(upper_bound(person+1, person+n+1, pos)-person)-1;
if (i == 0) return {n, (L-person[n])+pos};
else return {i, pos-person[i]};
}
void BuildNxt() {
rrep(i, n) {
int t = person[i]-C%L; if (t < 0) t += L;
tie(nxt[i], nxtd[i]) = GetNxt(t);
nxtd[i] += C;
}
}
void AddApples() {
rrep(i, m) {
int j, d; tie(j, d) = GetNxt(apple[i]);
apples[j].emplace_back(d);
}
}
void Work() {
In();
BuildNxt(); AddApples();
}
}
int cn;
int myci[maxn];
int croots[maxn];
ll crdst[maxn];
ll clen[maxn];
vector<int> celem[maxn];
bool iscv[maxn];
int tin[maxn], tout[maxn];
namespace Step2 {
bool onstk[maxn];
void dfs1(int x) {
onstk[x] = true;
int y = nxt[x];
if (!onstk[y]) {
if (!myci[y]) dfs1(y);
myci[x] = myci[y];
crdst[x] = crdst[y] + nxtd[x];
onstk[x] = false;
return;
}
myci[x] = ++cn; croots[cn] = x; iscv[x] = true;
celem[cn].pb(x);
for (int y=nxt[x]; y!=x; y=nxt[y]) celem[cn].pb(y);
onstk[x] = false;
}
vector<int> child[maxn];
int nt;
void dfs2(int x) {
tin[x] = ++nt;
for (int y:child[x]) {
dfs2(y);
}
tout[x] = nt;
}
void BuildGraph() {
rrep(i, n) if (!myci[i]) dfs1(i);
rrep(i, n) if (celem[myci[i]][0] != i) child[nxt[i]].pb(i);
rrep(i, cn) { int r = croots[i];
for (int x:celem[i]) {
iscv[x] = true;
clen[i] += nxtd[x];
}
dfs2(r);
}
}
}
MergeSortTree tconst, tinf;
vector<ll> rdl[maxn], rdp[maxn];
int xoff[maxn], xoz;
ll cp[maxn];
namespace Step3 {
void Build() {
rrep(i, n) for (int ad:apples[i]) tconst.add_point(tin[i], crdst[i]+ad);
tconst.init();
rrep(v, n) for (int ad:apples[v]) rdl[myci[v]].pb(crdst[v]+ad);
rrep(ci, cn) {
sort(all(rdl[ci])), rdp[ci].resize(rdl[ci].size());
xoff[ci] = xoz;
xoz += rdl[ci].size();
}
rrep(v, n) for (int ad:apples[v]) {
int ci = myci[v];
auto &vl=rdl[ci], &vp=rdp[ci];
ll tt = crdst[v] + ad, cl = clen[ci];
int x = int(lower_bound(all(vl), tt)-vl.begin())+1;
ll tn = tt/cl, tr = tt%cl;
tn = -tn-1; tr = cl-tr;
if (tr == cl) ++tn, tr = 0;
tinf.add_point(xoff[ci]+x, tr);
vp[x-1] += tn;
}
rrep(ci, cn) partial_sum(all(rdp[ci]), rdp[ci].begin());
tinf.init();
rrep(ci, cn) {
ll pt = 0;
for (int x:celem[ci]) cp[x]=pt, pt+=nxtd[x];
cp[celem[ci][0]] = pt;
}
}
}
int CountConst(int v, ll T) {
return tconst.rect(tin[v], tout[v], crdst[v]+T);
}
ll CountInf(int v, ll T) {
int ci = myci[v];
ll p = cp[v], cl = clen[ci];
ll np = (T-p)/cl, rp = (T-p)%cl;
if (rp < 0) rp+=cl, --np;
auto &vl = rdl[ci], &vp = rdp[ci];
int xr = upper_bound(all(vl), T-p)-vl.begin();
if (!xr) return 0;
ll ret = xr * (1 + np) + (xr ? vp[xr-1] : 0);
ret += tinf.rect(xoff[ci]+1, xoff[ci]+xr,
cl-rp, cl);
return ret;
}
int main() {
Step1::Work();
Step2::BuildGraph();
Step3::Build();
int q; cin >> q;
for (;q--;) {
int v; ll T; cin >> v >> T;
ll ans = CountConst(v, T);
if (iscv[v]) ans += CountInf(v, T);
cout << ans;
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
48604 KB |
Output is correct |
2 |
Correct |
37 ms |
50276 KB |
Output is correct |
3 |
Correct |
37 ms |
50072 KB |
Output is correct |
4 |
Correct |
42 ms |
50068 KB |
Output is correct |
5 |
Correct |
38 ms |
50300 KB |
Output is correct |
6 |
Correct |
40 ms |
50328 KB |
Output is correct |
7 |
Correct |
37 ms |
50376 KB |
Output is correct |
8 |
Correct |
38 ms |
50064 KB |
Output is correct |
9 |
Correct |
36 ms |
50068 KB |
Output is correct |
10 |
Correct |
37 ms |
49988 KB |
Output is correct |
11 |
Correct |
36 ms |
50084 KB |
Output is correct |
12 |
Correct |
37 ms |
50352 KB |
Output is correct |
13 |
Correct |
40 ms |
50492 KB |
Output is correct |
14 |
Correct |
40 ms |
50284 KB |
Output is correct |
15 |
Correct |
39 ms |
50252 KB |
Output is correct |
16 |
Correct |
37 ms |
50284 KB |
Output is correct |
17 |
Correct |
38 ms |
50232 KB |
Output is correct |
18 |
Correct |
42 ms |
50120 KB |
Output is correct |
19 |
Correct |
38 ms |
50124 KB |
Output is correct |
20 |
Correct |
37 ms |
50116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
57216 KB |
Output is correct |
2 |
Correct |
253 ms |
69812 KB |
Output is correct |
3 |
Correct |
219 ms |
73728 KB |
Output is correct |
4 |
Correct |
287 ms |
75428 KB |
Output is correct |
5 |
Correct |
294 ms |
86392 KB |
Output is correct |
6 |
Correct |
269 ms |
85896 KB |
Output is correct |
7 |
Correct |
193 ms |
64772 KB |
Output is correct |
8 |
Correct |
198 ms |
64776 KB |
Output is correct |
9 |
Correct |
457 ms |
90912 KB |
Output is correct |
10 |
Correct |
316 ms |
90216 KB |
Output is correct |
11 |
Correct |
503 ms |
89736 KB |
Output is correct |
12 |
Correct |
483 ms |
89904 KB |
Output is correct |
13 |
Correct |
494 ms |
89692 KB |
Output is correct |
14 |
Correct |
351 ms |
89076 KB |
Output is correct |
15 |
Correct |
457 ms |
82924 KB |
Output is correct |
16 |
Correct |
285 ms |
75140 KB |
Output is correct |
17 |
Correct |
253 ms |
74480 KB |
Output is correct |
18 |
Correct |
170 ms |
60168 KB |
Output is correct |
19 |
Correct |
162 ms |
59208 KB |
Output is correct |
20 |
Correct |
208 ms |
68776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
48604 KB |
Output is correct |
2 |
Correct |
37 ms |
50276 KB |
Output is correct |
3 |
Correct |
37 ms |
50072 KB |
Output is correct |
4 |
Correct |
42 ms |
50068 KB |
Output is correct |
5 |
Correct |
38 ms |
50300 KB |
Output is correct |
6 |
Correct |
40 ms |
50328 KB |
Output is correct |
7 |
Correct |
37 ms |
50376 KB |
Output is correct |
8 |
Correct |
38 ms |
50064 KB |
Output is correct |
9 |
Correct |
36 ms |
50068 KB |
Output is correct |
10 |
Correct |
37 ms |
49988 KB |
Output is correct |
11 |
Correct |
36 ms |
50084 KB |
Output is correct |
12 |
Correct |
37 ms |
50352 KB |
Output is correct |
13 |
Correct |
40 ms |
50492 KB |
Output is correct |
14 |
Correct |
40 ms |
50284 KB |
Output is correct |
15 |
Correct |
39 ms |
50252 KB |
Output is correct |
16 |
Correct |
37 ms |
50284 KB |
Output is correct |
17 |
Correct |
38 ms |
50232 KB |
Output is correct |
18 |
Correct |
42 ms |
50120 KB |
Output is correct |
19 |
Correct |
38 ms |
50124 KB |
Output is correct |
20 |
Correct |
37 ms |
50116 KB |
Output is correct |
21 |
Correct |
217 ms |
57216 KB |
Output is correct |
22 |
Correct |
253 ms |
69812 KB |
Output is correct |
23 |
Correct |
219 ms |
73728 KB |
Output is correct |
24 |
Correct |
287 ms |
75428 KB |
Output is correct |
25 |
Correct |
294 ms |
86392 KB |
Output is correct |
26 |
Correct |
269 ms |
85896 KB |
Output is correct |
27 |
Correct |
193 ms |
64772 KB |
Output is correct |
28 |
Correct |
198 ms |
64776 KB |
Output is correct |
29 |
Correct |
457 ms |
90912 KB |
Output is correct |
30 |
Correct |
316 ms |
90216 KB |
Output is correct |
31 |
Correct |
503 ms |
89736 KB |
Output is correct |
32 |
Correct |
483 ms |
89904 KB |
Output is correct |
33 |
Correct |
494 ms |
89692 KB |
Output is correct |
34 |
Correct |
351 ms |
89076 KB |
Output is correct |
35 |
Correct |
457 ms |
82924 KB |
Output is correct |
36 |
Correct |
285 ms |
75140 KB |
Output is correct |
37 |
Correct |
253 ms |
74480 KB |
Output is correct |
38 |
Correct |
170 ms |
60168 KB |
Output is correct |
39 |
Correct |
162 ms |
59208 KB |
Output is correct |
40 |
Correct |
208 ms |
68776 KB |
Output is correct |
41 |
Correct |
681 ms |
154948 KB |
Output is correct |
42 |
Correct |
552 ms |
161528 KB |
Output is correct |
43 |
Correct |
172 ms |
72132 KB |
Output is correct |
44 |
Correct |
425 ms |
146516 KB |
Output is correct |
45 |
Correct |
697 ms |
171792 KB |
Output is correct |
46 |
Correct |
720 ms |
168344 KB |
Output is correct |
47 |
Correct |
841 ms |
168232 KB |
Output is correct |
48 |
Correct |
541 ms |
159720 KB |
Output is correct |
49 |
Correct |
497 ms |
163836 KB |
Output is correct |
50 |
Correct |
526 ms |
152172 KB |
Output is correct |
51 |
Correct |
505 ms |
150740 KB |
Output is correct |
52 |
Correct |
1091 ms |
176672 KB |
Output is correct |
53 |
Correct |
976 ms |
176712 KB |
Output is correct |
54 |
Correct |
1109 ms |
176224 KB |
Output is correct |
55 |
Correct |
1136 ms |
175436 KB |
Output is correct |
56 |
Correct |
758 ms |
164308 KB |
Output is correct |
57 |
Correct |
718 ms |
161660 KB |
Output is correct |
58 |
Correct |
779 ms |
166516 KB |
Output is correct |
59 |
Correct |
494 ms |
150808 KB |
Output is correct |
60 |
Correct |
534 ms |
154220 KB |
Output is correct |
61 |
Correct |
490 ms |
154008 KB |
Output is correct |
62 |
Correct |
1014 ms |
170856 KB |
Output is correct |
63 |
Correct |
451 ms |
137980 KB |
Output is correct |
64 |
Correct |
447 ms |
138212 KB |
Output is correct |
65 |
Correct |
471 ms |
139012 KB |
Output is correct |
66 |
Correct |
367 ms |
132408 KB |
Output is correct |
67 |
Correct |
370 ms |
131692 KB |
Output is correct |
68 |
Correct |
397 ms |
130784 KB |
Output is correct |
69 |
Correct |
676 ms |
156632 KB |
Output is correct |
70 |
Correct |
631 ms |
153224 KB |
Output is correct |
71 |
Correct |
642 ms |
154292 KB |
Output is correct |
72 |
Correct |
593 ms |
154504 KB |
Output is correct |