#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int inf=1e9+7;
int n, lt[100001], rt[100001];
map<int, int> del, sum;
stack<int> stk;
struct seg {
int tree[100001*2], sz;
void init(int n) {sz=n;}
void update(int idx, int v) {
for(tree[idx+=sz]+=v; idx>1; idx>>=1) tree[idx>>1]=max(tree[idx], tree[idx^1]);
}
int query(int l, int r) {
int res=0;
for(l+=sz, r+=sz; l<=r; l>>=1, r>>=1) {
if(l&1) res=max(res, tree[l++]);
if(~r&1) res=max(res, tree[r--]);
}
return res;
}
} tree;
void init(int N, vector<int> a) {
n=N;
tree.init(n);
for(int i=0; i<n; i++) tree.update(i, a[i]);
for(int i=0; i<n; i++) {
while(!stk.empty() && a[stk.top()]>a[i]) stk.pop();
lt[i]=(stk.empty() ? -1 : stk.top());
stk.push(i);
}
for(int i=n-1; i>=0; i--) {
while(!stk.empty() && a[stk.top()]>a[i]) stk.pop();
rt[i]=(stk.empty() ? n : stk.top());
stk.push(i);
}
for(int i=0; i<n; i++) {
int curr=inf;
if(lt[i]>=0) curr=min(curr, tree.query(lt[i]+1, i));
if(rt[i]<n) curr=min(curr, tree.query(i, rt[i]-1));
if(curr==inf) {
del[inf]++;
continue;
}
del[curr-a[i]]++;
}
int prv=0;
for(auto it=del.rbegin(); it!=del.rend(); it++) {
prv+=it->second;
sum[it->first]=prv;
}
}
int max_towers(int l, int r, int d) {
return sum.lower_bound(d)->second;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
294 ms |
1872 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '124' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '124' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
596 ms |
5684 KB |
1st lines differ - on the 1st token, expected: '11903', found: '32996' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
121 ms |
1616 KB |
1st lines differ - on the 1st token, expected: '7197', found: '7190' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '124' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
294 ms |
1872 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |