-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasteriantree.cpp
More file actions
53 lines (53 loc) · 1.09 KB
/
casteriantree.cpp
File metadata and controls
53 lines (53 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define bs binary_search
const ll sized = 1e6;
const ll N = 1e7;
const ll inf = 1e9;
const ll MOD = 123456789;
const ll LOG = 20;
int n;
int bang[sized];
int parent[sized];
int D[sized];
int finddepth(int i){
if(D[i]!=-2) return D[i];
D[i]=finddepth(parent[i])+1;
return D[i];
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("RMQTREE.inp","r",stdin);
freopen("RMQTREE.out","w",stdout);
cin>>n;
bang[0]=inf;
for(int i=1;i<=n;i++){
cin>>bang[i];
}
int rightmost=0;// nút
for(int i=0;i<=n;i++){
int x=rightmost;
int y=-1;
while(true){
if(bang[x]>=bang[i]) break;
y=x;
x=parent[x];
}
parent[i]=x;
if(y!=-1) parent[y]=i;
rightmost=i;
}
D[0]=-1;
for(int i=1;i<=n;i++){
D[i]=-2;
}
for(int i=1;i<=n;i++) cout<<finddepth(i)<<" ";
return 0;
}