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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll,ll> pi;
typedef map<ll,ll> mi;
typedef long double ld;
typedef vector<ld> vd;
typedef vector<vector<ld>> vvd;
typedef pair<ld,ld> pd;
#define ff first
#define ss second
#define srt(a) sort(a.begin(),a.end());
#define fip(k, n) for (ll i = k; i < n; i++)
#define fjp(k, n) for (ll j = k; j < n; j++)
#define fin(k, n) for (ll i = k; i >= n; i--)
#define fjn(k, n) for (ll j = k; j >= n; j--)
#define fp(k, n, m) for (ll k = n; k < m; k++)
#define fn(k, n, m) for (ll k = n; k >= m; k--)
#define ordered_set tree<pi, null_type,less< pi >, rb_tree_tag,tree_order_statistics_node_update>
#define totalOne(n) __builtin_popcount(n)
#define backZero(n) __builtin_ctzll(n)
#define frontZero(n) __builtin_clzll(n)
#define fx(k) for ( auto x : k )
#define test ll t;cin >> t;while (t--)
#define nli "\n"
// ==========================(debug)============================================================================================== //
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _printn(x); cerr << nli;
#else
#define debug(x)
#endif
void _printn(ll x){ cerr<<x<<" "; }
void _printn(int x){ cerr<<x<<" "; }
void _printn(ld x){ cerr<<x<<" "; }
void _printn(double x){ cerr<<x<<" "; }
void _printn(string x){ cerr<<x<<" "; }
void _printn(char x){ cerr<<x<<" "; }
void _printn(bool x){ cerr<<x<<" "; }
template<class T,class V>void _printn(pair<T,V> vv);
template<class T> void _printn(vector<T> vv);
template<class T> void _printn(set<T> vv);
template<class T,class V> void _printn(map<T,V> vv);
template<class T> void _printn(multiset<T> vv);
template<class T,class V>void _printn(pair<T,V> vv){ cerr<<"( ";_printn(vv.ff);cerr<<",";_printn(vv.ss);cerr<<")";}
template<class T> void _printn(vector<T> vv){ cerr<<"[ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"]"; };
template<class T> void _printn(set<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T> void _printn(multiset<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T,class V> void _printn(map<T,V> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
// ==========================(debug)============================================================================================== //
ll n,m,tp,tp2,res,cnt,sum,tptp,ans;
const ll mx = 2e5+5;
const ll mod = 1e9+7;
// ==========================(MOD)=============================================================================================== //
ll mod_add(ll aa,ll bb){ return ((aa%mod)+(bb%mod))%mod; }
ll mod_minus(ll aa,ll bb){ return (((aa%mod)-(bb%mod))+10*mod)%mod; }
ll mod_mul(ll aa,ll bb){ return ((aa%mod)*(bb%mod))%mod; }
ll mod_power(ll aa,ll bb){ aa%=mod; ll empowered = 1; bb%=mod-1; while(bb > 0){ if(bb & 1) empowered = mod_mul(empowered,aa); bb = bb >> 1; aa = mod_mul(aa,aa); } return empowered; }
ll mod_divi(ll aa,ll bb){ aa=mod_mul(aa,mod_power(bb,mod-2)); return aa; }
// ==========================(MOD)=============================================================================================== //
bool f = false;
ll hld[mx];
ll dep[mx];
ll a[mx];
vi adj[mx];
ll sz[mx];
ll to[mx];
ll parent[mx];
ll sr,de;
vector<pi> lis[mx];
typedef struct Binary_indexed_tree{
vi arr;
ll range;
ll sm = 0;
void init(ll k){
range = log2(k);
range = 1<<(k+1);
arr.resize(range);
fip(0,range)
arr[i] = 0;
return;
}
ll get(ll l){
sm = 0;
for(ll i = l ; i > 0; i -= (i & -i))
sm += arr[i];
return sm;
}
void update(ll l,ll val){
for(ll i = l ; i < range; i += (i & -i))
arr[i] += val;
return ;
}
}BIT;
BIT bit;
void hld_dfs(ll u,ll par,ll hd){
hld[u] = hd;
if(to[u] != -1)
hld_dfs(to[u],u,hd);
fx(adj[u]){
if(x==par || x ==to[u])
continue;
hld_dfs(x,u,x);
}
return;
}
void dfs(ll u,ll par){
parent[u] = par;
sz[u] = 1;
ll tc=-1,tx=-1;
fx(adj[u])
if(x!=par){
dep[x] = dep[u] + 1;
dfs(x,u);
sz[u] += sz[x];
if(sz[x] > tc){
tc = sz[x];
tx = x;
}
}
to[u] = tx;
return;
}
void init(ll k){
fip(0,n+2){
dep[i] = 0;
to[i] = -1;
hld[i] = -1;
sz[i] = 0;
}
return ;
}
ll aa,bb,range,let;
ll calc(ll u,ll v){
vector<pi> na;
vector<pi> v2;
u = v;
while(u != -1){
aa = u;
let = 0;
bb = hld[aa];
range = dep[aa] - dep[bb] + 1;
vector<pi> &ss = lis[bb];
v2.clear();
while(ss.size() && ss.back().ss <= range){
v2.push_back({ss.back().ff,ss.back().ss-let});
let = ss.back().ss;
ss.pop_back();
}
if(ss.size())
v2.push_back({ss.back().ff,range - let});
ss.push_back({a[v],range});
u = parent[bb];
fin((int)v2.size()-1,0)
na.push_back(v2[i]);
}
res = 0;
m = na.size();
vi vv;
fx(na)
vv.push_back(x.ff);
srt(vv);
// debug(na);
fip(0,m)
na[i].ff = (lower_bound(vv.begin(),vv.end(),na[i].ff) - vv.begin()) + 1;
bit.init(m);
fx(na){
res += (x.ss*bit.get(x.ff-1));
bit.update(x.ff,x.ss);
}
return res;
}
vector<pi> ve;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input1.txt", "r", stdin);
// freopen("output1.txt", "w", stdout);
// freopen("error1.txt", "w", stderr);
// #endif // ONLINE_JUDGE
cin>>n;
init(n);
fip(1,n+1)
cin>>a[i];
fip(1,n){
cin>>sr>>de;
ve.push_back({sr,de});
adj[sr].push_back(de);
adj[de].push_back(sr);
}
dfs(1,-1);
hld_dfs(1,1,1);
lis[1].push_back({a[1],1});
fx(ve){
// fip(1,n+1){
// debug(lis[i]);
// }
cout<<calc(x.ff,x.ss)<<nli;
}
cerr << "Time elapsed: " << setprecision(6) << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |