• FZU 2277 树状数组


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

     Problem Description

    There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai.

    Initially all the node’s value is 0.

    We have q operations. There are two kinds of operations.

    1 v x k : a[v]+=x , a[v’]+=x-k (v’ is child of v) , a[v’’]+=x-2*k (v’’ is child of v’) and so on.

    2 v : Output a[v] mod 1000000007(10^9 + 7).

     Input

    First line contains an integer T (1 ≤ T ≤ 3), represents there are T test cases.

    In each test case:

    The first line contains a number n.

    The second line contains n-1 number, p2,p3,…,pn . pi is the father of i.

    The third line contains a number q.

    Next q lines, each line contains an operation. (“1 v x k” or “2 v”)

    1 ≤ n ≤ 3*10^5

    1 ≤ pi < i

    1 ≤ q ≤ 3*10^5

    1 ≤ v ≤ n; 0 ≤ x < 10^9 + 7; 0 ≤ k < 10^9 + 7

     Output

    For each operation 2, outputs the answer.

     Sample Input

    1 3 1 1 3 1 1 2 1 2 1 2 2

     Sample Output

    2 1

     Source

    第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)

    题意:略

    题目分析:略

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<vector>
     6 #define Pii pair<int,int>
     7 #define fi first
     8 #define se second
     9 #define mp make_pair
    10 #define pb push_back
    11 #define maxn 300100
    12 #define LL long long
    13 #define lson l,m,rt<<1
    14 #define rson m+1,r,rt<<1|1
    15 #define Mod 1000000007
    16 using namespace std;
    17 vector<int> G[maxn];
    18 int n,k,x,ti=0;
    19 LL tot[maxn];
    20 LL C[maxn],add[maxn],Add[maxn*4];
    21 int l[maxn],r[maxn];
    22 LL deep[maxn];
    23 inline int lowbit(int x){return (x&-x);}
    24 inline void update(int x,LL y,LL t){
    25     while(x<=n){
    26         C[x]+=y;
    27         tot[x]+=t;
    28           x+=lowbit(x);
    29       }
    30 }
    31 LL query(int x){
    32       LL ans=0;
    33      while (x>0){    
    34         ans+=C[x];
    35         x-=lowbit(x);
    36       }
    37       return ans;
    38 }
    39 LL Query(int x){
    40       LL ans=0;
    41      while (x>0){    
    42         ans+=tot[x];
    43         x-=lowbit(x);
    44       }
    45       return ans;
    46 }
    47 void dfs(int x,int fa,int dep){
    48     l[x]=++ti;
    49     deep[l[x]]=dep;
    50     for(int i=0;i<G[x].size();i++){
    51         if(G[x][i]==fa) continue;
    52         dfs(G[x][i],x,dep+1);
    53     }
    54     r[x]=ti;
    55 }
    56 int main(){
    57     int u,Q,v,T,opt;
    58     cin>>T;
    59     while(T--){
    60         cin>>n;
    61         memset(C,0,sizeof(C));
    62         memset(tot,0,sizeof(tot));
    63         for(int i=2;i<=n;i++){
    64             scanf("%d",&u);
    65             G[u].pb(i);
    66         }
    67         ti=0;
    68         dfs(1,-1,1);
    69         LL res;
    70         cin>>Q;
    71         for(int i=1;i<=Q;i++){
    72             scanf("%d",&opt);
    73             if(opt==1){
    74                 scanf("%d%d%d",&v,&x,&k);
    75                 update(l[v],x+(LL)k*deep[l[v]],k);
    76                 update(r[v]+1,-x-(LL)k*deep[l[v]],-k);
    77             }
    78             else{
    79                 scanf("%d",&v);
    80                 res=query(l[v])-Query(l[v])*deep[l[v]];
    81                 while(res<0) res+=Mod;
    82                 res%=Mod;
    83                 printf("%I64d
    ",res);
    84             }
    85         }    
    86         for(int i=1;i<=n;i++) G[i].clear();
    87     }
    88     return 0;    
    89 }
  • 相关阅读:
    【转】C++ ZLib压缩与解压缩
    【转】Log4j 配置最全说明
    【转】每个程序员都必须遵守的编程原则
    【转】MySQL忘记root密码
    【转】OpenGL开发环境配置
    Tomcat不能启动注意事项
    Tomcat不能启动注意事项
    Android通过php插入查询SQL数据库
    Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存
    Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存
  • 原文地址:https://www.cnblogs.com/poler/p/7275587.html
Copyright © 2020-2023  润新知