// #include "plants.h"
#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <utility>
#include <tuple>
// using namespace std;
#define maxVal 1'000'000'001
static std:: vector<int> inc,dec,rank,lz;
static std:: vector<std::pair<int,int>> t;
static std:: vector<std::vector<int>> g, path;
static int l,m,q2;
void print(std::vector<int> &v) {for(int elem: v) std::cout << elem << " "; std::cout << std::endl;}
void print(std::vector<std::vector<int>> &v) {for (auto elem: v) print(elem); std::cout << std::endl;}
void print(std::vector<std::pair<int,int>> &v) {for(auto [a,b]: v) std::cout << "(" << a << "," << b << ") ";std::cout << std::endl;}
std::pair<int,int> update(int node,int start,int end,int left,int right,int diff) {
if (left <= start and end <= right) lz[node] += diff;
if (start!=end) {lz[2*node] += lz[node]; lz[2*node+1] += lz[node];}
t[node].first += lz[node]; lz[node]=0;
if (right < start or end < left) return {maxVal,m+1};
if (left <= start and end <= right) return t[node];
int mid=(start+end)/2;
auto min1 = update(2*node,start,mid,left,right,diff);
auto min2 = update(2*node+1,mid+1,end,left,right,diff);
auto ans = min1<min2? min1:min2;
t[node] = t[2*node]<t[2*node+1]? t[2*node]:t[2*node+1];
return ans;
}
std::pair<int,int> findMin(int node, int start, int end, int left, int right) {
if (start!=end) {lz[2*node] += lz[node]; lz[2*node+1] += lz[node];}
t[node].first += lz[node]; lz[node]=0;
if (right < start or end < left) return {maxVal,m+1};
if (left <= start and end <= right) return t[node];
int mid=(start+end)/2;
auto min1 = findMin(2*node,start,mid,left,right);
auto min2 = findMin(2*node+1,mid+1,end,left,right);
auto ans = min1<min2? min1:min2;
t[node] = t[2*node]<t[2*node+1]? t[2*node]:t[2*node+1];
return ans;
}
void segInit(int node, int start, int end, std::vector<int> &r) {
if (start==end) {t[node]={r[start],start}; return;}
int mid=(start+end)/2;
segInit(2*node,start,mid,r);
segInit(2*node+1,mid+1,end,r);
t[node] = t[2*node]<t[2*node+1]? t[2*node]:t[2*node+1];
return;
}
int findPreMinI(int k,int minI1) {
if (minI1>0) {
auto[min2,minI2] = findMin(1,0,m-1,std::max(0,minI1-k+1),minI1-1);
if (min2==0) minI1 = minI2;
}
if (minI1-k+1<0) {
auto[min2,minI2] = findMin(1,0,m-1,minI1-k+1+m,m-1);
if (min2==0) minI1 = minI2;
}
return minI1;
}
int computeRank(int minI1, int k, int ranking) {
if (rank[minI1]!=m) return ranking;
int minI2 = findPreMinI(k,minI1);
while (minI2 != minI1) {
ranking = computeRank(minI2,k,ranking);
minI2 = findPreMinI(k,minI1);
}
rank[minI1] = ranking--;
update(1,0,m-1,minI1,minI1,m-1);
update(1,0,m-1,std::max(0,minI1-k+1),minI1-1,-1);
if (minI1-k+1<0) update(1,0,m-1,minI1-k+1+m,m-1,-1);
return ranking;
}
void transClosure() {
// path.resize(m,std::vector<int>(m));
// auto path = g;
for(int k=0; k<m; k++)
for (int i=0; i<m; i++)
for(int j=0; j<m; j++)
g[i][j] = std::max(g[i][j],g[i][k]*g[k][j]);
return;
}
void init5(int k, std::vector<int> &r) {
int n = r.size();
t.resize(4*n); lz.resize(4*n); g.resize(n,std::vector<int>(n));
segInit(1,0,n-1,r);
rank.resize(n,n);
int ranking = n-1;
while (ranking >=0) {
auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
ranking = computeRank(minI1,k,ranking);
std::vector<int> visit(n);
for(int i=minI1-k+1+n; i<=minI1+k-1+n; i++) {
if (i!=minI1 and rank[i%n]==n and visit[i%n]==0)
{g[minI1][i%n]=1;}
}
}
// std::cout << "g" << std::endl; print(g);
transClosure();
// std::cout << "path" << std::endl; print(g);
return;
}
void init4(int k, std::vector<int> &r) {
int n = r.size();
t.resize(4*n); lz.resize(4*n);
segInit(1,0,n-1,r);
rank.resize(n,n);
int ranking = n-1;
while (ranking >=0) {
auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
ranking = computeRank(minI1,k,ranking);
}
return;
}
void init3(int k, std::vector<int> &r) {
int n = r.size();
t.resize(4*n); lz.resize(4*n);
segInit(1,0,n-1,r);
rank.resize(n);
for(int i=n-1; i>=0; i--) {
auto [min1,minI1] = findMin(1,0,n-1,0,n-1);
int min2=1, minI2=minI1;
int min3=1, minI3=minI1;
if (minI1>0) std::tie(min2,minI2) = findMin(1,0,n-1,std::max(0,minI1-(n-k)),minI1-1);
if (minI1-(n-k)<0) std::tie(min3,minI3) = findMin(1,0,n-1,minI1-(n-k)+n,n-1);
if (min3==0) minI1=minI3;
else if (min2==0) minI1=minI2;
rank[minI1] = i;
update(1,0,n-1,minI1,minI1,n-1);
update(1,0,n-1,std::max(0,minI1-k+1),minI1-1,-1);
if (minI1-k+1<0) update(1,0,n-1,minI1-k+1+n,n-1,-1);
}
return;
}
int findMax(int k, std::vector<int> &r) {
int i=2*m-1;
while (r[i%m]!=0) {i--;}
int j = i-1;
while (j > i-k) {
if (r[j%m]==0) i = j;
j--;
}
for(int j=i; j>i-k; j--) r[j%m]--;
return i%m;
}
void init2(int k, std::vector<int> &r) {
int n = r.size();
rank.resize(n);
for(int i=n-1; i>=0; i--)
rank[findMax(k,r)] = i;
return;
}
void init1(int k, std::vector<int> &r) {
int n = r.size();
inc.resize(n); dec.resize(n);
for(int i=0; i<n; i++) {inc[i] = dec[i] = i;}
int s,incVal,decVal;
for(s=2*n-1; s>=0; s--) {
if (r[s%n]==0) incVal = s;
else decVal = s;
inc[s%n] = incVal; dec[s%n] = decVal;
}
return;
}
void init(int k, std::vector<int> r) {
int n = r.size();
m=n; l=k;
if(k==2) init1(k, r);
else if(n<=5000 and 2*k>n) init2(k, r);
else if(2*k>n) init3(k,r);
else if (n<=300 and q2 <= n*(n-1)/2) init5(k,r);
else init4(k,r);
return;
}
int compare_plants5(int x, int y) {
if (g[x][y]) return 1;
else if (g[y][x]) return -1;
else return 0;
}
int compare_plants2(int x, int y) {
if (rank[x]>rank[y]) return 1;
else return -1;
}
int compare_plants1(int x, int y) {
if (y <= dec[x] or x+m <= inc[y]) return 1;
else if (y <= inc[x] or x+m <= dec[y]) return -1;
return 0;
}
int compare_plants(int x, int y) {
if(l==2) return compare_plants1(x,y);
else if(m<=5000 and 2*l>m) return compare_plants2(x,y);
else if(2*l>m) return compare_plants2(x,y);
else if (m<=300 and q2 <= m*(m-1)/2) return compare_plants5(x,y);
else return compare_plants2(x,y);
return 0;
}
Compilation message
plants.cpp: In function 'void init1(int, std::vector<int>&)':
plants.cpp:170:37: warning: 'decVal' may be used uninitialized in this function [-Wmaybe-uninitialized]
170 | inc[s%n] = incVal; dec[s%n] = decVal;
plants.cpp:170:18: warning: 'incVal' may be used uninitialized in this function [-Wmaybe-uninitialized]
170 | inc[s%n] = incVal; dec[s%n] = decVal;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
28 ms |
3024 KB |
Output is correct |
7 |
Correct |
30 ms |
3412 KB |
Output is correct |
8 |
Correct |
41 ms |
5968 KB |
Output is correct |
9 |
Correct |
41 ms |
5720 KB |
Output is correct |
10 |
Correct |
39 ms |
5712 KB |
Output is correct |
11 |
Correct |
54 ms |
5716 KB |
Output is correct |
12 |
Correct |
39 ms |
5888 KB |
Output is correct |
13 |
Correct |
37 ms |
5712 KB |
Output is correct |
14 |
Correct |
39 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
114 ms |
3088 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
115 ms |
3088 KB |
Output is correct |
11 |
Correct |
89 ms |
3152 KB |
Output is correct |
12 |
Correct |
89 ms |
3412 KB |
Output is correct |
13 |
Correct |
131 ms |
3112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
114 ms |
3088 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
115 ms |
3088 KB |
Output is correct |
11 |
Correct |
89 ms |
3152 KB |
Output is correct |
12 |
Correct |
89 ms |
3412 KB |
Output is correct |
13 |
Correct |
131 ms |
3112 KB |
Output is correct |
14 |
Correct |
52 ms |
4212 KB |
Output is correct |
15 |
Correct |
308 ms |
14672 KB |
Output is correct |
16 |
Correct |
50 ms |
4436 KB |
Output is correct |
17 |
Correct |
294 ms |
14420 KB |
Output is correct |
18 |
Correct |
206 ms |
14280 KB |
Output is correct |
19 |
Correct |
220 ms |
14532 KB |
Output is correct |
20 |
Correct |
243 ms |
14532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
32 ms |
3320 KB |
Output is correct |
4 |
Correct |
179 ms |
15700 KB |
Output is correct |
5 |
Correct |
228 ms |
14608 KB |
Output is correct |
6 |
Correct |
276 ms |
14416 KB |
Output is correct |
7 |
Correct |
306 ms |
14416 KB |
Output is correct |
8 |
Correct |
328 ms |
14432 KB |
Output is correct |
9 |
Correct |
235 ms |
14424 KB |
Output is correct |
10 |
Correct |
183 ms |
14292 KB |
Output is correct |
11 |
Correct |
38 ms |
5840 KB |
Output is correct |
12 |
Correct |
160 ms |
14420 KB |
Output is correct |
13 |
Correct |
197 ms |
14416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
28 ms |
3024 KB |
Output is correct |
7 |
Correct |
30 ms |
3412 KB |
Output is correct |
8 |
Correct |
41 ms |
5968 KB |
Output is correct |
9 |
Correct |
41 ms |
5720 KB |
Output is correct |
10 |
Correct |
39 ms |
5712 KB |
Output is correct |
11 |
Correct |
54 ms |
5716 KB |
Output is correct |
12 |
Correct |
39 ms |
5888 KB |
Output is correct |
13 |
Correct |
37 ms |
5712 KB |
Output is correct |
14 |
Correct |
39 ms |
5716 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
114 ms |
3088 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
348 KB |
Output is correct |
24 |
Correct |
115 ms |
3088 KB |
Output is correct |
25 |
Correct |
89 ms |
3152 KB |
Output is correct |
26 |
Correct |
89 ms |
3412 KB |
Output is correct |
27 |
Correct |
131 ms |
3112 KB |
Output is correct |
28 |
Correct |
52 ms |
4212 KB |
Output is correct |
29 |
Correct |
308 ms |
14672 KB |
Output is correct |
30 |
Correct |
50 ms |
4436 KB |
Output is correct |
31 |
Correct |
294 ms |
14420 KB |
Output is correct |
32 |
Correct |
206 ms |
14280 KB |
Output is correct |
33 |
Correct |
220 ms |
14532 KB |
Output is correct |
34 |
Correct |
243 ms |
14532 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
32 ms |
3320 KB |
Output is correct |
38 |
Correct |
179 ms |
15700 KB |
Output is correct |
39 |
Correct |
228 ms |
14608 KB |
Output is correct |
40 |
Correct |
276 ms |
14416 KB |
Output is correct |
41 |
Correct |
306 ms |
14416 KB |
Output is correct |
42 |
Correct |
328 ms |
14432 KB |
Output is correct |
43 |
Correct |
235 ms |
14424 KB |
Output is correct |
44 |
Correct |
183 ms |
14292 KB |
Output is correct |
45 |
Correct |
38 ms |
5840 KB |
Output is correct |
46 |
Correct |
160 ms |
14420 KB |
Output is correct |
47 |
Correct |
197 ms |
14416 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |