P3384 【模板】轻重链剖分/树链剖分

模板题记录一下

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
ll mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

const int V = 5e6, E=5e6;
ll d[V],cost[E];
ll head[V],pnt[E],nxt[E],e=1;
int n,m;
ll sizes[V], son[V],deep[V], fa[V];
ll id[V], top[V];
ll sum1[V], sum2[V];
int cnt = 0;
ll used[V];

void add(ll pos, ll y)            //在pos位置+y,对d
{
        for(ll i=pos;i<=n;i+=lowbit(i))
        sum1[i] += y, sum2[i] += pos*y; //从这个位置开始,包含pos项的sum都改变。    sum1是d[i]的前缀和,sum2是d[i]*i的前缀和,sum1*pos-sum2就是a的前缀和
}

void add_range(ll l, ll r,ll x)
{
    if(l>r) return;
     if(r<=0||r>n) return;
     add(l,x), add(r+1,-x);
}

ll ask_for_one(ll p)
{
        ll ans  = 0;
        for(ll i=p;i>=1;i-=lowbit(i))
        ans += sum1[i];
        return  ans;
}

ll ask(ll p)
{
        //if(l>r) return;
     if(p<=0||p>n) return 0;
        ll ans  = 0;
        for(ll i=p;i>=1;i-=lowbit(i))
        ans += ((p+1)*sum1[i] - sum2[i]);
        return ans;
}


void addedge(ll u,ll v)
{
    pnt[e]=v;       //当前以u为顶点,c为边长的,到v的一条边
    nxt[e]=head[u];     //下一个其实是前一个
    head[u]=e++;        //当前边编号
}

void dfs1(int x,int f)
{
    fa[x]=f;
    sizes[x]=1;
    deep[x]=deep[f]+1;
    for(int i=head[x];i;i=nxt[i])
    {
        int y= pnt[i];
        if(y==f)
            continue;
        dfs1(y,x);
        sizes[x]+=sizes[y];
        if(son[x]==0 || sizes[y]>sizes[son[x]])
            son[x]=y;
    }
}


void dfs2(int x,int t)
{
    id[x]=++cnt;
    top[x]=t;
    if(!son[x])
        return;
    dfs2(son[x],t);
    for(int i=head[x];i;i=nxt[i])
    {
        int y=pnt[i];
        if(y==fa[x] || y==son[x])
            continue;
        dfs2(y,y);
    }
}


int len[V];
int ans = 0;

void update1(int num, int f, int flag=0)
{
    int cur = num;
    used[id[top[cur]]] = f;
}

void update2(int num, int f, int flag=0)
{
    int cur = num;
     while(cur)
     {
         int l = id[cur], r = id[top[cur]];
         if(l>r) swap(l,r);
         ll tmp = ask(r) - ask(l-1);
         if(tmp) add_range(l,id[tmp],-tmp);
         add_range(l,r,f);
         cur = fa[top[cur]];
     }
}

void LCA1(int x, int y, ll z)
{
    while(top[x]!=top[y])
    {
        if(deep[top[x]]>deep[top[y]] ) swap(x,y);
        add_range(id[top[y]],id[y],z);
        y = fa[top[y]];
        if(deep[top[x]]>deep[top[y]] ) swap(x,y);
    }
    if(deep[x]<deep[y]) add_range(id[x],id[y],z);
    else add_range(id[y],id[x],z);
}

ll LCA2(int x, int y)
{
    ll ans = 0;
    while(top[x]!=top[y])
    {
        if(deep[top[x]]>deep[top[y]] ) swap(x,y);
        ans += ask(id[y]) - ask(id[top[y]]-1);
        y = fa[top[y]];
        if(deep[top[x]]>deep[top[y]] ) swap(x,y);
    }
    if(deep[x]<deep[y]) ans += ask(id[y]) - ask(id[x]-1);
    else ans += ask(id[x]) - ask(id[y]-1);
    return ans%mod;
}

ll a[maxn];

int main()
{
    ll m, r;
    n = read(), m = read(), r = read(), mod = read();
    rep(i,1,n) a[i] = read();
    rep(i,1,n-1)
    {
        ll x = read(), y = read();
        addedge(x,y), addedge(y,x);
    }
    dfs1(r,0);
    dfs2(r,0);
    rep(i,1,n) add_range(id[i],id[i],a[i]);
    rep(i,1,m)
    {
        int flag = read();
        if(flag==1)
        {
            ll x = read(), y = read(), z = read();
            LCA1(x,y,z);
        }
        else if(flag==2)
        {
            ll x = read(), y = read();
            cout<<LCA2(x,y)<<endl;
        }
        else if(flag==3)
        {
            ll x = read(), z = read();
            add_range(id[x],id[x]+sizes[x]-1,z);
        }
        else
        {
            ll x = read();
            cout<<(ask(id[x]+sizes[x]-1) - ask(id[x]-1)+mod)%mod<<endl;
        }
    }
    return 0;
}

/*

5 5 1 55
1 2 3 4 5
1 2
1 3
1 5
3 4
1 1 4 1


*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值