#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200200;
struct A {
int mn, idx;
A operator + (const A& o) const {
A t;
if (mn < o.mn) t = *this;
else t = o;
return t;
}
} tree[4 * N], tree1[4 * N];
struct B {
int mx, idx;
B() : mx(-1), idx(-1) {}
B operator + (const B& o) const {
B t;
if (mx > o.mx) t = *this;
else t = o;
return t;
}
} tree2[4 * N], __;
int lazy[4 * N], lazy1[4 * N], lv[N];
int jl[19][N], jr[19][N];
int n, k, a[N];
void build(int now = 1, int l = 0, int r = n - 1) {
if (l == r) {
tree[now].mn = 1e9;
tree1[now].mn = a[l];
tree[now].idx = tree1[now].idx = l;
return;
}
int mid = (l + r) / 2;
build(2 * now, l, mid), build(2 * now + 1, mid + 1, r);
tree[now] = tree[2 * now] + tree[2 * now + 1];
tree1[now] = tree1[2 * now] + tree1[2 * now + 1];
}
void lzy(int now, int l, int r) {
tree[now].mn += lazy[now];
tree1[now].mn += lazy1[now];
if (l != r) {
lazy[2 * now] += lazy[now], lazy[2 * now + 1] += lazy[now];
lazy1[2 * now] += lazy1[now], lazy1[2 * now + 1] += lazy1[now];
}
lazy[now] = lazy1[now] = 0;
}
void update(int ql, int qr, int w, int now = 1, int l = 0, int r = n - 1) {
lzy(now, l, r);
if (l > qr || r < ql) return;
if (ql <= l && r <= qr) {
lazy[now] += w;
lzy(now, l, r);
return;
}
int mid = (l + r) / 2;
update(ql, qr, w, 2 * now, l, mid), update(ql, qr, w, 2 * now + 1, mid + 1, r);
tree[now] = tree[2 * now] + tree[2 * now + 1];
tree1[now] = tree1[2 * now] + tree1[2 * now + 1];
}
void update1(int ql, int qr, int w, int now = 1, int l = 0, int r = n - 1) {
lzy(now, l, r);
if (l > qr || r < ql) return;
if (ql <= l && r <= qr) {
lazy1[now] += w;
lzy(now, l, r);
return;
}
int mid = (l + r) / 2;
update1(ql, qr, w, 2 * now, l, mid), update1(ql, qr, w, 2 * now + 1, mid + 1, r);
tree[now] = tree[2 * now] + tree[2 * now + 1];
tree1[now] = tree1[2 * now] + tree1[2 * now + 1];
}
void update2(int v, int w, int now = 1, int l = 0, int r = n - 1) {
if (l == r) {
tree2[now].idx = v;
tree2[now].mx = w;
return;
}
int mid = (l + r) / 2;
if (v <= mid) update2(v, w, 2 * now, l, mid);
else update2(v, w, 2 * now + 1, mid + 1, r);
tree2[now] = tree2[2 * now] + tree2[2 * now + 1];
}
B query(int ql, int qr, int now = 1, int l = 0, int r = n - 1) {
if (l > qr || r < ql) return __;
if (ql <= l && r <= qr) return tree2[now];
int mid = (l + r) / 2;
return query(ql, qr, 2 * now, l, mid) + query(ql, qr, 2 * now + 1, mid + 1, r);
}
void update_1(int i, int w) {
int j = i + k - 1;
if (j >= n) {
update(i + 1, n - 1, w);
update(0, j - n, w);
}
else update(i + 1, j, w);
}
void update_2(int i) {
update1(i, i, 1e9);
update(i, i, -1e9);
}
void update_3(int i, int w) {
int j = i - k + 1;
if (j < 0) {
update1(0, i - 1, w);
update1(j + n, n - 1, w);
}
else update1(j, i - 1, w);
}
void init(int K, vector<int> r) {
n = r.size();
k = K;
for (int i = 0;i < n;i++) a[i] = r[i];
build();
for (int i = 0;i < n;i++) {
if (a[i] == 0) {
update_1(i, 1);
update_2(i);
}
}
stack<int> ord;
for (int _ = 1;_ <= n;_++) {
A now = tree[1];
int i = now.idx;
ord.push(i);
update_1(i, -1);
update_3(i, -1);
update(i, i, 1e9);
A nxt = tree1[1];
while (nxt.mn == 0) {
update_1(nxt.idx, 1);
update_2(nxt.idx);
nxt = tree1[1];
}
}
int x = 0;
while (!ord.empty()) {
int i = ord.top();
ord.pop();
lv[i] = ++x;
}
}
int compare_plants(int x, int y) {
int ret = 1;
if (lv[x] < lv[y]) swap(x, y), ret = -1;
deque<int> dq;
dq.push_back(x);
for (int i = 1;i < n;i++) {
int idx = (x + i) % n;
while (!dq.empty() && (idx - dq.front() + n) % n >= k) dq.pop_front();
if (dq.empty()) break;
if (idx == y) return ret;
while (!dq.empty() && lv[dq.back()] < lv[i]) dq.pop_back();
dq.push_back(i);
}
dq.clear();
dq.push_back(x);
for (int i = 1;i < n;i++) {
int idx = (x - i + n) % n;
while (!dq.empty() && (dq.front() - idx + n) % n >= k) dq.pop_front();
if (dq.empty()) break;
if (idx == y) return ret;
while (!dq.empty() && lv[dq.back()] < lv[i]) dq.pop_back();
dq.push_back(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Incorrect |
1 ms |
14684 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14692 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
14684 KB |
Output is correct |
5 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14692 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Correct |
1 ms |
14684 KB |
Output is correct |
5 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Incorrect |
53 ms |
17424 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Incorrect |
1 ms |
14684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Incorrect |
1 ms |
14684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
1 ms |
14684 KB |
Output is correct |
3 |
Correct |
1 ms |
14684 KB |
Output is correct |
4 |
Incorrect |
1 ms |
14684 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |