#include "plants.h"
#include <iostream>
#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 int l,m;
// void print(std::vector<int> &v) {for(int elem: v) std::cout << elem << " "; std::cout << std::endl;}
void print(std::vector<int> &v) {for(int elem: v) std::cout << 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;
return;
}
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].second += lz[node]; lz[node]=0;
if (right < start or end < left) return {m+1,maxVal};
if (left <= start and end <= right) return t[node];
int mid=(start+end)/2;
int minI1,minI2,min1,min2;
std::pair<int,int> ans;
std::tie(minI1,min1) = update(2*node,start,mid,left,right,diff);
std::tie(minI2,min2) = update(2*node+1,mid+1,end,left,right,diff);
if (min1<min2 or min1==min2 and minI1 < minI2) ans = {minI1,min1};
else ans = {minI2,min2};
std::tie(minI1,min1) = t[2*node];
std::tie(minI2,min2) = t[2*node+1];
if (min1<min2 or min1==min2 and minI1 < minI2) t[node] = t[2*node];
else t[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].second += lz[node]; lz[node]=0;
if (right < start or end < left) return {m+1,maxVal};
if (left <= start and end <= right) return t[node];
int mid=(start+end)/2;
int minI1,minI2,min1,min2;
std::pair<int,int> ans;
std::tie(minI1,min1) = findMin(2*node,start,mid,left,right);
std::tie(minI2,min2) = findMin(2*node+1,mid+1,end,left,right);
if (min1<min2 or min1==min2 and minI1 < minI2) ans = {minI1,min1};
else ans = {minI2,min2};
std::tie(minI1,min1) = t[2*node];
std::tie(minI2,min2) = t[2*node+1];
if (min1<min2 or min1==min2 and minI1 < minI2) t[node] = t[2*node];
else t[node] = t[2*node+1];
return ans;
}
void segInit(int node, int start, int end) {
if (start==end) {t[node]={start,r[start]}; return;}
int mid=(start+end)/2;
segInit(2*node,start,mid);
segInit(2*node+1,mid+1,end);
auto [minI1,min1] = t[2*node];
auto [minI2,min2] = t[2*node+1];
t[node] = min1<min2 or min1==min2 and minI1<minI2? t[2*node]:t[2*node+1];
return;
}
void init3(int k, std::vector<int> r) {
int n = r.size();
//r.push_back(maxVal);
t.resize(4*n); lz.resize(4*n);
segInit(1,0,n-1);
// print(r); print(t);
rank.resize(n);
for(int i=n-1; i>=0; i--) {
auto [minI1,min1] = findMin(1,0,n-1,0,n-1);
// std::cout << "minI1: " << minI1 << std::endl;
// std:: cout << "t" << std::endl; print(t);
int minI2=minI1,min2=1;
int minI3=minI1,min3=1;
if (minI1>0) std::tie(minI2,min2) = findMin(1,0,n-1,std::max(0,minI1-(n-k)),minI1-1);
if (minI1-(n-k)<0) std::tie(minI3,min3) = findMin(1,0,n-1,minI1-(n-k)+n,n-1);
if (min3==0) minI1=minI3;
else if (min2==0) minI1=minI2;
// std::cout << "minI1: " << minI1 << std::endl;
// print(t);
rank[minI1] = i;
// std::cout << "rank: "; print(rank);
update(1,0,n-1,minI1,minI1,n-1);
// std::cout << "t" << std::endl; print(t);
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);
// std::cout << "minI1-k+1: " << minI1-k+1 << std::endl;
// std::cout << "t" << std::endl; print(t);
// std::cout << "lz" << std::endl; print(lz);
}
return;
}
int compare_plants3(int x, int y) {
if (rank[x]>rank[y]) return 1;
else return -1;
}
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;
}
int compare_plants2(int x, int y) {
if (rank[x]>rank[y]) return 1;
else return -1;
}
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;
}
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;
}
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);
return;
}
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_plants3(x,y);
return 0;
}
Compilation message
plants.cpp: In function 'void print(std::vector<std::pair<int, int> >&)':
plants.cpp:13:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
13 | for(auto [a,b]: v)
| ^
plants.cpp: In function 'std::pair<int, int> update(int, int, int, int, int, int)':
plants.cpp:31:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
31 | if (min1<min2 or min1==min2 and minI1 < minI2) ans = {minI1,min1};
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
plants.cpp:36:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
36 | if (min1<min2 or min1==min2 and minI1 < minI2) t[node] = t[2*node];
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
plants.cpp: In function 'std::pair<int, int> findMin(int, int, int, int, int)':
plants.cpp:53:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
53 | if (min1<min2 or min1==min2 and minI1 < minI2) ans = {minI1,min1};
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
plants.cpp:58:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
58 | if (min1<min2 or min1==min2 and minI1 < minI2) t[node] = t[2*node];
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
plants.cpp: In function 'void segInit(int, int, int)':
plants.cpp:63:37: error: 'r' was not declared in this scope
63 | if (start==end) {t[node]={start,r[start]}; return;}
| ^
plants.cpp:63:45: error: no match for 'operator=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} and '<brace-enclosed initializer list>')
63 | if (start==end) {t[node]={start,r[start]}; return;}
| ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
from /usr/include/c++/10/vector:60,
from plants.h:1,
from plants.cpp:1:
/usr/include/c++/10/bits/stl_pair.h:390:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(typename std::conditional<std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value, const std::pair<_T1, _T2>&, const std::__nonesuch&>::type) [with _T1 = int; _T2 = int; typename std::conditional<std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value, const std::pair<_T1, _T2>&, const std::__nonesuch&>::type = const std::pair<int, int>&]'
390 | operator=(typename conditional<
| ^~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:393:41: note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::conditional<true, const std::pair<int, int>&, const std::__nonesuch&>::type' {aka 'const std::pair<int, int>&'}
390 | operator=(typename conditional<
| ~~~~~~~~~~~~~~~~~~~~~
391 | __and_<is_copy_assignable<_T1>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
392 | is_copy_assignable<_T2>>::value,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
393 | const pair&, const __nonesuch&>::type __p)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_pair.h:401:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(typename std::conditional<std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&, std::__nonesuch&&>::type) [with _T1 = int; _T2 = int; typename std::conditional<std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&, std::__nonesuch&&>::type = std::pair<int, int>&&]'
401 | operator=(typename conditional<
| ^~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:404:31: note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::conditional<true, std::pair<int, int>&&, std::__nonesuch&&>::type' {aka 'std::pair<int, int>&&'}
401 | operator=(typename conditional<
| ~~~~~~~~~~~~~~~~~~~~~
402 | __and_<is_move_assignable<_T1>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
403 | is_move_assignable<_T2>>::value,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
404 | pair&&, __nonesuch&&>::type __p)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_pair.h:418:2: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, const _U1&>, std::is_assignable<_T2&, const _U2&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U1 = _U1; _U2 = _U2; _T1 = int; _T2 = int]'
418 | operator=(const pair<_U1, _U2>& __p)
| ^~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:418:2: note: template argument deduction/substitution failed:
plants.cpp:63:45: note: couldn't deduce template parameter '_U1'
63 | if (start==end) {t[node]={start,r[start]}; return;}
| ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
from /usr/include/c++/10/vector:60,
from plants.h:1,
from plants.cpp:1:
/usr/include/c++/10/bits/stl_pair.h:430:2: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, _U1&&>, std::is_assignable<_T2&, _U2&&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U1 = _U1; _U2 = _U2; _T1 = int; _T2 = int]'
430 | operator=(pair<_U1, _U2>&& __p)
| ^~~~~~~~
/usr/include/c++/10/bits/stl_pair.h:430:2: note: template argument deduction/substitution failed:
plants.cpp:63:45: note: couldn't deduce template parameter '_U1'
63 | if (start==end) {t[node]={start,r[start]}; return;}
| ^
plants.cpp:67:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
67 | auto [minI1,min1] = t[2*node];
| ^
plants.cpp:68:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | auto [minI2,min2] = t[2*node+1];
| ^
plants.cpp:69:39: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
69 | t[node] = min1<min2 or min1==min2 and minI1<minI2? t[2*node]:t[2*node+1];
| ~~~~~~~~~~~^~~~~~~~~~~~~~~
plants.cpp: In function 'void init3(int, std::vector<int>)':
plants.cpp:80:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
80 | auto [minI1,min1] = findMin(1,0,n-1,0,n-1);
| ^