G-awesome Backup SystemTime
limit:2000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticeURAL 2030
Description
It is known so all people can being divided into the groups:those who has never lost important data and those who regular ly perform data backups. Kirill is on the He-to-the-second after the incident with tests described in the problem "another D Ress rehearsal ". Not satisfied with the existing data backup solutions for various reasons, he decided to design his own backup system. He chose a simple and proud name for it:the "Awesome Backup System," ABS for short. Since errors in such an important system is absolutely unacceptable, Kirill asks you to test the beta version of his prod Uct. The ABS is organized as Follows:let there be
NComputers in a local network. The computers is numbered with integers from 1 to
N. Some pairs of computers is connected by cables. For economy, the network doesn ' t has unnecessary cables, which means that there are a unique cable path between any and co Mputers. Initially there is
a
Ibytes of information written on the
I-th computer. The ABS can process 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 com Puters'll has xv bytes of information more, while computer v would 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.
For testing the ABS, you is asked to write a program for a quick processing of such requests.
Input
The first line contains the number
Nof computers in the network (1≤
N≤10 5). The second line is given integers
a1,...,
a
N, which is the amounts of information (in bytes) on the computers at the initial time (0≤
a
I≤10 9). Each of the following
N−1 lines contains integers
xand
y(1≤
x,
y≤
N;
x≠
y), which mean the computers with numbers
xand
yis connected by a cable. It is guaranteed, the network is connected. The next line contains the number
mof requests to the system (1≤
m≤10 5). In the following
mLines you is given the requests in the order of their execution. Each request is a pair of integers
Tand
v(1≤
T≤2 and 1≤
v≤
N), where
TSpecifies 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 1 0 9 + 7.
Sample Input
input |
Output |
41 1 1 11 21 32 492 12 22 32 41 12 12 22 32 4 |
11111221 |
21 11 2142 22 11 12 21 22 11 12 21 22 11 12 21 22 1 |
1123581321 |
Puzzle: The nodes are layered into two categories, the child node is more than sqrt (n), the child node is less than sqrt (n). On the front of the offline update, after the online update.
#include <iostream>#include<vector>using namespaceStd;typedefLong LongLL;Const intN =100050;Const intK111;Const intMOD = 1e9 +7; LL Ans[n], out[N];vector<int>Cop[n],top[n];voidADD (LL &a,ConstLL B) {a+=b; if(A >=MOD) {a-=MOD; }}intMain () {Ios::sync_with_stdio (0); intN; while(cin>>N) { for(intI=1; i<=n;i++) {cin>>ans[i]; out[i]=0;} for(intI=0; i<n-1; i++) { intA,b;cin>>a>>b; Cop[a].push_back (b); Cop[b].push_back (a); } for(intI=1; i<=n;i++) for(intj=0; J<cop[i].size (); j + +)if(Cop[cop[i][j]].size () >M) Top[i].push_back (Cop[i][j]); intM;cin>>m; while(m--) { intA,b;cin>>a>>b; if(a==1) { if(Cop[b].size () >M) { for(intI=0; I<top[b].size (); i++) Add (Ans[top[b][i]],ans[b]); ADD ( out[b],ans[b]); } Else{LL x=Ans[b]; for(intI=0; I<cop[b].size (); i++) Add (x, out[Cop[b][i]]); for(intI=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(intI=0; I<cop[b].size (); i++) Add (x, out[Cop[b][i]]); cout<<x<<Endl; } } } } return 0;}
View Code
Father's update to his son