FZU 2277 树状数组

Posted poler

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\n",res);
84             }
85         }    
86         for(int i=1;i<=n;i++) G[i].clear();
87     }
88     return 0;    
89 }

 

以上是关于FZU 2277 树状数组的主要内容,如果未能解决你的问题,请参考以下文章

FZU 2277Change

Change FZU - 2277 毒瘤啊 毒瘤题目

FZU-2134 上车(树状数组)

FZU-2075 Substring(后缀数组)

浅析树状数组

FZU 2092 收集水晶 BFS记忆化搜索