Awesome Backup System

Time limit: 2.0 second
Memory limit: 64 MB
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 ≤ 105). 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 ≤ 109). Each of the following n − 1 lines contains integers x and y (1 ≤ x, yn; xy), 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 ≤ 105). 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 ≤ vn), where t specifies the type of the request and v is 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 109 + 7.

Samples

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
Problem Author: Nikita Sivukhin

对一棵树进行下列操作

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

2 v 查询顶点v的权值

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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e5+;
const int M = +;
const int mod=1e9+;
int n,m,k,tot=,s,t,r;
vector<int>G[N];
ll A[N],add[N]; int p[N];
void dfs(int u,int fa)
{
p[u]=fa;
for(int i=; 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;
for(int i=;i<=n;i++)cin>>A[i];
int u,v;
for(int i=;i<n;i++)
{
cin>>u>>v;
G[u].pb(v);
G[v].pb(u);
}
cin>>m;
memset(add,,sizeof add);
memset(p,,sizeof p);
dfs(,);
while(m--)
{
cin>>u>>v;
int x=p[v];
if(u==)
{
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 ;
}

URAL 2030 Awesome Backup System的更多相关文章

  1. 图解Android - Zygote, System Server 启动分析

    Init 是所有Linux程序的起点,而Zygote于Android,正如它的英文意思,是所有java程序的'孵化池'(玩过星际虫族的兄弟都晓得的).用ps 输出可以看到 >adb shell ...

  2. 图解Android - System Service 概论 和 Android GUI 系统

    通过 图解Android - Binder 和 Service 一文中,我们已经分析了Binder 和 Service的工作原理.接下来,我们来简要分析Android 系统里面都有哪些重要的Servi ...

  3. VES Hand Book Contents

    3...ABOUT THE VES4...Foreword 6...Chapter 1......Introduction6......Visual Effects and Special Effec ...

  4. malware analysis、Sandbox Principles、Design && Implementation

    catalog . 引言 . sandbox introduction . Sandboxie . seccomp(short for secure computing mode): API级沙箱 . ...

  5. Azure SQL Database 时间点还原(Point in Time Restore)功能

      微软中国TechNet 7 Oct 2014 9:17 PM Comments 0 Likes 原文地址:http://blogs.technet.com/b/azuretw/archive/20 ...

  6. Merkle Tree学习

    /*最近在看Ethereum,其中一个重要的概念是Merkle Tree,以前从来没有听说过,所以查了些资料,学习了Merkle Tree的知识,因为接触时间不长,对Merkle Tree的理解也不是 ...

  7. Mac OS 终端常用命令【搜藏】

    基础概念 OS X 采用的Unix文件系统,所有文件都挂在跟目录“ /” 下面,所以不在要有Windows 下的盘符概念.比如什么“C:”你在桌面上看到的硬盘都挂在 /Volumes 下.比如接上个叫 ...

  8. ubuntu mint 15 编译安装PHP开发环境

    php 5.3.5(download zip) httpd 2.2.24(download zip) mysql: apt-get install mysql step 1: install mysq ...

  9. Facebook Architecture

    Facebook Architecture Quora article a relatively old presentation on facebook architecture another I ...

随机推荐

  1. Jquery easyui datagrid 导出Excel

    From:http://www.cnblogs.com/weiqt/articles/4022399.html datagrid的扩展方法,用于将当前的数据生成excel需要的内容. 1 <sc ...

  2. asp.net mvc 2.o 中使用JQuery.uploadify

    From:http://www.cnblogs.com/strugglesMen/archive/2011/07/01/2095916.html 官方网站http://www.uploadify.co ...

  3. Android 自动朗读(TTS)

    在Android应用中,有时候需要朗读一些文本内容,今天介绍一下Android系统自带的朗读TextToSpeech(TTS).自动朗读支持可以对指定文本内容进行朗读,还可以把文本对应的音频录制成音频 ...

  4. SharePoint表单和工作流 - Nintex篇(五)

    博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 本篇我们应用Nintex来创建一个简单的工作流. 首先创建一个自定义列表,用于存放请假数据用,我们就叫做Leav ...

  5. 理解Mac和iOS中的 Event 处理

    根据现在的理解,我把event处理分为5部分,第一是,Event处理的Architecture:第二是,Event的Dispatch到first responder之前: 第三是,Event从firs ...

  6. 转载--Ubuntu设置环境变量

    Ubuntu设置环境变量并立即生效(以Ubuntu12.04为例) 标签: UbuntuLinux环境变量 2013-09-12 19:04 9961人阅读 评论(1) 收藏 举报  分类: Ubun ...

  7. explain分析查询

    参考以下文章,在此非常感谢原作者 explain分析查询

  8. (转)面向移动设备的HTML5开发框架

    (原)http://www.cnblogs.com/itech/archive/2013/07/27/3220352.html 面向移动设备的HTML5开发框架   转自:http://blogrea ...

  9. GSM短信侦听的便宜方案

    侦听GSM短信常用的是OsmocomBB + C118方案,主要是用luca/gsmmap分支.使用ccch_scan这个程序可以把通信封装成GSMTAP发给本机,然后用WireShark接收GSMT ...

  10. 黑马程序员——【Java基础】——面向对象(一)概述、类与对象、继承、抽象类、接口、多态、内部类

    ---------- android培训.java培训.期待与您交流! ---------- 一.面向对象概述 1.面向对象:是一个很抽象的概念,它相对面向过程而言,是一种程序设计的思想. 2.面向对 ...