#include <bits/stdc++.h>
#include "plants.h"
using namespace std;
int n;
int k;
vector<int> arr;
void genSome();
void init(int k_, std::vector<int> r_) {
k = k_;
arr = r_;
n = arr.size();
genSome();
}
vector<int> h;
int cur;
vector<pair<int, int>> tree;
vector<int> mod;
void build(int v, int l, int r) {
if (l + 1 == r) {
tree[v] = {arr[l], l};
} else {
int m = (l + r) / 2;
build(2 * v + 1, l, m);
build(2 * v + 2, m, r);
tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]);
}
}
void build() {
tree.resize(4 * n);
mod.resize(4 * n);
build(0, 0, n);
}
void push(int v, int l, int r) {
tree[v].first += mod[v];
if (l + 1 != r) {
mod[2 * v + 1] += mod[v];
mod[2 * v + 2] += mod[v];
}
mod[v] = 0;
}
pair<int, int> GetMin(int v, int l, int r, int ql, int qr) {
if (ql >= r || qr <= l) {
return {n, -1};
}
push(v, l, r);
if (ql <= l && r <= qr) {
return tree[v];
}
int m = (l + r) / 2;
return min(GetMin(2 * v + 1, l, m, ql, qr),
GetMin(2 * v + 2, m, r, ql, qr));
}
pair<int, int> GetMin(int l, int r) {
// l might be negative
if (l >= 0) {
return GetMin(0, 0, n, l, r);
}
return min(GetMin(0, 0, n, 0, r), GetMin(0, 0, n, n + l, n));
}
void Set(int v, int l, int r, int ind, int x) {
if (ind < l || ind >= r)
return;
push(v, l, r);
if (l + 1 == r) {
tree[v] = {x, ind};
} else {
int m = (l + r) / 2;
Set(2 * v + 1, l, m, ind, x);
Set(2 * v + 2, m, r, ind, x);
tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]);
}
}
void Set(int ind, int x) {
Set(0, 0, n, ind, x);
}
void Add(int v, int l, int r, int ql, int qr, int x) {
push(v, l, r);
if (ql >= r || qr <= l)
return;
if (ql <= l && r <= qr) {
mod[v] = x;
push(v, l, r);
return;
}
int m = (l + r) / 2;
Add(2 * v + 1, l, m, ql, qr, x);
Add(2 * v + 2, m, r, ql, qr, x);
tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]);
}
void Add(int l, int r, int x) {
if (l >= 0) {
return Add(0, 0, n, l, r, x);
}
Add(0, 0, n, 0, r, x);
Add(0, 0, n, n + l, n, x);
}
void rec(int ind) {
while (true) {
auto pp = GetMin(ind - k + 1, ind);
if (pp.first == 0) {
rec(pp.second);
} else {
break;
}
}
h[ind] = cur--;
Set(ind, n);
Add(ind - k + 1, ind, -1);
}
void genSome() {
h.resize(n, -1);
build();
cur = n;
while (true) {
auto pp = GetMin(0, n);
if (pp.first == 0) {
rec(pp.second);
} else {
break;
}
}
}
int compare_plants(int x, int y) {
if (h[x] > h[y]) {
return 1;
} else {
return -1;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
1 ms |
216 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
1 ms |
216 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
65 ms |
3172 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |