#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
#define tol(bi) ((1LL)<<((int)(bi)))
constexpr int MAXN = 1e5;
constexpr int LOG = 20;
int st[MAXN*4][LOG], tin[MAXN], rev[MAXN*4];
vector<int> tree[MAXN];
map<int,int> hsh;
vector<int> H;
int query(int l, int r){
int ara = r-l+1;
int lg = log2(ara);
return min(st[l][lg],st[r-tol(lg)+1][lg]);
}
int lca(int a, int b){
if (tin[a]>tin[b]) swap(a,b);
return rev[query(tin[a],tin[b])];
}
int ind;
void dfs(int node){
st[ind][0]=ind;
rev[ind]=node;
tin[node]=ind++;
for (auto it : tree[node]){
dfs(it);
st[ind++][0]=tin[node];
}
}
bool sub1 = false;
void init(int N, std::vector<int> H) {
::H=H;
ind=0;
int inc = 0;
int dec = N-1;
for (int i = 1; i < N; i++){
if (H[i]>H[i-1]) inc++;
else break;
}
for (int i = 1; i < N; i++){
if (H[i]<H[i+1]) dec--;
else break;
}
if (inc==dec) sub1=true;
for (int i = 0; i < N; i++){
hsh[H[i]]=i;
st[i][0]=-H[i];
}
for (int j = 1; j < LOG; j++){
for (int i = 0; i < N; i++){
st[i][j]=-1;
if (st[i][j-1]==-1 || i+tol(j-1)>=N) continue;
st[i][j]=min(st[i][j-1],st[i+tol(j-1)][j-1]);
}
}
function<int(int,int)> partition = [&](int l, int r)->int{
if (l==r) return l;
int ele = hsh[-query(l,r)];
tree[ele].clear();
if (l<=ele-1) tree[ele].push_back(partition(l,ele-1));
if (ele+1<=r) tree[ele].push_back(partition(ele+1,r));
return ele;
};
int root = partition(0,N-1);
dfs(root);
for (int j = 1; j < LOG; j++){
for (int i = 0; i < ind; i++){
st[i][j]=-1;
if (st[i][j-1]==-1 || i+tol(j-1)>=ind) continue;
st[i][j]=min(st[i][j-1],st[i+tol(j-1)][j-1]);
}
}
}
int max_towers(int L, int R, int D) {
int ara = lca(L,R);
if (ara==L || ara==R) return 1;
if (max(H[L],H[R])<=H[ara]-D) return 2;
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
24680 KB |
Output is correct |
2 |
Correct |
516 ms |
38844 KB |
Output is correct |
3 |
Correct |
504 ms |
38960 KB |
Output is correct |
4 |
Correct |
469 ms |
39500 KB |
Output is correct |
5 |
Correct |
487 ms |
39260 KB |
Output is correct |
6 |
Correct |
484 ms |
39268 KB |
Output is correct |
7 |
Correct |
499 ms |
39504 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6744 KB |
1st lines differ - on the 1st token, expected: '13', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6744 KB |
1st lines differ - on the 1st token, expected: '13', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
444 ms |
28992 KB |
1st lines differ - on the 1st token, expected: '11903', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
164 ms |
10584 KB |
1st lines differ - on the 1st token, expected: '7197', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6744 KB |
1st lines differ - on the 1st token, expected: '13', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
24680 KB |
Output is correct |
2 |
Correct |
516 ms |
38844 KB |
Output is correct |
3 |
Correct |
504 ms |
38960 KB |
Output is correct |
4 |
Correct |
469 ms |
39500 KB |
Output is correct |
5 |
Correct |
487 ms |
39260 KB |
Output is correct |
6 |
Correct |
484 ms |
39268 KB |
Output is correct |
7 |
Correct |
499 ms |
39504 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7000 KB |
Output is correct |
11 |
Incorrect |
1 ms |
6744 KB |
1st lines differ - on the 1st token, expected: '13', found: '1' |
12 |
Halted |
0 ms |
0 KB |
- |