#include #include using namespace std; //1-indexed #include template struct BIT{ int n; vectorbit; BIT(int n_=0,T a=0):n(n_),bit(n_+1,a){} T sum(int i) { T ans=0; for(;i>0;i-=i&-i)ans+=bit[i]; return ans; } void add(int i,T a) { if(i==0)return; for(;i<=n;i+=i&-i)bit[i]+=a; } }; int n,a[2<<17]; vectorG[2<<17]; vectort; void dfs(int u) { t.push_back(u+1); for(int i=0;i>n; for(int i=1;i>a[i]; G[a[i]].push_back(i); } dfs(0); BITP(n); long ans=0; for(int i=0;i