题目的操作大概是:求某个点到根的链的逆序对,然后对这条链做区间赋值
求某个点到根的链,就是LCT中的access操作,所以我们每一次把access过后的链打上标记,就可以做到区间赋值了。
计算答案只需要在access的过程中用树状数组求一下逆序对即可。
#include<bits/stdc++.h>
#define PII pair < int , int >
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return f ? -a : a;
}
const int MAXN = 1e5 + 7;
struct node{
int fa , ch[2] , sz;
bool mark;
}Tree[MAXN];
int N , cnt , val[MAXN] , lsh[MAXN];
long long ans;
queue < PII > q;
namespace BIT{
#define lowbit(x) ((x) & -(x))
int BIT[MAXN];
inline void add(int x , int num){
while(x <= cnt){
BIT[x] += num;
x += lowbit(x);
}
}
inline int get(int x){
int sum = 0;
while(x){
sum += BIT[x];
x -= lowbit(x);
}
return sum;
}
}
using namespace BIT;
inline bool nroot(int x){
return Tree[Tree[x].fa].ch[0] == x || Tree[Tree[x].fa].ch[1] == x;
}
inline bool son(int x){
return Tree[Tree[x].fa].ch[1] == x;
}
inline void pushup(int x){
Tree[x].sz = Tree[Tree[x].ch[0]].sz + Tree[Tree[x].ch[1]].sz + 1;
}
inline void rotate(int x){
bool f = son(x);
int y = Tree[x].fa , z = Tree[y].fa , w = Tree[x].ch[f ^ 1];
Tree[x].fa = z;
if(nroot(y))
Tree[z].ch[son(y)] = x;
Tree[y].fa = x;
Tree[x].ch[f ^ 1] = y;
Tree[y].ch[f] = w;
if(w)
Tree[w].fa = y;
pushup(y);
}
inline void mark(int x){
Tree[x].mark ^= 1;
swap(Tree[x].ch[0] , Tree[x].ch[1]);
}
inline void pushdown(int x){
if(Tree[x].mark){
mark(Tree[x].ch[0]);
mark(Tree[x].ch[1]);
Tree[x].mark = 0;
}
}
void pushdown_all(int x){
if(nroot(x))
pushdown_all(Tree[x].fa);
pushdown(x);
}
inline void Splay(int x){
pushdown_all(x);
while(nroot(x)){
if(nroot(Tree[x].fa))
rotate(son(x) == son(Tree[x].fa) ? Tree[x].fa : x);
rotate(x);
}
pushup(x);
}
inline void access(int x){
int y = x;
x = Tree[x].fa;
for( ; x ; y = x , x = Tree[x].fa){
Splay(x);
int t = x;
while(Tree[t].ch[1])
pushdown(t = Tree[t].ch[1]);
q.push(PII(val[t] , Tree[Tree[x].ch[0]].sz + 1));
ans += 1ll * get(val[t] - 1) * (Tree[Tree[x].ch[0]].sz + 1);
add(val[t] , Tree[Tree[x].ch[0]].sz + 1);
Tree[x].ch[1] = y;
pushup(x);
}
}
inline void clear(){
while(!q.empty()){
PII t = q.front();
q.pop();
add(t.first , -t.second);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
N = read();
for(int i = 1 ; i <= N ; ++i)
val[i] = lsh[i] = read();
sort(lsh + 1 , lsh + N + 1);
cnt = unique(lsh + 1 , lsh + N + 1) - lsh - 1;
for(int i = 1 ; i <= N ; ++i)
val[i] = lower_bound(lsh + 1 , lsh + cnt + 1 , val[i]) - lsh;
for(int i = 1 ; i < N ; ++i){
int a = read() , b = read();
ans = 0;
Tree[b].fa = a;
access(b);
Splay(b);
cout << ans << '\n';
clear();
}
return 0;
}