This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;}
const int LOG = 20;
struct SparseTable{
vector<vii> sp;
int n;
SparseTable() {}
SparseTable(int n): n(n), sp(n+3, vii(LOG, mp(0,0))) {}
ii merge(ii a, ii b){
return {min(a.fi, b.fi), max(a.se,b.se)};
}
void build(){
fore(b, 1, LOG) {
fo(i, n-(1<<b)+1) {
sp[i][b] = merge(sp[i][b-1], sp[i+(1<<(b-1))][b-1]);
}
}
}
ii query(int l,int r) {
int lg = log2(r-l+1);
return merge(sp[l][lg], sp[r-(1<<lg)+1][lg]);
}
};
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;
SparseTable qs;
void init(int nn, vi hh){
n = nn;
h = hh;
qs = SparseTable(n);
fo(i, n) qs.sp[i][0] = {h[i], h[i]};
qs.build();
}
int max_towers(int l,int r,int d){
SegTree dp1(n+1), dp2(n+1);
int ans = 0;
fore(i, l, r+1){
// cout << "Indice " << i << '\n';
dp1.update(i, max(1, dp2.query(i, r)));
// cout << dp1.query(i, i) << '\n';
int lb = l, rb = i-1;
while(lb <= rb){
int mb = (lb + rb)/2;
ii mx = qs.query(mb, i-1);
if(mx.se > h[i]-d) lb = mb+1;
else rb = mb-1;
}
int bst = -1e9;
if(lb < i) bst = dp1.query(lb, i-1) + 1;
lb = i+1, rb = r;
while(lb <= rb) {
int mb = (lb + rb)/2;
ii mx = qs.query(i+1, mb);
if(mx.se > h[i]-d) rb = mb-1;
else lb = mb+1;
}
if(rb > i && dp2.query(rb,rb) < bst) dp2.update(rb, bst);
}
return dp1.query(l, r);
}
// void test_case(){
// int n;
// cin >> n;
// vi h(n);
// fo(i, n) cin >> h[i];
// init(n, h);
// int Q;
// cin >> Q;
// while(Q--) {
// int l, r, d;
// cin >> l >> r >> d;
// cout << max_towers(l, r, d) << "\n";
// }
// }
// int main(){cin.tie(0)->sync_with_stdio(0);
// int t=1;
// // cin >> t;
// while(t--)test_case();
// }
Compilation message (stderr)
towers.cpp: In constructor 'SparseTable::SparseTable(int)':
towers.cpp:41:9: warning: 'SparseTable::n' will be initialized after [-Wreorder]
41 | int n;
| ^
towers.cpp:40:17: warning: 'std::vector<std::vector<std::pair<int, int> > > SparseTable::sp' [-Wreorder]
40 | vector<vii> sp;
| ^~
towers.cpp:43:5: warning: when initialized here [-Wreorder]
43 | SparseTable(int n): n(n), sp(n+3, vii(LOG, mp(0,0))) {}
| ^~~~~~~~~~~
towers.cpp: In constructor 'SegTree::SegTree(int)':
towers.cpp:61:9: warning: 'SegTree::n' will be initialized after [-Wreorder]
61 | int n;
| ^
towers.cpp:60:8: warning: 'vi SegTree::st' [-Wreorder]
60 | vi st;
| ^~
towers.cpp:62:5: warning: when initialized here [-Wreorder]
62 | SegTree(int n): n(n), st(4*n+4, 0) {}
| ^~~~~~~
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:93:9: warning: unused variable 'ans' [-Wunused-variable]
93 | int ans = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |