父亲对儿子的更新
Description
It is known that all people can be divided into two groups: those who have never lost important data and those who regularly perform data backups. Kirill is on his way from the first group to the second after the incident with tests described in the problem “ Another Dress Rehearsal”. Not satisfied with the existing data backup solutions for various reasons, he decided to design his own backup system. He chose a simple but proud name for it: the “Awesome Backup System,” ABS for short. Since errors in such an important system are absolutely unacceptable, Kirill asks you to test the beta version of his product.
The ABS is organized as follows: let there be n computers in a local network. The computers are numbered with integers from 1 to n. Some pairs of computers are connected by cables. For economy, the network doesn’t have unnecessary cables, which means that there is a unique cable path between any two computers. Initially there are ai bytes of information written on the i-th computer. The ABS can process two types of requests:
- Copy all the information from computer v to all adjacent computers (i.e., to all computers directly connected to it by a cable) If computer v had xv bytes of information, then, after copying, all adjacent computers will have xv bytes of information more, while computer v will still have xv bytes of information.
- Output the current amount of information on computer v. Since this amount can grow very quickly, output the remainder of its division by the number 109 + 7.
Input
The first line contains the number n of computers in the network (1 ≤ n ≤ 10 5). In the second line you are given integers a1, …, an, which are the amounts of information (in bytes) on the computers at the initial time (0 ≤ ai ≤ 10 9). Each of the following n − 1 lines contains integers x and y (1 ≤ x, y ≤ n; x ≠ y), which mean that the computers with numbers x and y are connected by a cable. It is guaranteed that the network is connected.
The next line contains the number m of requests to the system (1 ≤ m ≤ 10 5). In the following m lines you are given the requests in the order of their execution. Each request is a pair of integers t and v (1 ≤ t ≤ 2 and 1 ≤ v ≤ n), where t specifies the type of the request and vis the number of the computer to which the request is applied.
Output
For each request of the second type, output in a separate line the remainder of the division of the answer by the number 10 9 + 7.
Sample Input
input | output |
---|---|
4 1 1 1 1 1 2 1 3 2 4 9 2 1 2 2 2 3 2 4 1 1 2 1 2 2 2 3 2 4 |
1 1 1 1 1 2 2 1 |
2 1 1 1 2 14 2 2 2 1 1 1 2 2 1 2 2 1 1 1 2 2 1 2 2 1 1 1 2 2 1 2 2 1 |
1 1 2 3 5 8 13 21 |
题解:把结点分层两类, 孩子结点大于 sqrt(n) 个的 , 孩子结点小于 sqrt(n) 。对前面的离线更新, 后面的在线更新 。
#include <iostream> #include <vector> using namespace std; typedef long long LL; const int N = 100050; const int M = 111; const int MOD = 1e9 + 7; LL ans[N],out[N]; vector<int> cop[N],top[N]; void Add(LL &a, const LL b) { a += b; if (a >= MOD) { a -= MOD; } } int main() { ios::sync_with_stdio(0); int n; while(cin>>n) { for(int i=1;i<=n;i++) {cin>>ans[i];out[i]=0;} for(int i=0;i<n-1;i++) { int a,b;cin>>a>>b; cop[a].push_back(b); cop[b].push_back(a); } for(int i=1;i<=n;i++) for(int j=0;j<cop[i].size();j++) if(cop[cop[i][j]].size()>M) top[i].push_back(cop[i][j]); int m;cin>>m; while(m--) { int a,b;cin>>a>>b; if(a==1) { if(cop[b].size()>M) { for(int i=0;i<top[b].size();i++) Add(ans[top[b][i]],ans[b]); Add(out[b],ans[b]); } else { LL x=ans[b]; for(int i=0;i<cop[b].size();i++) Add(x,out[cop[b][i]]); for(int i=0;i<top[b].size();i++) Add(ans[top[b][i]],x); Add(out[b],x); } } else { if(cop[b].size()>M) cout<<ans[b]<<endl; else { LL x=ans[b]; for(int i=0;i<cop[b].size();i++) Add(x,out[cop[b][i]]); cout<<x<<endl; } } } } return 0; }