LOJ2831 JOISC2018 道路建设 LCT、树状数组

传送门


题目的操作大概是:求某个点到根的链的逆序对,然后对这条链做区间赋值

求某个点到根的链,就是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;
}

转载于:https://www.cnblogs.com/Itst/p/10623755.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值