#include <iostream>
#include <vector>
#include <cassert>
#define MAXN 200001
using namespace std;
int ans[MAXN]; //ans[x] = rank of h[x]
int myr[MAXN];
int n;
int h;
struct Seg{
pair<int,int> tree[2*MAXN];
int lazy[MAXN];
void make() {
for (int i=0; i<n; i++) {
tree[i+n].first = myr[i];
tree[i+n].second = i;
}
for (int i=n-1; i>0; i--) {
tree[i] = min(tree[i<<1], tree[i<<1|1]);
}
}
void apply(int p, int val) {
tree[p].first += val;
if (p<n) lazy[p] += val;
}
void build(int p) {
while (p>1) {
p>>=1;
int x = tree[p<<1].first;
int y = tree[p<<1|1].first;
if (x<=y) {
tree[p].first = x + lazy[p];
tree[p].second = tree[p<<1].second;
} else {
tree[p].first = y+ lazy[p];
tree[p].second = tree[p<<1|1].second;
}
}
}
void push(int p) {
for (int i=h; i>0; i--) {
int j = p>>i;
if (lazy[j] != 0) {
apply(j<<1, lazy[j]);
apply(j<<1|1, lazy[j]);
lazy[j] = 0;
}
}
}
void increm(int l, int r, int val) {
l+=n;
r+=n;
int l0 = l;
int r0 = r;
for (; l<r; l>>=1, r>>=1) {
if (l&1) apply(l++, val);
if (r&1) apply(--r, val);
}
}
pair<int, int> query(int l, int r) {
l+=n;
r+=n;
push(l);
push(r-1);
pair<int,int> res = {2e9, -1};
for (; l<r; l>>=1, r>>=1) {
if (l&1) res = min(res, tree[l++]);
if (r&1) res = min(res, tree[--r]);
}
return res;
}
} seg;
void init(int k, vector<int> r) {
n = r.size();
h = 31 - __builtin_clz(n);
for (int i=0; i<n; i++) myr[i] = r[i];
seg.make();
for (int m=0; m<n; m++) {
pair<int, int> first = seg.query(0, n);
int ind = first.second;
if (ind<k-1) {
pair<int, int> sec = seg.query(n-k+ind+1, n);
if (sec.first==0) {
ind = sec.second;
}
}
ans[ind] = n-m;
if (ind >= k-1) {
seg.increm(ind-k+1, ind, -1);
} else {
seg.increm(0, ind, -1);
seg.increm(n-k+ind+1, n, -1);
}
seg.increm(ind, ind+1, n);
}
}
int compare_plants(int x, int y) {
if (ans[x] > ans[y]) {
return 1;
} else return -1;
}
# | 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... |