#include <bits/stdc++.h>
#ifndef LOCAL
#include "plants.h"
#endif
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define Time (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
return y > x ? (x = y, true) : false;
}
void debug_out() {
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
cerr << ' ' << A;
debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
for (int i = 0; i < n; ++i)
cerr << a[i] << ' ';
cerr << endl;
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
for (auto& e : v)
stream << e << ' ';
return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
return stream << p.first << ' ' << p.second;
}
namespace sol {
const int INF32 = 1e9;
const int maxN = 2e5 + 5;
int n, k;
int r[maxN];
bool earlier_than_0[maxN];
bool only_earlier_than_0[maxN];
int md(int i) {
return (i % n + n) % n;
}
bool in_seg(int x, int l, int r) {
if (l <= r)
return l <= x && x <= r;
return x >= l || x <= r;
}
namespace sgt {
const int logN = 18;
const int N = 1 << logN;
int mn[2][2 * N];
int lazy[2][2 * N];
void apply(int v, int delta0, int delta1) {
mn[0][v] += delta0;
lazy[0][v] += delta0;
mn[1][v] += delta1;
lazy[1][v] += delta1;
}
void push(int v) {
// if (!lazy[0][v] && !lazy[1][v]) return;
apply(v << 1, lazy[0][v], lazy[1][v]);
apply(v << 1 | 1, lazy[0][v], lazy[1][v]);
lazy[0][v] = lazy[1][v] = 0;
}
void upd(int v) {
for (int t = 0; t < 2; ++t)
mn[t][v] = min(mn[t][v << 1], mn[t][v << 1 | 1]);
}
void build(int v, int vl, int vr) {
lazy[0][v] = lazy[1][v] = 0;
if (vl == vr) {
mn[0][v] = mn[1][v] = r[vl];
return;
}
int vm = (vl + vr) >> 1;
build(v << 1, vl, vm);
build(v << 1 | 1, vm + 1, vr);
upd(v);
}
void add(int v, int vl, int vr, int l, int r, int delta0, int delta1) {
if (l > r || vr < l || r < vl) return;
if (l <= vl && vr <= r) {
apply(v, delta0, delta1);
return;
}
int vm = (vl + vr) >> 1;
push(v);
add(v << 1, vl, vm, l, r, delta0, delta1);
add(v << 1 | 1, vm + 1, vr, l, r, delta0, delta1);
upd(v);
}
void disable(int v, int vl, int vr, int i, int t) {
if (vl == vr) {
mn[t][v] = +INF32;
return;
}
int vm = (vl + vr) >> 1;
push(v);
if (i <= vm)
disable(v << 1, vl, vm, i, t);
else
disable(v << 1 | 1, vm + 1, vr, i, t);
upd(v);
}
int getFirstZero(int v, int vl, int vr, int t) {
// debug(v, vl, vr, t, mn[t][v]);
if (mn[t][v] > 0) return -1;
if (vl == vr) return vl;
int vm = (vl + vr) >> 1;
push(v);
if (mn[t][v << 1] == 0)
return getFirstZero(v << 1, vl, vm, t);
return getFirstZero(v << 1 | 1, vm + 1, vr, t);
}
int gt(int v, int vl, int vr, int i, int t) {
if (vl == vr) return mn[t][v];
int vm = (vl + vr) >> 1;
push(v);
if (i <= vm)
return gt(v << 1, vl, vm, i, t);
return gt(v << 1 | 1, vm + 1, vr, i, t);
}
int gt(int i, int t) {
return gt(1, 0, n - 1, i, t);
}
void add(int l, int r, int delta0, int delta1) {
// cerr << "add " << l << ' ' << r << ' ' << delta0 << ' ' << delta1 << endl;
if (l <= r)
add(1, 0, n - 1, l, r, delta0, delta1);
else {
add(1, 0, n - 1, 0, r, delta0, delta1);
add(1, 0, n - 1, l, n - 1, delta0, delta1);
}
}
}
namespace fnw {
int f[maxN];
void add(int i, int delta) {
for (++i; i < maxN; i += i & -i)
f[i] += delta;
}
int pref(int i) {
int ans = 0;
for (++i; i >= 1; i -= i & -i)
ans += f[i];
return ans;
}
int gt(int l, int r) {
if (l <= r) {
return pref(r) - pref(l - 1);
} else {
return pref(r) + pref(n - 1) - pref(l - 1);
}
}
}
}
void dbg() {
using namespace sol;
for (int t = 0; t < 2; ++t) {
cerr << t << ": ";
for (int i = 0; i < n; ++i)
cerr << sgt::gt(i, t) << ' ';
cerr << endl;
}
}
void init(int _k, vector<int> _r) {
using namespace sol;
n = _r.size();
k = _k;
for (int i = 0; i < n; ++i)
r[i] = _r[i];
{
sgt::build(1, 0, n - 1);
for (int i = 0; i < n; ++i)
if (r[i] == 0) {
sgt::add(md(i + 1), md(i + k - 1), 0, +1);
sgt::disable(1, 0, n - 1, i, 0);
}
sgt::add(0, 0, 0, +1);
for (int y = 0; y < n; ++y) {
int i = sgt::getFirstZero(1, 0, n - 1, 1);
if (i == -1) {
break;
}
earlier_than_0[i] = 1;
sgt::disable(1, 0, n - 1, i, 0);
sgt::disable(1, 0, n - 1, i, 1);
sgt::add(md(i + 1), md(i + k - 1), 0, -1);
sgt::add(md(i - k + 1), md(i - 1), -1, -1);
for (int j = sgt::getFirstZero(1, 0, n - 1, 0); j != -1; j = sgt::getFirstZero(1, 0, n - 1, 0)) {
sgt::add(md(j + 1), md(j + k - 1), 0, +1);
sgt::disable(1, 0, n - 1, j, 0);
}
}
// mdebug(earlier_than_0, n);
}
{
sgt::build(1, 0, n - 1);
for (int i = 0; i < n; ++i)
if (r[i] == 0) {
sgt::add(md(i + 1), md(i + k - 1), 0, +1);
sgt::disable(1, 0, n - 1, i, 0);
}
vector<int> bad;
for (int y = 0; y < n; ++y) {
int i;
if (sgt::gt(0, 1) == 0)
i = 0;
else
i = sgt::getFirstZero(1, 0, n - 1, 1);
if (i == 0) {
// debug(bad);
// vector<int> mda(n, 0);
// mda[0] = 1;
fnw::add(0, 1);
for (int t = (int)bad.size() - 1; t >= 0; --t) {
int j = bad[t];
if (fnw::gt(md(j + 1), md(j + k - 1)) || fnw::gt(md(j - k + 1), md(j - 1))) {
only_earlier_than_0[j] = 1;
fnw::add(j, 1);
}
// for (int q = 0; q < n; ++q)
// if (mda[q] && (in_seg(q, md(j + 1), md(j + k - 1)) || in_seg(q, md(j - k + 1), md(j - 1)))) {
// only_earlier_than_0[j] = 1;
// mda[j] = 1;
// break;
// }
}
break;
}
bad.push_back(i);
sgt::disable(1, 0, n - 1, i, 0);
sgt::disable(1, 0, n - 1, i, 1);
sgt::add(md(i + 1), md(i + k - 1), 0, -1);
sgt::add(md(i - k + 1), md(i - 1), -1, -1);
for (int j = sgt::getFirstZero(1, 0, n - 1, 0); j != -1; j = sgt::getFirstZero(1, 0, n - 1, 0)) {
sgt::add(md(j + 1), md(j + k - 1), 0, +1);
sgt::disable(1, 0, n - 1, j, 0);
}
}
}
}
int compare_plants(int x, int y) {
using namespace sol;
assert(x == 0);
if (earlier_than_0[y] && only_earlier_than_0[y])
return -1;
if (earlier_than_0[y])
return 0;
return 1;
}
#ifdef LOCAL
static int n, k, q;
static std::vector<int> r;
static std:: vector<int> x;
static std:: vector<int> y;
static std:: vector<int> answer;
int main() {
freopen("in", "r", stdin);
assert(scanf("%d%d%d", &n, &k, &q) == 3);
r.resize(n);
answer.resize(q);
for (int i = 0; i < n; i++) {
int value;
assert(scanf("%d", &value) == 1);
r[i] = value;
}
x.resize(q);
y.resize(q);
for (int i = 0; i < q; i++) {
assert(scanf("%d%d", &x[i], &y[i]) == 2);
}
fclose(stdin);
init(k, r);
for (int i = 0; i < q; i++) {
answer[i] = compare_plants(x[i], y[i]);
}
for (int i = 0; i < q; i++) {
printf("%d\n", answer[i]);
}
fclose(stdout);
return 0;
}
// int32_t main() {
// #ifdef LOCAL
// freopen("in", "r", stdin);
// #endif
// ios::sync_with_stdio(0);
// cin.tie(0);
// return 0;
// }
#endif
# |
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 |
1 ms |
588 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Runtime error |
2 ms |
588 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Runtime error |
2 ms |
588 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
588 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Runtime error |
2 ms |
588 KB |
Execution killed with signal 6 |
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 |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
5 ms |
332 KB |
Output is correct |
6 |
Correct |
1919 ms |
14580 KB |
Output is correct |
7 |
Correct |
2130 ms |
17728 KB |
Output is correct |
8 |
Correct |
182 ms |
17048 KB |
Output is correct |
9 |
Correct |
2612 ms |
18016 KB |
Output is correct |
10 |
Correct |
1791 ms |
16864 KB |
Output is correct |
11 |
Correct |
759 ms |
17476 KB |
Output is correct |
12 |
Correct |
1214 ms |
17080 KB |
Output is correct |
13 |
Correct |
1809 ms |
17540 KB |
Output is correct |
14 |
Correct |
685 ms |
17072 KB |
Output is correct |
15 |
Correct |
1958 ms |
17668 KB |
Output is correct |
16 |
Correct |
1113 ms |
16956 KB |
Output is correct |
17 |
Correct |
1797 ms |
16952 KB |
Output is correct |
# |
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 |
1 ms |
588 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |