ural 2030

Awesome Backup System

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice URAL
2030

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:

  1. 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.
  2. 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 are asked to write a program for a quick processing of such requests.

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 ≤ xy ≤ nx ≠ 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

对一棵树进行下列操作

1 v 将顶点v的邻接顶点的权值加上w[v]

2 v 查询顶点v的权值

哎,还是太弱了,这题不需要任何数据结构,只需要先将树转换成有根树,开一个标记数组add,add[v]表示给v的邻接顶点增加了add[v],在进行1操作的时候,直接将v的父亲加上v的权值,则查询的时候只有父亲对他有影响,加上父亲的add标记就行

/*************************************************************************
    > File Name: Spaly.cpp
    > Author: acvcla
    > QQ:
    > Mail: [email protected]
    > Created Time: 2014年11月16日 星期日 00时14分26秒
 ************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 100;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
vector<int>G[maxn];
LL A[maxn],add[maxn];
const int mod=1e9+7;
int p[maxn];
void dfs(int u,int fa)
{
    p[u]=fa;
    for(int i=0; i<G[u].size(); i++)
    {
        int &v=G[u][i];
        if(v!=fa&&v!=u)
        {
            dfs(v,u);
        }
    }
}
int main()
{
    int n,m;
    cin>>n;
    rep(i,0,n)G[i].clear();
    rep(i,1,n)cin>>A[i];
    int u,v;
    rep(i,1,n-1)
    {
        cin>>u>>v;
        G[u].pb(v);
        G[v].pb(u);
    }
    cin>>m;
    memset(add,0,sizeof add);
    memset(p,0,sizeof p);
    dfs(1,0);
    while(m--)
    {
        cin>>u>>v;
        int x=p[v];
        if(u==1)
        {
            add[v]=(add[v]+A[v]+add[x])%mod;
            A[x]=(A[x]+A[v]+add[x])%mod;
        }
        else cout<<(A[v]+add[x])%mod<<endl;
    }
    return 0;
}
时间: 2024-12-26 07:54:31

ural 2030的相关文章

父亲对儿子的更新

G - Awesome Backup System Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2030 Description It is known that all people can be divided into two groups: those who have never lost important data a

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

ural 1219. Symbolic Sequence

1219. Symbolic Sequence Time limit: 1.0 secondMemory limit: 64 MB Your program is to output a sequence of 1 000 000 lowercase Latin letters. This sequence should satisfy the following restrictions: Every letter occurs not more than 40 000 times in th