#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=3e5+7, INF=1e9+7;
int nxt[LIM], odl[LIM], cnt[LIM], ile[LIM][2], ans[LIM][2], cykl[2], n;
vector<int>V[LIM];
void sortuj() {
int ma=0, ilei=0;
rep(i, n) {
if(odl[i]==INF) ++ilei;
else ma=max(ma, odl[i]);
}
rep(i, n) if(odl[i]<INF) ++cnt[odl[i]];
int l=0;
for(int i=0; cnt[ma]; ++i) {
while(!cnt[l]) ++l;
odl[i]=l;
--cnt[l];
}
for(int i=n-1; ilei; --i, --ilei) odl[i]=INF;
}
void solve(int x, int r) {
cykl[r]=INF;
rep(i, 2*n) odl[i]=INF;
odl[x]=0;
queue<int>q;
q.push(x);
while(!q.empty()) {
int p=q.front(); q.pop();
for(auto i : V[p]) if(odl[i]>odl[p]+1) {
odl[i]=odl[p]+1;
q.push(i);
}
for(auto i : V[p]) if(i==x) cykl[r]=odl[p]+1;
}
sortuj();
int l=0;
rep(i, LIM) {
int s=l;
while(s<n && odl[s]==i) {
++ans[i][r];
++s;
}
if(cykl[r]<INF) {
ans[i][r]+=ile[i%cykl[r]][r];
}
while(l<n && odl[l]==i) {
++ile[i%cykl[r]][r];
++l;
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
n=N;
rep(i, 2*n) nxt[i]=-1;
rep(i, M) {
int p1=R[i][1], p2=R[i][0];
if(nxt[R[i][1]]==-1) p1+=n;
if(nxt[R[i][0]]==-1) p2+=n;
if(nxt[R[i][0]]==-1) nxt[R[i][0]]=p1;
else if(nxt[R[i][0]+n]==-1) nxt[R[i][0]+n]=p1;
if(nxt[R[i][1]]==-1) nxt[R[i][1]]=p2;
else if(nxt[R[i][1]+n]==-1) nxt[R[i][1]+n]=p2;
}
rep(i, n) if(nxt[i]!=-1 && nxt[n+i]==-1) nxt[n+i]=nxt[i];
rep(i, 2*n) if(nxt[i]!=-1) V[nxt[i]].pb(i);
solve(P, 0);
solve(P+n, 1);
rep(i, Q) {
if(G[i]<LIM) answer(ans[G[i]][0]+ans[G[i]][1]);
else {
int x=0;
if(cykl[0]<INF) x+=ile[G[i]%cykl[0]][0];
if(cykl[1]<INF) x+=ile[G[i]%cykl[1]][1];
answer(x);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9772 KB |
Output is correct |
2 |
Correct |
10 ms |
9784 KB |
Output is correct |
3 |
Correct |
9 ms |
9784 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
8 ms |
9644 KB |
Output is correct |
6 |
Correct |
10 ms |
9796 KB |
Output is correct |
7 |
Correct |
9 ms |
9668 KB |
Output is correct |
8 |
Correct |
11 ms |
9784 KB |
Output is correct |
9 |
Correct |
11 ms |
9916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9772 KB |
Output is correct |
2 |
Correct |
10 ms |
9784 KB |
Output is correct |
3 |
Correct |
9 ms |
9784 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
8 ms |
9644 KB |
Output is correct |
6 |
Correct |
10 ms |
9796 KB |
Output is correct |
7 |
Correct |
9 ms |
9668 KB |
Output is correct |
8 |
Correct |
11 ms |
9784 KB |
Output is correct |
9 |
Correct |
11 ms |
9916 KB |
Output is correct |
10 |
Correct |
10 ms |
9748 KB |
Output is correct |
11 |
Correct |
16 ms |
11436 KB |
Output is correct |
12 |
Correct |
25 ms |
12672 KB |
Output is correct |
13 |
Correct |
44 ms |
18940 KB |
Output is correct |
14 |
Correct |
74 ms |
18468 KB |
Output is correct |
15 |
Correct |
90 ms |
18692 KB |
Output is correct |
16 |
Correct |
62 ms |
16628 KB |
Output is correct |
17 |
Correct |
58 ms |
15924 KB |
Output is correct |
18 |
Correct |
21 ms |
10296 KB |
Output is correct |
19 |
Correct |
71 ms |
18456 KB |
Output is correct |
20 |
Correct |
97 ms |
18708 KB |
Output is correct |
21 |
Correct |
66 ms |
16492 KB |
Output is correct |
22 |
Correct |
63 ms |
15940 KB |
Output is correct |
23 |
Correct |
68 ms |
19152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9772 KB |
Output is correct |
2 |
Correct |
10 ms |
9784 KB |
Output is correct |
3 |
Correct |
9 ms |
9784 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
8 ms |
9644 KB |
Output is correct |
6 |
Correct |
10 ms |
9796 KB |
Output is correct |
7 |
Correct |
9 ms |
9668 KB |
Output is correct |
8 |
Correct |
11 ms |
9784 KB |
Output is correct |
9 |
Correct |
11 ms |
9916 KB |
Output is correct |
10 |
Correct |
10 ms |
9748 KB |
Output is correct |
11 |
Correct |
16 ms |
11436 KB |
Output is correct |
12 |
Correct |
25 ms |
12672 KB |
Output is correct |
13 |
Correct |
44 ms |
18940 KB |
Output is correct |
14 |
Correct |
74 ms |
18468 KB |
Output is correct |
15 |
Correct |
90 ms |
18692 KB |
Output is correct |
16 |
Correct |
62 ms |
16628 KB |
Output is correct |
17 |
Correct |
58 ms |
15924 KB |
Output is correct |
18 |
Correct |
21 ms |
10296 KB |
Output is correct |
19 |
Correct |
71 ms |
18456 KB |
Output is correct |
20 |
Correct |
97 ms |
18708 KB |
Output is correct |
21 |
Correct |
66 ms |
16492 KB |
Output is correct |
22 |
Correct |
63 ms |
15940 KB |
Output is correct |
23 |
Correct |
68 ms |
19152 KB |
Output is correct |
24 |
Correct |
10 ms |
9656 KB |
Output is correct |
25 |
Correct |
17 ms |
11460 KB |
Output is correct |
26 |
Correct |
26 ms |
12684 KB |
Output is correct |
27 |
Correct |
39 ms |
19060 KB |
Output is correct |
28 |
Correct |
65 ms |
18472 KB |
Output is correct |
29 |
Correct |
92 ms |
18748 KB |
Output is correct |
30 |
Correct |
64 ms |
16588 KB |
Output is correct |
31 |
Correct |
56 ms |
15856 KB |
Output is correct |
32 |
Correct |
21 ms |
10352 KB |
Output is correct |
33 |
Correct |
73 ms |
18472 KB |
Output is correct |
34 |
Correct |
86 ms |
18732 KB |
Output is correct |
35 |
Correct |
64 ms |
16488 KB |
Output is correct |
36 |
Correct |
67 ms |
15896 KB |
Output is correct |
37 |
Correct |
81 ms |
19244 KB |
Output is correct |
38 |
Correct |
10 ms |
9656 KB |
Output is correct |