PerfectPan
;

FZUOJ 2277 Change

July 23, 2017

题目链接http://acm.fzu.edu.cn/problem.php?pid=2277

题意:给你一棵以 11 为根的树,树上每个初始节点为 00,然后有两种操作:1.1 v x k 表示给 vv 节点加 xxvv 节点的孩子加 xkx-kvv 节点孩子的孩子加 x2kx-2*k,一直到叶子节点;2.2 v 表示查询当前 vv 的权值。

思路:操作1可以转化为 [x+dep[u]×kdep[v]×k][x+dep[u]\times k-dep[v]\times k]其中 vvuu 的子节点,那么我们可以先树链剖分,然后用一个树状数组维护 x+dep[u]×kx+dep[u]\times k 的值,再用另一个树状数组维护 k\sum k的值就可以了,查询的时候相当于从 vv 走到根节点所有的值相加。

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=3e5+10;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
int T;
int n,m,q,dfs_clock;
int son[maxn],id[maxn],fa[maxn],bel[maxn],dep[maxn],sz[maxn];
ll sum[maxn][2];
vector<int>G[maxn];
void init(){
    dfs_clock=0;
    for (int i=1;i<=n;i++) G[i].clear();
    memset(sum,0,sizeof(sum));
}
void dfs1(int u,int f){
    dep[u]=(f==-1?1:dep[f]+1);
    fa[u]=f;
    sz[u]=1;
    son[u]=-1;
    for (int i=0;i<(int)G[u].size();i++){
        int v=G[u][i];
        if (v==f) continue;
        dfs1(v,u);
        sz[u]+=sz[v];
        if (son[u]==-1 || sz[v]>sz[son[u]]){
            son[u]=v;
        }
    }
}
void dfs2(int u,int f){
    bel[u]=f;
    id[u]=++dfs_clock;
    if (son[u]==-1) return;
    dfs2(son[u],f);
    for (int i=0;i<(int)G[u].size();i++){
        int v=G[u][i];
        if (v==fa[u] || v==son[u]) continue;
        dfs2(v,v);
    }
}
int lowbit(int x){return x&(-x);}
void add(int x,int v,int k){
    while (x<=n){
        sum[x][k]+=v;
        sum[x][k]%=MOD;
        x+=lowbit(x);
    }
}
ll getSum(int x,int k){
    ll ret=0;
    while (x){
        ret+=sum[x][k];
        x-=lowbit(x);
    }
    return ret%MOD;
}
void solve(int v){
    int t=dep[v];
    ll sum1=0,sum2=0;
    while (bel[v]!=1){
        sum1=(sum1+getSum(id[v],0)-getSum(id[bel[v]]-1,0)+MOD)%MOD;
        sum2=(sum2+getSum(id[v],1)-getSum(id[bel[v]]-1,1)+MOD)%MOD;
        v=fa[bel[v]];
    }
    sum1=(sum1+getSum(id[v],0)-getSum(id[bel[v]]-1,0)+MOD)%MOD;
    sum2=(sum2+getSum(id[v],1)-getSum(id[bel[v]]-1,1)+MOD)%MOD;
    sum1=(sum1*t)%MOD;
    sum2-=sum1;
    sum2=(sum2%MOD+MOD)%MOD;
    printf("%I64d\n",sum2);
    return;
}
int main(){
    for (read(T);T;T--){
        read(n);
        init();
        for (int i=2;i<=n;i++){
            int p;read(p);
            G[i].push_back(p);
            G[p].push_back(i);
        }
        dfs1(1,-1);
        dfs2(1,1);
        read(q);
        for (int i=1;i<=q;i++){
            int op,v,x,k;
            read(op);
            if (op==1){
                read(v),read(x),read(k);
                add(id[v],k,0);
                add(id[v],(x+(ll)k*dep[v]%MOD)%MOD,1);
            }
            else{
                read(v);
                solve(v);
            }
        }
    }
    return 0;
}

Yiming Pan

Written by Yiming Pan who lives and works in Hangzhou China. Welcome follow me on Github

Cannot load comments. Please check you network.