This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e9
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
#define sz size()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
const int N = (1<<20);
int n, k;
vi r;
struct Seg {
ii seg[N*2]; int laz[N*2];
void build(int p=1, int l=0, int r=N-1) {
laz[p] = 0;
if(l == r) {
seg[p] = {0,l};
return;
}
int m=(l+r)/2;
build(p*2,l,m);
build(p*2+1,m+1,r);
seg[p] = min(seg[p*2], seg[p*2+1]);
}
void addNode(int p, int v) {
laz[p] += v;
seg[p] = {seg[p].fi+v, seg[p].se};
}
void push(int p) {
addNode(p*2,laz[p]);
addNode(p*2+1,laz[p]);
laz[p] = 0;
}
ii get(int i=0, int j=n-1, int p=1, int l=0 , int r=N-1) {
if(j < l || i > r) return {INF,INF};
if(i <= l && j >= r) return seg[p];
push(p);
int m=(l+r)/2;
ii a=get(i,j,p*2,l,m);
ii b=get(i,j,p*2+1,m+1,r);
return min(a,b);
}
void add(int i, int j, int v, int p=1, int l=0, int r=N-1) {
if(j < l || i > r) return;
if(i <= l && j >= r) {
addNode(p,v);
return;
}
push(p);
int m=(l+r)/2;
add(i,j,v,p*2,l,m);
add(i,j,v,p*2+1,m+1,r);
seg[p] = min(seg[p*2], seg[p*2+1]);
}
void addCycle(int i, int l, int v) {
int j = i+l-1;
add(i,min(n-1,j),v);
if(j >= n)
add(0,j-n,v);
}
} seg1, seg2;
struct MinSeg {
ii seg[N*2];
void buildSeg(int p=1, int l=0, int r=N-1) {
if(l == r) {
seg[p] = {0,l};
return;
}
int m=(l+r)/2;
buildSeg(p*2,l,m);
buildSeg(p*2+1,m+1,r);
seg[p] = min(seg[p*2], seg[p*2+1]);
}
void setSeg(int i, int v, int p=1, int l=0, int r=N-1) {
if(i < l || i > r) return;
if(l == r) {
seg[p] = {v, seg[p].se};
return;
}
int m=(l+r)/2;
setSeg(i,v,p*2,l,m);
setSeg(i,v,p*2+1,m+1,r);
seg[p] = min(seg[p*2], seg[p*2+1]);
}
ii getSeg(int i, int j, int p=1, int l=0, int r=N-1) {
if(i > j) return {INF,INF};
if(j < l || i > r) return {INF,INF};
if(i <= l && j >= r) return seg[p];
int m=(l+r)/2;
ii a = getSeg(i,j,p*2,l,m);
ii b = getSeg(i,j,p*2+1,m+1,r);
return min(a,b);
}
} minSeg;
vi val, rVal;
vi al[20], ar[20];
void init(int k_, vi r_) {
k = k_; r = r_;
n = r.size();
// init segs
seg1.build(); seg2.build();
RE(i,n) seg1.add(i,i,r[i]);
RE(i,n) seg2.add(i,i,1);
// fill val
val.assign(n,0);
REV(i,0,n) {
while(true) {
ii p = seg1.get();
if(p.fi != 0)
break;
int j = p.se;
seg1.add(j,j,INF);
seg2.add(j,j,-1);
seg2.addCycle((j+1)%n,k-1,1);
}
ii p = seg2.get();
if(p.fi != 0)
break;
int j = p.se;
seg2.add(j,j,INF);
seg1.addCycle((j-k+1+n)%n,k-1,-1);
seg2.addCycle((j+1)%n,k-1,-1);
val[j] = i;
}
// reverse val
rVal.assign(n,0);
RE(i,n) rVal[val[i]] = i;
// fill al and ar
RE(i,20) al[i].assign(n*2,-1), ar[i].assign(n*2,-1);
RE(i,n*2) minSeg.setSeg(i,INF);
REV(x,0,n) {
int i = rVal[x];
al[0][i ] = minSeg.getSeg(max(0,i -k+1), i -1).se;
al[0][i+n] = minSeg.getSeg(max(0,i+n-k+1), i+n-1).se;
ar[0][i ] = minSeg.getSeg(i +1, min(n*2-1, i +k-1)).se;
ar[0][i+n] = minSeg.getSeg(i+n+1, min(n*2-1, i+n+k-1)).se;
if(al[0][i ] == INF) al[0][i ] = -1;
if(al[0][i+n] == INF) al[0][i+n] = -1;
if(ar[0][i ] == INF) ar[0][i ] = -1;
if(ar[0][i+n] == INF) ar[0][i+n] = -1;
minSeg.setSeg(i,x);
}
REP(i,1,20) RE(j,n*2) if(al[i-1][j] != -1) al[i][j] = al[i-1][al[i-1][j]];
REP(i,1,20) RE(j,n*2) if(ar[i-1][j] != -1) ar[i][j] = ar[i-1][ar[i-1][j]];
}
int compare_plants(int x, int y) {
int mult = -1;
if(val[x] > val[y]) swap(x,y), mult = 1;
// going to the right
{
int u=x, v=y;
if(u > v) v += n;
REV(i,0,20) while(ar[i][u] != -1 && ar[i][u] <= v) u = ar[i][u];
if(val[u] < val[v] && abs(u-v) < k)
return mult;
}
// going to the left
{
int u=x, v=y;
if(u < v) u += n;
REV(i,0,20) while(al[i][u] != -1 && al[i][u] >= v) u = al[i][u];
if(val[u] < val[v] && abs(u-v) < k)
return mult;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |