#include "towers.h"
#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
#ifdef DEBUG
auto& operator<<(auto &os, const pair<auto,auto> &p) {
return os<<"("<<p.first<<", "<<p.second<<")";
}
auto& operator<<(auto &os, const auto &v) {
os<<"{";
for(auto it=begin(v);it!=end(v);++it) {
if(it != begin(v)) os<<", ";
os<<(*it);
}
return os<<"}";
}
void dbg_out(auto... x) {
((cerr<<' '<<x), ...) << endl;
}
#define dbg(x...) cerr<<"("<<#x<<"):", dbg_out(x);
#else
#define dbg(...)
#endif
struct SegTree {
int n;
vi tree;
vi ind;
SegTree(vi vals) {
n = sz(vals);
tree.resize(2*n);
ind.resize(2*n);
rep(i,0,n) {
tree[n+i] = vals[i];
ind[n+i] = i;
}
for(int i=n-1;i>0;i--) {
tree[i] = max(tree[i*2], tree[i*2+1]);
ind[i] = tree[i] == tree[i*2] ? ind[i*2] : ind[i*2+1];
}
}
int query(int l, int r) {
dbg(l, r);
int res = 0;
int index = -1;
l += n;
r += n;
for(;l < r;l /= 2, r /= 2) {
dbg(l, r);
if(l&1) {
if(tree[l] > res) {
res = tree[l];
index = ind[l];
}
l++;
}
if(r&1) {
--r;
if(tree[r] > res) {
res = tree[r];
index = ind[r];
}
}
}
return index;
}
};
const int inf = 1e9 + 7;
SegTree *tree;
int n, root;
vi h;
vi L, R, siz;
int get_max(int l, int r) {
return tree->query(l, r);
}
int build_tree(int l, int r) {
if(l == r) return -1;
if(l+1 == r) {
siz[l] = 1;
return l;
}
int index = get_max(l, r);
L[index] = build_tree(l, index);
R[index] = build_tree(index+1, r);
siz[index] = (L[index] == -1 ? 0 : siz[L[index]])
+(R[index] == -1 ? 0 : siz[R[index]]);
return index;
}
void init(int N, vi H) {
n = N;
h = H;
L.resize(n);
R.resize(n);
siz.assign(n, 0);
tree = new SegTree(h);
root = build_tree(0, n);
dbg(root);
dbg(L);
dbg(R);
}
int solve(int v, int l, int r, int a, int b, int d, int maxi) {
if(l == r || r <= a || b <= l) return 0;
dbg(v, l, r, a, b, d, maxi);
if(l+1 == r) return h[v] <= maxi;
int res = 0;
if(L[v] == -1 || v < a)
res += solve(R[v], v+1, r, a, b, d, maxi);
else if(R[v] == -1 || v >= b)
res += solve(L[v], l, v, a, b, d, maxi);
else {
res += solve(L[v], l , v, a, b, d, min(maxi, h[v] - d));
res += solve(R[v], v+1, r, a, b, d, min(maxi, h[v] - d));
res = max(res, solve(R[v], v+1, r, a, b, d, maxi));
res = max(res, solve(L[v], l, v, a, b, d, maxi));
}
if(res == 0 && (a <= v && v < b && h[v] <= maxi))
return 1;
return res;
}
int max_towers(int L, int R, int D) {
int res = solve(root, 0, n, L, R+1, D, inf);
return res == 0 ? 1 : res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4085 ms |
7864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
20 ms |
516 KB |
Output is correct |
3 |
Correct |
24 ms |
344 KB |
Output is correct |
4 |
Correct |
569 ms |
344 KB |
Output is correct |
5 |
Correct |
123 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
28 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
103 ms |
344 KB |
Output is correct |
17 |
Correct |
9 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
103 ms |
344 KB |
Output is correct |
21 |
Correct |
390 ms |
344 KB |
Output is correct |
22 |
Correct |
176 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
52 ms |
344 KB |
Output is correct |
26 |
Correct |
82 ms |
344 KB |
Output is correct |
27 |
Correct |
610 ms |
344 KB |
Output is correct |
28 |
Correct |
709 ms |
344 KB |
Output is correct |
29 |
Correct |
662 ms |
344 KB |
Output is correct |
30 |
Correct |
2344 ms |
496 KB |
Output is correct |
31 |
Correct |
334 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
20 ms |
516 KB |
Output is correct |
3 |
Correct |
24 ms |
344 KB |
Output is correct |
4 |
Correct |
569 ms |
344 KB |
Output is correct |
5 |
Correct |
123 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
28 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
103 ms |
344 KB |
Output is correct |
17 |
Correct |
9 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
103 ms |
344 KB |
Output is correct |
21 |
Correct |
390 ms |
344 KB |
Output is correct |
22 |
Correct |
176 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
52 ms |
344 KB |
Output is correct |
26 |
Correct |
82 ms |
344 KB |
Output is correct |
27 |
Correct |
610 ms |
344 KB |
Output is correct |
28 |
Correct |
709 ms |
344 KB |
Output is correct |
29 |
Correct |
662 ms |
344 KB |
Output is correct |
30 |
Correct |
2344 ms |
496 KB |
Output is correct |
31 |
Correct |
334 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Execution timed out |
4038 ms |
3160 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4038 ms |
4696 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4061 ms |
1368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
20 ms |
516 KB |
Output is correct |
3 |
Correct |
24 ms |
344 KB |
Output is correct |
4 |
Correct |
569 ms |
344 KB |
Output is correct |
5 |
Correct |
123 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
28 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
103 ms |
344 KB |
Output is correct |
17 |
Correct |
9 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
103 ms |
344 KB |
Output is correct |
21 |
Correct |
390 ms |
344 KB |
Output is correct |
22 |
Correct |
176 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
52 ms |
344 KB |
Output is correct |
26 |
Correct |
82 ms |
344 KB |
Output is correct |
27 |
Correct |
610 ms |
344 KB |
Output is correct |
28 |
Correct |
709 ms |
344 KB |
Output is correct |
29 |
Correct |
662 ms |
344 KB |
Output is correct |
30 |
Correct |
2344 ms |
496 KB |
Output is correct |
31 |
Correct |
334 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Execution timed out |
4038 ms |
3160 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4085 ms |
7864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |