#include "garden.h"
#include "gardenlib.h"
#include <cstdio>
#include <vector>
#include <initializer_list>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 151515;
int N;
int minv1[MAXN], minv2[MAXN];
int outlist[2*MAXN];
vector<int> inlist[2*MAXN];
bool visit[2*MAXN];
int ans1[2*MAXN], ans2[2*MAXN];
void run(int P, int dist, int *ans, int &res, int oP)
{
if(visit[P])
{
if(P == oP) res = dist;
return;
}
visit[P] = true;
if(P<N) ans[dist]++;
for(auto x: inlist[P])
run(x, dist+1, ans, res, oP);
}
void count_routes(int _N, int M, int P, int R[][2], int Q, int G[])
{
N = _N;
for(int i=0; i<N; ++i) minv1[i] = minv2[i] = -1;
for(int i=0; i<M; ++i)
{
int u = R[i][0], v = R[i][1];
for(int x: {u, v})
{
int y = u+v-x;
if(minv1[x] == -1) minv1[x] = y;
else if(minv2[x] == -1) minv2[x] = y;
}
}
for(int i=0; i<N; ++i) if(minv2[i] == -1) minv2[i] = minv1[i];
for(int i=0; i<2*N; ++i)
{
int to = (i<N)?minv1[i%N]:minv2[i%N];
if(i%N == minv1[to]) to += N;
outlist[i] = to;
inlist[to].push_back(i);
}
int res1 = 0x3f3f3f3f, res2 = 0x3f3f3f3f;
run(P, 0, ans1, res1, P);
for(int i=0; i<2*N; ++i) visit[i] = false;
run(P+N, 0, ans2, res2, P+N);
for(int i=0; i<Q; ++i)
{
int ans = 0;
for(int j=G[i]%res1; j<=G[i] && j <= N; j += res1)
ans += ans1[j];
for(int j=G[i]%res2; j<=G[i] && j <= N; j += res2)
ans += ans2[j];
answer(ans);
}
return ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7644 KB |
Output is correct |
2 |
Correct |
9 ms |
7516 KB |
Output is correct |
3 |
Correct |
9 ms |
7544 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
6 ms |
7644 KB |
Output is correct |
7 |
Correct |
8 ms |
7408 KB |
Output is correct |
8 |
Correct |
9 ms |
7544 KB |
Output is correct |
9 |
Correct |
10 ms |
7548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7644 KB |
Output is correct |
2 |
Correct |
9 ms |
7516 KB |
Output is correct |
3 |
Correct |
9 ms |
7544 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
6 ms |
7644 KB |
Output is correct |
7 |
Correct |
8 ms |
7408 KB |
Output is correct |
8 |
Correct |
9 ms |
7544 KB |
Output is correct |
9 |
Correct |
10 ms |
7548 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
18 ms |
9080 KB |
Output is correct |
12 |
Correct |
32 ms |
9948 KB |
Output is correct |
13 |
Correct |
59 ms |
26200 KB |
Output is correct |
14 |
Correct |
103 ms |
15540 KB |
Output is correct |
15 |
Correct |
135 ms |
15608 KB |
Output is correct |
16 |
Correct |
103 ms |
13660 KB |
Output is correct |
17 |
Correct |
89 ms |
12712 KB |
Output is correct |
18 |
Correct |
31 ms |
9900 KB |
Output is correct |
19 |
Correct |
100 ms |
16184 KB |
Output is correct |
20 |
Correct |
122 ms |
16492 KB |
Output is correct |
21 |
Correct |
103 ms |
14412 KB |
Output is correct |
22 |
Correct |
85 ms |
13632 KB |
Output is correct |
23 |
Correct |
131 ms |
17104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7644 KB |
Output is correct |
2 |
Correct |
9 ms |
7516 KB |
Output is correct |
3 |
Correct |
9 ms |
7544 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
6 ms |
7644 KB |
Output is correct |
7 |
Correct |
8 ms |
7408 KB |
Output is correct |
8 |
Correct |
9 ms |
7544 KB |
Output is correct |
9 |
Correct |
10 ms |
7548 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
18 ms |
9080 KB |
Output is correct |
12 |
Correct |
32 ms |
9948 KB |
Output is correct |
13 |
Correct |
59 ms |
26200 KB |
Output is correct |
14 |
Correct |
103 ms |
15540 KB |
Output is correct |
15 |
Correct |
135 ms |
15608 KB |
Output is correct |
16 |
Correct |
103 ms |
13660 KB |
Output is correct |
17 |
Correct |
89 ms |
12712 KB |
Output is correct |
18 |
Correct |
31 ms |
9900 KB |
Output is correct |
19 |
Correct |
100 ms |
16184 KB |
Output is correct |
20 |
Correct |
122 ms |
16492 KB |
Output is correct |
21 |
Correct |
103 ms |
14412 KB |
Output is correct |
22 |
Correct |
85 ms |
13632 KB |
Output is correct |
23 |
Correct |
131 ms |
17104 KB |
Output is correct |
24 |
Correct |
9 ms |
7416 KB |
Output is correct |
25 |
Correct |
17 ms |
9336 KB |
Output is correct |
26 |
Correct |
33 ms |
10360 KB |
Output is correct |
27 |
Correct |
61 ms |
26644 KB |
Output is correct |
28 |
Correct |
151 ms |
15508 KB |
Output is correct |
29 |
Correct |
187 ms |
15836 KB |
Output is correct |
30 |
Correct |
100 ms |
13640 KB |
Output is correct |
31 |
Correct |
124 ms |
12860 KB |
Output is correct |
32 |
Correct |
34 ms |
9872 KB |
Output is correct |
33 |
Correct |
158 ms |
15584 KB |
Output is correct |
34 |
Correct |
172 ms |
15716 KB |
Output is correct |
35 |
Correct |
102 ms |
13612 KB |
Output is correct |
36 |
Correct |
107 ms |
12796 KB |
Output is correct |
37 |
Correct |
118 ms |
16364 KB |
Output is correct |
38 |
Correct |
103 ms |
31352 KB |
Output is correct |