#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define rrep(i,a,b) for(int i=int(a);i>int(b);i--)
#define trav(a,v) for(auto& a: v)
#define sz(v) v.size()
#define all(v) v.begin(),v.end()
#define vi vector<int>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int inf = -1e9;
using namespace std;
vector<ll> sorted(3e5);
static const int infint = 1e9;
struct Node {
Node* l = 0, * r = 0;
int lo, hi, madd = 0, mset = inf;
pair<int, int> val = make_pair(-inf, -inf);
Node(int lo, int hi) :lo(lo), hi(hi) {} // Large interval of -inf
Node(vector<pair<int, int>>& v, int lo, int hi) : lo(lo), hi(hi) {
if (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
l = new Node(v, lo, mid); r = new Node(v, mid, hi);
val = min(l->val, r->val);
}
else val = v[lo];
}
pair<int, int> query(int L, int R) {
if (R <= lo || hi <= L) return make_pair(-inf, -inf);
if (L <= lo && hi <= R) return val;
push();
return min(l->query(L, R), r->query(L, R));
}
void set(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) mset = val.first = x, madd = 0;
else {
push(), l->set(L, R, x), r->set(L, R, x);
val = min(l->val, r->val);
}
}
void add(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) {
if (mset != inf) mset += x;
else madd += x;
val.first += x;
}
else {
push(), l->add(L, R, x), r->add(L, R, x);
val = min(l->val, r->val);
}
}
void push() {
if (!l) {
int mid = lo + (hi - lo) / 2;
l = new Node(lo, mid); r = new Node(mid, hi);
}
if (mset != inf)
l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
else if (madd)
l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
}
};
ll counter = 0;
vector<vector<ll>> g(302);
vector<bool> visited(302);
vector<bool> visited0(302);
bool dfs(ll cur,ll goal) {
if (visited[cur])return cur == goal;
visited[cur] = 1;
if (cur == goal)return 1;
bool ans = 0;
trav(v, g[cur]) {
ans = ans || dfs(v, goal);
}
return ans;
}
void extract(Node*& tree, ll cur, ll k, ll n) {
if (cur - k + 1 < 0) {
while (!tree->query(0, cur).first) {
extract(tree, tree->query(0, cur).second, k, n);
}
while (!tree->query(n - (k - 1 - cur), n).first) {
extract(tree, tree->query(n - (k - 1 - cur), n).second, k, n);
}
tree->add(0, cur, -1);
tree->add(n - (k - 1 - cur), n, -1);
rep(j, (n - (k - 1 - cur)), n) {
if (tree->query(j, j + 1).first < 1e6)g[cur].push_back(j);
}
rep(j, 0, cur) {
if (tree->query(j, j + 1).first < 1e6)g[cur].push_back(j);
}
}
else {
while (!tree->query(cur - k + 1, cur).first) {
extract(tree, tree->query(cur - k + 1, cur).second, k, n);
}
tree->add(cur - k + 1, cur, -1);
rep(j, cur - k + 1, cur) {
if (tree->query(j, j + 1).first < 1e6)g[cur].push_back(j);
}
}
rep(j, cur + 1, cur + k) {
if (tree->query(j, j + 1).first < 1e6)g[cur].push_back(j % n);
}
tree->set(cur, cur + 1, infint);
return;
}
void init(int k, std::vector<int> r) {
ll n = r.size();
ll indx = 0;
vector<pair<int, int>> v;
rep(i, 0, n)v.emplace_back(r[i], i);
Node* tree = new Node(v, 0, n);
rep(i, 0, n) {
pair<int, int> val = tree->query(0, n);
if (val.first == 0)extract(tree, val.second, k, n);
else break;
}
}
int compare_plants(int x, int y) {
visited = visited0;
if (dfs(x,y))return 1;
else if (dfs(y,x)) return -1;
return 0;
}
Compilation message
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:118:5: warning: unused variable 'indx' [-Wunused-variable]
118 | ll indx = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2664 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
99 ms |
6396 KB |
Output is correct |
7 |
Runtime error |
55 ms |
16576 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2656 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
3 |
Correct |
2 ms |
2660 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2656 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
3 |
Correct |
2 ms |
2660 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2660 KB |
Output is correct |
3 |
Runtime error |
54 ms |
10692 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2660 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2664 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
99 ms |
6396 KB |
Output is correct |
7 |
Runtime error |
55 ms |
16576 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |