#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
const int MAX_N = 2*1e5;
int bl[2*MAX_N][30];
int max_p[MAX_N];
vector<vector<pii>> adj;
int target;
int n;
int dfs(int u, vector<int>& vis, int d){
////////cout<<"dfs "<<u<<" "<<u%n<<" "<<u/n<<endl;
vis[u] =d;
if(vis[bl[u][0]] == -1){
return dfs(bl[u][0], vis, d+1);
}
else{
if(bl[u][0] == target){
return d+1;
}
else{
return -1;
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
n =N;
adj.resize(N);
for(int i = 0; i <M; i++){
adj[R[i][0]].push_back(pii(i, R[i][1]));
adj[R[i][1]].push_back(pii(i, R[i][0]));
}
for(int i = 0; i<N; i++){
max_p[i] = adj[i][0].first;
}
for(int i = 0; i<N; i++){
bl[i][0] = adj[i][0].second;
if(adj[i][0].first == max_p[adj[i][0].second]){
bl[i][0] += N;
}
if(adj[i].size()>1){
bl[i+N][0] = adj[i][1].second;
if(adj[i][1].first == max_p[adj[i][1].second]){
bl[i+N][0] += N;
}
}
else{
bl[i+N][0] = bl[i][0];
}
}
for(int step = 1; step<30; step++){
for(int i = 0; i<2*N; i++){
bl[i][step] = bl[bl[i][step-1]][step-1];
}
}
vector<int> res(Q, 0);
auto calc = [&](){
vector<int> ch(2*N, -1);
int len = dfs(target, ch, 0);
////////cout<<endl;
//cout<<len<<endl;
if(len == -1){
map<int, int> oc;
for(int i = 0; i<N; i++){
int dist= -1;
if(i == target){
dist = 0;
}
else if(ch[i]!=-1){
dist= -1;
}
else{
int cur = i;
dist = 1;
for(int step = 29; step>=0; step--){
if(ch[bl[cur][step]] == -1){
cur = bl[cur][step];
dist+= (1<<step);
}
////cout<<dist<<endl;
}
if(bl[cur][0] == target){
}
else{
dist = -1;
}
}
//cout<<i<<"dist "<<dist<<endl;
if(dist!=-1){
oc[dist] ++;
}
}
for(int i = 0; i<Q; i++){
res[i] += oc[G[i]];
}
}
else{
vector<vector<int>> oc(len);
for(int i = 0; i<N; i++){
int dist= -1;
if(i == target){
dist = 0;
}
else if(ch[i]!=-1){
dist= len-ch[i];
}
else{
int cur = i;
dist = 1;
for(int step = 29; step>=0; step--){
if(ch[bl[cur][step]] == -1){
cur = bl[cur][step];
dist+=(1<<step);
}
}
if(ch[bl[cur][0]] != -1){
dist += (len-ch[bl[cur][0]])%len;
}
else{
dist = -1;
}
}
if(dist!=-1){
oc[dist%len].push_back(dist);
}
//cout<<i<<" dist: "<<dist<<endl;
}
for(vector<int>& e: oc){
sort(e.begin(), e.end());
}
for(int i = 0; i<Q; i++){
int mod = G[i]%len;
for(auto e: oc[mod]){
//cout<<e<<" | ";
}
//cout<<endl;
int cur =-1;
for(int step = (1<<29); step>0; step/=2){
if(cur+step< oc[mod].size() && oc[mod][cur+step]<=G[i]){
////cout<<oc[mod][cur+step]<<endl;
cur+=step;
}
}
//cout<<i<<" "<<cur<<" "<<endl;
res[i]+=cur+1;
}
}
};
target = P;
calc();
target = P+N;
calc();
for(auto e: res){
answer(e);
}
}
Compilation message
garden.cpp: In lambda function:
garden.cpp:159:22: warning: unused variable 'e' [-Wunused-variable]
159 | for(auto e: oc[mod]){
| ^
garden.cpp:166:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | if(cur+step< oc[mod].size() && oc[mod][cur+step]<=G[i]){
| ~~~~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
2 ms |
4840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
2 ms |
4840 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
17 ms |
10488 KB |
Output is correct |
12 |
Correct |
33 ms |
16440 KB |
Output is correct |
13 |
Correct |
67 ms |
38056 KB |
Output is correct |
14 |
Correct |
119 ms |
46604 KB |
Output is correct |
15 |
Correct |
143 ms |
47652 KB |
Output is correct |
16 |
Correct |
98 ms |
34968 KB |
Output is correct |
17 |
Correct |
72 ms |
31992 KB |
Output is correct |
18 |
Correct |
27 ms |
16432 KB |
Output is correct |
19 |
Correct |
125 ms |
46600 KB |
Output is correct |
20 |
Correct |
116 ms |
47444 KB |
Output is correct |
21 |
Correct |
90 ms |
34780 KB |
Output is correct |
22 |
Correct |
72 ms |
31716 KB |
Output is correct |
23 |
Correct |
223 ms |
51792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
2 ms |
4840 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
17 ms |
10488 KB |
Output is correct |
12 |
Correct |
33 ms |
16440 KB |
Output is correct |
13 |
Correct |
67 ms |
38056 KB |
Output is correct |
14 |
Correct |
119 ms |
46604 KB |
Output is correct |
15 |
Correct |
143 ms |
47652 KB |
Output is correct |
16 |
Correct |
98 ms |
34968 KB |
Output is correct |
17 |
Correct |
72 ms |
31992 KB |
Output is correct |
18 |
Correct |
27 ms |
16432 KB |
Output is correct |
19 |
Correct |
125 ms |
46600 KB |
Output is correct |
20 |
Correct |
116 ms |
47444 KB |
Output is correct |
21 |
Correct |
90 ms |
34780 KB |
Output is correct |
22 |
Correct |
72 ms |
31716 KB |
Output is correct |
23 |
Correct |
223 ms |
51792 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
19 ms |
10520 KB |
Output is correct |
26 |
Correct |
34 ms |
16476 KB |
Output is correct |
27 |
Correct |
66 ms |
38224 KB |
Output is correct |
28 |
Correct |
116 ms |
46760 KB |
Output is correct |
29 |
Correct |
142 ms |
47708 KB |
Output is correct |
30 |
Correct |
96 ms |
34948 KB |
Output is correct |
31 |
Correct |
70 ms |
31996 KB |
Output is correct |
32 |
Correct |
28 ms |
16464 KB |
Output is correct |
33 |
Correct |
118 ms |
46844 KB |
Output is correct |
34 |
Correct |
122 ms |
47644 KB |
Output is correct |
35 |
Correct |
93 ms |
34868 KB |
Output is correct |
36 |
Correct |
70 ms |
31692 KB |
Output is correct |
37 |
Correct |
211 ms |
51800 KB |
Output is correct |
38 |
Correct |
421 ms |
56580 KB |
Output is correct |