#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#include "plants.h"
int n,kk;
int a[200000],h[200000],o[200000];
pii tree[1 << 19];
int lazy[1 << 19];
int build(int s,int e,int i) {
if (s == e) {
tree[i] = mp(a[s],s);
return 0;
}
int mid = (s+e) / 2;
build(s,mid,2*i+1),build(mid+1,e,2*i+2);
tree[i] = min(tree[2*i+1],tree[2*i+2]);
return 0;
}
int prop(int s,int e,int i) {
tree[i].first += lazy[i];
if (s != e) lazy[2*i+1] += lazy[i],lazy[2*i+2] += lazy[i];
lazy[i] = 0;
return 0;
}
int update(int s,int e,int as,int ae,int i,int num) {
prop(s,e,i);
if ((s > ae) || (e < as)) return 0;
else if ((s >= as) && (e <= ae)) {
lazy[i] += num;
prop(s,e,i);
return 0;
}
int mid = (s+e) / 2;
update(s,mid,as,ae,2*i+1,num),update(mid+1,e,as,ae,2*i+2,num);
tree[i] = min(tree[2*i+1],tree[2*i+2]);
return 0;
}
pii query(int s,int e,int qs,int qe,int i) {
prop(s,e,i);
if ((s > qe) || (e < qs)) return mp(1e9,-1);
else if ((s >= qs) && (e <= qe)) return tree[i];
int mid = (s+e) / 2;
return min(query(s,mid,qs,qe,2*i+1),query(mid+1,e,qs,qe,2*i+2));
}
set<int> S;
set<pii> S2;
int insert(int u) {
if (S.empty()) {
S.insert(u),S2.insert(mp(n,u));
return 0;
}
auto it = S.lower_bound(u);
if (it == S.end()) it = S.begin();
auto it2 = it;
if (it2 == S.begin()) it2 = S.end();
it2--;
S2.erase(mp(((*it-*it2+n-1) % n)+1,*it2));
S2.insert(mp(((u-*it2+n-1) % n)+1,*it2));
S2.insert(mp(((*it-u+n-1) % n)+1,u));
S.insert(u);
return 0;
}
int erase(int u) {
S.erase(u);
if (S.empty()) {
S2.erase(mp(n,u));
return 0;
}
auto it = S.lower_bound(u);
if (it == S.end()) it = S.begin();
auto it2 = it;
if (it2 == S.begin()) it2 = S.end();
it2--;
S2.erase(mp(((u-*it2+n-1) % n)+1,*it2));
S2.erase(mp(((*it-u+n-1) % n)+1,u));
S2.insert(mp(((*it-*it2+n-1) % n)+1,*it2));
return 0;
}
int ll[200000][18],rr[200000][18],logn;
void init(int k,vector<int> r) {
int i;
n = r.size(),kk = --k;
for (i = 0; i < n; i++) a[i] = r[i];
build(0,n-1,0);
for (i = 0; i < n; i++) {
if (r[i] == 0) insert(i),update(0,n-1,i,i,0,1e9);
}
for (i = n-1; i >= 0; i--) {
int u = S2.rbegin()->second;
o[i] = u,h[u] = i,a[u] = 1e9,erase(u);
if (u+k < n) update(0,n-1,u+1,u+k,0,-1);
else update(0,n-1,u+1,n-1,0,-1),update(0,n-1,0,u+k-n,0,-1);
while (tree[0].first == 0) insert(tree[0].second),update(0,n-1,tree[0].second,tree[0].second,0,1e9);
}
build(0,n-1,0);
for (i = n-1; i >= 0; i--) {
pii p;
int u = o[i];
update(0,n-1,u,u,0,i-1e9);
if (u-k >= 0) p = query(0,n-1,u-k,u-1,0);
else p = min(query(0,n-1,0,u-1,0),query(0,n-1,u-k+n,n-1,0));
if (p.first == 1e9) ll[i][0] = n;
else ll[i][0] = (i-p.second+n) % n;
if (u+k < n) p = query(0,n-1,u+1,u+k,0);
else p = min(query(0,n-1,u+1,n-1,0),query(0,n-1,0,u+k-n,0));
if (p.first == 1e9) rr[i][0] = n;
else rr[i][0] = (p.second-i+n) % n;
}
int j;
for (i = 1; (1 << i) < n; i++) {
for (j = 0; j < n; j++) {
ll[j][i] = min(ll[j][i-1]+ll[(j+ll[j][i-1]) % n][i-1],n);
rr[j][i] = min(rr[j][i-1]+rr[(j+ll[j][i-1]) % n][i-1],n);
}
}
logn = i;
}
int path(int x,int y) {
int i,u = x;
for (i = logn-1; i >= 0; i--) {
if (ll[u][i] < ((u-y+n) % n)) u = (u-ll[u][i]+n) % n;
}
if ((((u-y+n) % n) <= kk) && (h[u] < h[y])) return 1;
u = x;
for (i = logn-1; i >= 0; i--) {
if (rr[u][i] < ((y-u+n) % n)) u = (u+rr[u][i]) % n;
}
if ((((y-u+n) % n) <= kk) && (h[u] < h[y])) return 1;
return 0;
}
int compare_plants(int x,int y) {
if (h[x] < h[y]) return path(x,y) ? -1:0;
else return path(y,x) ? 1:0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Runtime error |
45 ms |
7184 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |