This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://www.instagram.com/_modwwe/
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2")
#include<bits/stdc++.h>
#define int long long
//#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans ","w",stdout)
#define pb push_back
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void phongbeo();
const int inf=1e18;
const int mod2=998244353;
const int mod1=998244353;
struct icd
{
int a,b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a,b,c,d,e,f;
};
int n,m,s1,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,l;
int i,s10,s12;
int kk;
int el=29;
main()
{
#ifndef ONLINE_JUDGE
// fin(task),fou(task);
#endif
NHP
/// cin>>s1;
// modwwe
phongbeo();
}
int st[100001];
int pos[100001];
int heavy[100001];
int head[100001];
int b[100001];
int a[100001];
vector<int> v[100001];
vector<ib> vv;
int t[400001];
int bit[100001];
int t2[400001];
int dfs(int x,int y)
{
st[x]=y;
int sz=1;
int mx_sz=0;
for(auto f:v[x])
{
if(f!=y)
{
int s=dfs(f,x);
sz+=s;
if(s>mx_sz)
mx_sz=s,heavy[x]=f;
}
}
return sz;
}
void descompose(int x,int y)
{
pos[x]=++dem;
head[x]=y;
if(heavy[x]!=0)
descompose(heavy[x],y);
for(auto f:v[x])
if(pos[f]==0)
descompose(f,f);
}
void upd_fenwick(int x,int y)
{
for(x;x<=n;x+=x&-x)
bit[x]+=y;
}
int get_fenwick(int x)
{
int s=0;
for(x;x;x-=x&-x)s+=bit[x];
return s;
}
void ff(int x)
{
for(int i=x*2; i<=x*2+1; i++)
t[i]=t2[x],
t2[i]=t2[x];
t2[x]=0;
return;
}
void upd(int node,int l,int r,int l1,int r1,int x)
{
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1&&t[node])
{
s2+=get_fenwick(t[node]-1)*(r-l+1);
if(l!=1)
upd_fenwick(t[node],r-l+1), vv.pb({t[node],r-l+1});
t[node]=x;
t2[node]=x;
return;
}
else if(l==r)
{
t[node]=x;
t2[node]=x;
return;
}
int mid=l+r>>1;
if(t2[node]!=0)
ff(node);
upd(node<<1|1,mid+1,r,l1,r1,x);
upd(node<<1,l,mid,l1,r1,x);
t[node]=(t[node<<1]==t[node<<1|1])*t[node<<1];
}
void upd_hld(int x,int y,int c)
{
if(pos[head[x]]<=pos[y])
{
upd(1,1,n,pos[y],pos[x],c);
}
else
{
upd(1,1,n,pos[head[x]],pos[x],c);
upd_hld(st[head[x]],y,c);
}
}
vector<int> v2;
void phongbeo()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],v2.pb(a[i]);
sort(v2.begin(),v2.end());
for(int i=1;i<=n;i++)
a[i]=lower_bound(v2.begin(),v2.end(),a[i])-v2.begin()+1;
for(int i=1;i<n;i++)
cin>>l>>r,v[l].pb(r),v[r].pb(l),b[i]=r;
dfs(1,0);
descompose(1,1);
upd(1,1,n,1,1,a[1]);
//for(int i=1;i<=n;i++)
//cout<<pos[i]<<" "<<i,down
for(int i=1;i<n;i++)
{
s2=0;
vv.clear();
upd_hld(b[i],1,a[b[i]]);
for(auto f:vv)
{
upd_fenwick(f.a,-f.b);
}
cout<<s2,down
}
}
Compilation message (stderr)
construction.cpp:49:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
49 | main()
| ^~~~
construction.cpp: In function 'void upd_fenwick(long long int, long long int)':
construction.cpp:99:11: warning: statement has no effect [-Wunused-value]
99 | for(x;x<=n;x+=x&-x)
| ^
construction.cpp: In function 'long long int get_fenwick(long long int)':
construction.cpp:105:13: warning: statement has no effect [-Wunused-value]
105 | for(x;x;x-=x&-x)s+=bit[x];
| ^
construction.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int)':
construction.cpp:122:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
122 | if(l!=1)
| ^~
construction.cpp:124:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
124 | t[node]=x;
| ^
construction.cpp:134:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
134 | int mid=l+r>>1;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |