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>
using namespace std;
#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define Debug1(a) cout<<#a<<"="<<(a)<<"\n"
#define Debug2(a,b) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define Debug3(a,b,c) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define Debug4(a,b,c,d) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define Debug5(a,b,c,d,e) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define Debug6(a,b,c,d,e,f) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define Debug(...) macro_dispatcher(Debug, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) { bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)
#define all(x) x.begin(), x.end()
#define geti1(X) cin >> X
#define geti2(X,Y) cin >> X >> Y
#define geti3(X,Y,Z) cin >> X >> Y >> Z
#define geti4(X,Y,Z,W) cin >> X >> Y >> Z >> W
#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define geti(...) GET_MACRO(__VA_ARGS__, geti4, geti3, geti2, geti1) (__VA_ARGS__)
#define INF 987654321
#define endl '\n'
const ll mod = 1e9 + 7;
struct sumTree{
vector<ll> d; int n;
void init(int x){
n = 1; while( n < x ) n *= 2;
d.resize(n*2+5);
}
void update(int p, ll val, int op){
p += n-1;
if( op == 0 && d[p] == val ) return;
if( op ) d[p] += val;
else d[p] = val;
for(p>>=1;p;p>>=1) d[p] = d[p*2] + d[p*2+1];
}
ll query(int l, int r){
if( l > r ) return 0;
ll res = 0;
for(l+=n-1,r+=n-1;l<=r;l>>=1,r>>=1){
if( l&1 ) res += d[l++];
if( !(r&1) ) res += d[r--];
}
return res;
}
};
struct segNode{
int l,r,val,t;
};
segNode combine(segNode a, segNode b){
if( a.t > b.t ) return a;
else return b;
}
struct segTree{
int timer, n; vector<segNode> d;
void init(int x){
n = 1; while( n < x ) n *= 2;
d.resize(n*2+5);
for(auto& e : d) e = {0,0,0,0};
}
void update(int l, int r, int val){
++timer;
segNode x = {l,r,val,timer};
for(l+=n-1,r+=n-1;l<=r;l>>=1,r>>=1){
if( l&1 ) d[l++] = x;
if( !(r&1) ) d[r--] = x;
}
}
segNode query(int p){
p += n-1;
segNode ret = d[p];
for(p>>=1;p;p>>=1) ret = combine(ret, d[p]);
return ret;
}
};
int sz[100500], cNo[100500], cPos[100500], cSize[100500], cHead[100500], cLeaf[100500], cN;
int vis[100500], pa[100500];
int N, timer;
int C[100500]; vector<int> E[100500];
int dfs_build(int x, int p){
pa[x] = p;
sz[x] = 1;
for(auto e : E[x])if(e!=p) sz[x] += dfs_build(e,x);
return sz[x];
}
void build_chain(int cur){
vis[cur] = true;
if( cHead[cN] == 0 ) cHead[cN] = cur; // new Chain
cLeaf[cN] = cur; cNo[cur] = cN;
cSize[cN]++; cPos[cur] = cSize[cN];
int nxt = -1, mx = -1;
// find chain to extend
for(auto e : E[cur])if(!vis[e]){
if( sz[e] > mx ){
nxt = e; mx = sz[e];
}
}
if( mx >= 0 ) build_chain(nxt);
for(auto e : E[cur])if(!vis[e]){
cN++; build_chain(e);
}
}
vector<segTree> tree;
void build_hld_segTree(){
for(int n=1;n<=cN;n++){
int cur = cLeaf[n];
tree[n].init(cSize[n]+2);
}
}
void update_to_root(int pos, int val){
// handle leaf
int cur = pos;
int cno = cNo[cur];
int cpos = cPos[cur];
tree[cno].update(1,cpos,val);
cur = pa[cHead[cno]];
while( cur != -1 ){
// do on current chain
cno = cNo[cur];
cpos = cPos[cur];
segNode seg = tree[cno].query(cpos);
tree[cno].update(cpos+1, seg.r, seg.val);
tree[cno].update(1, cpos, val);
cur = pa[cHead[cno]];
}
}
sumTree stree;
ll calc_inv(int pos){
ll ans = 0;
vector<int> dielist;
int cur = pos;
while( cur != -1 ){
// do on current chain
int cno = cNo[cur];
int cpos = cPos[cur];
while( cpos >= 1 ){
// 1 - look at current seg
segNode seg = tree[cno].query(cpos);
ll cnt = (cpos - seg.l + 1);
ans += cnt * stree.query(1, seg.val-1);
stree.update(seg.val, cnt, 1);
dielist.push_back(seg.val);
// 2 - move to prev seg
cpos = seg.l - 1;
}
// move to prev chain
cur = pa[cHead[cno]];
}
for(auto e : dielist) stree.update(e,0,0);
return ans;
}
int main(){
scanf("%d",&N);
vector<int> v = {-100};
repp(i,N){
scanf("%d",C+i); v.pb(C[i]);
}
sort(all(v)); v.erase(unique(all(v)), v.end());
repp(i,N) C[i] = lower_bound(all(v),C[i]) - v.begin();
stree.init(sz(v)+5);
vector<Pi> elist;
rep(i,N-1){
int a,b; geti(a,b);
E[a].pb(b); E[b].pb(a);
elist.push_back({a,b});
}
dfs_build(1,-1);
cN = 1; build_chain(1);
tree.resize(cN+2);
build_hld_segTree();
for(auto& e : elist) if( pa[e.Se] != e.Fi ) swap(e.Fi, e.Se);
update_to_root(1,C[1]);
for(auto e : elist){
printf("%lld\n",calc_inv(e.Fi));
update_to_root(e.Se, C[e.Se]);
}
}
Compilation message (stderr)
construction.cpp: In function 'void build_hld_segTree()':
construction.cpp:151:7: warning: unused variable 'cur' [-Wunused-variable]
int cur = cLeaf[n];
^~~
construction.cpp: In function 'int main()':
construction.cpp:209:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
construction.cpp:212:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",C+i); v.pb(C[i]);
~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |