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 }
时间: 2024-10-17 16:55:01

FZU 2277 树状数组的相关文章

fzu 1962 树状数组

优雅的树状数组!人们发明了复杂度为logn的求解第k小的方法,常数小且代码量小,实在是用来求解本类题目的最佳方法,可惜的是我的Treap超时了...... 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 500001; 7 int c[N]; 8 int n, m, maxn; 9 10 int lb( int

FZU 2176 easy problem (DFS序+树状数组)

对于一颗树,dfs遍历为每个节点标号,在进入一个树是标号和在遍历完这个树的子树后标号,那么子树所有的标号都在这两个数之间,是一个连续的区间.(好神奇~~~) 这样每次操作一个结点的子树时,在每个点的开始结束两个点标记一下就可以,用后缀数组求前缀和就可知道每个点的值. 这道题虽然很麻烦(dep[y]-dep[x])%k .但是注意到K很小(1<=k<=5),可以维护k个树状数组. 提交时编译器选GUN C++迷之RE...换Visual C++ #include <cstdio> #

FZU 2225 小茗的魔法阵 扫描线+树状数组

这个题和一个CF上的找"Z"的题差不多,都是扫描线+树状数组 从右上角的主对角线开始扫描,一直扫到左下角,每次更新,右延伸等于该扫描线的点,注意在其所在的树状数组更新就好了 时间复杂度O(n^2logn) #include <stdio.h> #include <iostream> #include <vector> #include <math.h> #include <set> #include <map> #

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 636    Accepted Submission(s): 378 Problem Description P is a permutation of the

HDU 5775 树状数组

Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 853    Accepted Submission(s): 504 Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is t

HDU 5775 Bubble Sort(树状数组)

题目链接:HDU 5775 题面: Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 709    Accepted Submission(s): 418 Problem Description P is a permutation of the integers from 1 to N(index startin

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

(POJ 3067) Japan (慢慢熟悉的树状数组)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29295   Accepted: 7902 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas