#pragma GCC optimize("Ofast")
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+5;
const int SZ = 512;
const int inf = 1e9;
int R[MAXN];
int n;
typedef pair<int, int> pii;
pii operator+(pii a, pii b) {
return pii(a.first+b.first, a.second+b.second);
}
class stree {
public:
// int lp, rp;
// stree *l = nullptr;
// stree *r = nullptr;
int mn[2*SZ];
int lz[2*SZ];
stree() {}
void reset(bool tp) {
memset(lz, 0, sizeof(int)*2*SZ);
for (int i = SZ; i < 2*SZ; i++) {
mn[i] = (tp && i < SZ+n) ? R[i-SZ] : inf;
}
for (int i = SZ-1; i > 0; i--) mn[i] = min(mn[2*i], mn[2*i+1]);
}
void activate(int p, int cur = 1, int lp = 0, int rp = SZ-1) {
if (lp > p || rp < p) return;
if (lp == rp) {
mn[cur] = lz[cur];
return;
}
prop(cur);
int md = (lp+rp)/2;
activate(p, 2*cur, lp, md);
activate(p, 2*cur+1, md+1, rp);
mn[cur] = min(mn[2*cur], mn[2*cur+1]);
}
void set_lz(int cur, int p) {
lz[cur] += p;
mn[cur] += p;
}
void prop(int cur) {
assert(cur < SZ);
set_lz(2*cur, lz[cur]);
set_lz(2*cur+1, lz[cur]);
lz[cur] = 0;
}
int find(int cur = 1, int lp = 0, int rp = SZ-1) { // finds a position that is 0
if (mn[cur] > 0) return -1;
if (lp == rp) {
mn[cur] = inf;
return lp;
}
prop(cur);
int md = (lp+rp)/2;
int lq = find(2*cur, lp, md);
if (lq >= 0) {
mn[cur] = min(mn[2*cur], mn[2*cur+1]);
return lq;
}
int v = find(2*cur+1, md+1, rp);
assert(v >= 0);
mn[cur] = min(mn[2*cur], mn[2*cur+1]);
return v;
}
// void del(int p, int cur = 1, int lp = 0, int rp = SZ-1) { // can be absorbed into find
// if (lp > p || rp < p) return;
// if (lp == rp) {
// mn[cur] = inf;
// return;
// }
// prop(cur);
// int md = (lp+rp)/2;
// del(p, 2*cur, lp, md); del(p, 2*cur+1, md+1, rp);
// mn[cur] = min(mn[2*cur], mn[2*cur+1]);
// }
void upd(int lv, int rv, int v, int cur = 1, int lp = 0, int rp = SZ-1) {
if (lp > rv || rp < lv) return;
if (lp >= lv && rp <= rv) {
set_lz(cur, v);
return;
}
prop(cur);
int md = (lp+rp)/2;
upd(lv, rv, v, 2*cur, lp, md);
upd(lv, rv, v, 2*cur+1, md+1, rp);
mn[cur] = min(mn[2*cur], mn[2*cur+1]);
}
};
stree *tree[2];
int k;
void init(int K, vector<int> r) {
k = K;
n = r.size();
assert(n <= 300);
for (int i = 0; i < n; i++) {
R[i] = r[i];
}
tree[0] = new stree(); // 0s to the left
tree[1] = new stree(); // rval
}
bool before(int x, int y) { // could x be taller than y
tree[0]->reset(0);
tree[1]->reset(1);
for (int i = 0; i < n; i++) {
int f1;
while ((f1 = tree[1]->find()) >= 0) {
tree[0]->activate(f1);
// tree[1]->del(f1);
tree[0]->upd(f1+1, min(n-1, f1+k-1), 1);
if (f1+k-1 >= n) tree[0]->upd(0, f1+k-1-n, 1);
}
int v = tree[0]->find();
// tree[0]->del(v);
if (v == -1) return 0;
if (v == x) return 1;
if (v == y) {
continue;
}
tree[0]->upd(v+1, min(n-1, v+k-1), -1);
tree[0]->upd(0, v+k-1-n, -1);
tree[1]->upd(max(0, v-k+1), v-1, -1);
if (v-k+1 < 0) tree[1]->upd(n+v-k+1, n-1, -1);
}
assert(false);
}
int compare_plants(int x, int y) {
bool a = before(x, y);
bool b = !before(y, x);
// delete tree[0];
// delete tree[1];
return a+b-1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2228 ms |
3060 KB |
Output is correct |
7 |
Runtime error |
31 ms |
5436 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
20 ms |
328 KB |
Output is correct |
6 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 6 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
20 ms |
328 KB |
Output is correct |
6 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 6 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Runtime error |
31 ms |
5220 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
324 KB |
Output is correct |
6 |
Correct |
211 ms |
372 KB |
Output is correct |
7 |
Execution timed out |
4046 ms |
852 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2228 ms |
3060 KB |
Output is correct |
7 |
Runtime error |
31 ms |
5436 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |