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>
using namespace std;
#include "towers.h"
typedef int ll;
int const M = 1e5 + 10 , lg = 32 , Size = M * lg;
int n , s[M] , dp[M];
void init(int N, vector<ll> H) {
n = N;
for(int i = 1 ; i <= n ; ++i) s[i] = H[i - 1];
}
struct sgtree {
int cur = 1 , type;
int Left[Size] , Right[Size] , mx[Size];
void Init(int t) {
type = t;
for(int i = 0 ; i < Size ; ++i) Left[i] = Right[i] = mx[i] = 0;
}
inline void UpdateRange(int l , int r , int val , int node = 1 , int lx = 1 , int rx = 1e9) {
if(lx > r || rx < l) return;
if(lx >= l && rx <= r) {
mx[node] = max(mx[node] , val);
return;
}
int mid = (lx + rx) / 2;
if(Left[node] == 0) Left[node] = ++cur;
if(Right[node] == 0) Right[node] = ++cur;
Update(l , r , val , Left[node] , lx , mid);
Update(l , r , val , Right[node] , mid + 1 , rx);
}
inline void Update(int l , int r , int val , int node = 1 , int lx = 1 , int rx = 1e9) {
if(lx > r || rx < l) return;
if(lx >= l && rx <= r) {
mx[node] = max(mx[node] , val);
return;
}
int mid = (lx + rx) / 2;
if(Left[node] == 0) Left[node] = ++cur;
if(Right[node] == 0) Right[node] = ++cur;
Update(l , r , val , Left[node] , lx , mid);
Update(l , r , val , Right[node] , mid + 1 , rx);
mx[node] = max(mx[Left[node]] , mx[Right[node]]);
}
inline ll GetMax(int l , int r , int node = 1 , int lx = 1 , int rx = 1e9) {
if(lx > r || rx < l || node == 0) return 0;
if(lx >= l && rx <= r) return mx[node];
int mid = (lx + rx) / 2;
int res = max(GetMax(l , r , Left[node] , lx , mid) , GetMax(l , r , Right[node] , mid + 1 , rx));
if(type == 1) res = max(res , mx[node]);
return res;
}
} Tree[2];
int max_towers(int L, int R, int D) {
Tree[0].Init(0);
Tree[1].Init(1);
++L , ++R;
int ans = 1;
for(int i = L ; i <= R ; ++i) {
dp[i] = 1 + Tree[1].GetMax(s[i] , s[i]);
if(s[i] - D >= 1) {
int MaxLeft = Tree[0].GetMax(1 , s[i] - D);
//cout << "i = " << i << " MaxLeft = " << MaxLeft << endl;
Tree[1].UpdateRange(1 , s[i] - D , MaxLeft);
}
Tree[0].Update(s[i] , s[i] , dp[i]);
ans = max(ans , dp[i]);
//cout << "i = " << i << " " << dp[i] << endl;
}
return ans;
}
# | 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... |