#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2")
// #pragma GCC target("popcnt")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using lld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int,int>;
using pll = pair<ll, ll>;
using vii = vector<ii>;
using vpll = vector<pll>;
#define endl '\n'
#define all(x) x.begin(),x.end()
#define lsb(x) x&(-x)
#define gcd(a,b) __gcd(a,b)
#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fls cout.flush()
#define fore(i,l,r) for(auto i=l;i<r;i++)
#define fo(i,n) fore(i,0,n)
#define forex(i,r,l) for(auto i=r; i>=l;i--)
#define ffo(i,n) forex(i,n-1,0)
bool cmin(int &a, int b){if(b<a){a=b;return 1;}return 0;}
bool cmax(int &a, int b){if(b>a){a=b;return 1;}return 0;}
void valid(ll in){cout<<((in)?"YES\n":"NO\n");}
ll lcm(ll a, ll b){return (a/gcd(a,b))*b;}
ll gauss(ll n){return (n*(n+1))/2;}
struct SegTree{
vi st;
int n;
SegTree(int n): n(n), st(4*n+4, 0) {}
void update(int i,int x) {update(1,0,n-1,i,x);}
void update(int nodo,int l,int r,int i,int x) {
if(l == r) st[nodo] = x;
else {
int m = (l+r)/2;
if(i <= m) update(nodo*2, l, m, i, x);
else update(nodo*2+1, m+1, r, i, x);
st[nodo] = max(st[nodo*2], st[nodo*2+1]);
}
}
int query(int l,int r) {return query(1, 0, n-1, l, r);}
int query(int nodo,int l,int r,int i,int j){
if(l>j || r<i) return INT_MIN;
if(l>=i && r<=j) return st[nodo];
int m =(l+r)/2;
return max(query(nodo*2, l, m, i, j), query(nodo*2+1, m+1, r, i, j));
}
};
int n;
vi h;
void init(int nn, vi hh){
n = nn;
h = hh;
}
/*
11
5 31 49 51 20 60 40 50 30 52 1
1
0 10 10
7
10 20 60 40 50 30 70
3
1 5 10
2 2 100
0 6 17
*/
int max_towers(int l,int r,int d){
vi ord;
fore(i, l, r+1)ord.pb(h[i]);
sort(all(ord));
auto pos = [&](int a){
return upper_bound(all(ord), a)-ord.begin()-1;
};
SegTree dp1(n+1),dp2(n+1);
fore(i,l,r+1){
int p = pos(h[i]);
// cout << p << '\n';
int dp_i = dp2.query(p, n-1);
// cout << dp_i << '\n';
int wtp = pos(h[i] - d);
// cout << wtp << '\n';
if(wtp >= 0){
int bst = dp1.query(0, wtp) + 1;
// cout << bst << '\n';
if(bst > dp2.query(wtp, wtp))
dp2.update(wtp, bst);
}
// cout << "FIN\n";
dp1.update(p, max(1, dp_i));
}
return dp1.query(0, r-l+1);
}
Compilation message
towers.cpp: In constructor 'SegTree::SegTree(int)':
towers.cpp:40:9: warning: 'SegTree::n' will be initialized after [-Wreorder]
40 | int n;
| ^
towers.cpp:39:8: warning: 'vi SegTree::st' [-Wreorder]
39 | vi st;
| ^~
towers.cpp:41:5: warning: when initialized here [-Wreorder]
41 | SegTree(int n): n(n), st(4*n+4, 0) {}
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4014 ms |
3240 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 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 |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
520 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 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 |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
520 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
19 ms |
3160 KB |
Output is correct |
37 |
Correct |
29 ms |
4536 KB |
Output is correct |
38 |
Correct |
26 ms |
4304 KB |
Output is correct |
39 |
Correct |
24 ms |
4560 KB |
Output is correct |
40 |
Correct |
18 ms |
4304 KB |
Output is correct |
41 |
Correct |
72 ms |
5172 KB |
Output is correct |
42 |
Correct |
11 ms |
4304 KB |
Output is correct |
43 |
Correct |
38 ms |
4816 KB |
Output is correct |
44 |
Correct |
12 ms |
4488 KB |
Output is correct |
45 |
Correct |
10 ms |
4304 KB |
Output is correct |
46 |
Correct |
10 ms |
4304 KB |
Output is correct |
47 |
Correct |
38 ms |
4640 KB |
Output is correct |
48 |
Correct |
27 ms |
4560 KB |
Output is correct |
49 |
Correct |
16 ms |
4280 KB |
Output is correct |
50 |
Correct |
21 ms |
4560 KB |
Output is correct |
51 |
Correct |
32 ms |
4560 KB |
Output is correct |
52 |
Correct |
101 ms |
5072 KB |
Output is correct |
53 |
Correct |
97 ms |
5072 KB |
Output is correct |
54 |
Correct |
97 ms |
5072 KB |
Output is correct |
55 |
Correct |
59 ms |
5072 KB |
Output is correct |
56 |
Correct |
56 ms |
5052 KB |
Output is correct |
57 |
Correct |
68 ms |
5084 KB |
Output is correct |
58 |
Correct |
80 ms |
5064 KB |
Output is correct |
59 |
Correct |
85 ms |
5068 KB |
Output is correct |
60 |
Correct |
98 ms |
5072 KB |
Output is correct |
61 |
Correct |
88 ms |
5072 KB |
Output is correct |
62 |
Correct |
89 ms |
5064 KB |
Output is correct |
63 |
Correct |
87 ms |
5240 KB |
Output is correct |
64 |
Correct |
42 ms |
5072 KB |
Output is correct |
65 |
Correct |
40 ms |
5072 KB |
Output is correct |
66 |
Correct |
49 ms |
5072 KB |
Output is correct |
67 |
Correct |
42 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4059 ms |
5108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4027 ms |
1624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 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 |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
520 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
19 ms |
3160 KB |
Output is correct |
37 |
Correct |
29 ms |
4536 KB |
Output is correct |
38 |
Correct |
26 ms |
4304 KB |
Output is correct |
39 |
Correct |
24 ms |
4560 KB |
Output is correct |
40 |
Correct |
18 ms |
4304 KB |
Output is correct |
41 |
Correct |
72 ms |
5172 KB |
Output is correct |
42 |
Correct |
11 ms |
4304 KB |
Output is correct |
43 |
Correct |
38 ms |
4816 KB |
Output is correct |
44 |
Correct |
12 ms |
4488 KB |
Output is correct |
45 |
Correct |
10 ms |
4304 KB |
Output is correct |
46 |
Correct |
10 ms |
4304 KB |
Output is correct |
47 |
Correct |
38 ms |
4640 KB |
Output is correct |
48 |
Correct |
27 ms |
4560 KB |
Output is correct |
49 |
Correct |
16 ms |
4280 KB |
Output is correct |
50 |
Correct |
21 ms |
4560 KB |
Output is correct |
51 |
Correct |
32 ms |
4560 KB |
Output is correct |
52 |
Correct |
101 ms |
5072 KB |
Output is correct |
53 |
Correct |
97 ms |
5072 KB |
Output is correct |
54 |
Correct |
97 ms |
5072 KB |
Output is correct |
55 |
Correct |
59 ms |
5072 KB |
Output is correct |
56 |
Correct |
56 ms |
5052 KB |
Output is correct |
57 |
Correct |
68 ms |
5084 KB |
Output is correct |
58 |
Correct |
80 ms |
5064 KB |
Output is correct |
59 |
Correct |
85 ms |
5068 KB |
Output is correct |
60 |
Correct |
98 ms |
5072 KB |
Output is correct |
61 |
Correct |
88 ms |
5072 KB |
Output is correct |
62 |
Correct |
89 ms |
5064 KB |
Output is correct |
63 |
Correct |
87 ms |
5240 KB |
Output is correct |
64 |
Correct |
42 ms |
5072 KB |
Output is correct |
65 |
Correct |
40 ms |
5072 KB |
Output is correct |
66 |
Correct |
49 ms |
5072 KB |
Output is correct |
67 |
Correct |
42 ms |
5072 KB |
Output is correct |
68 |
Execution timed out |
4059 ms |
5108 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4014 ms |
3240 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |