Aladdin and the Return Journey

Time Limit: 2000ms
Memory Limit: 32768KB

This problem will be judged on LightOJ. Original ID: 1348
64-bit integer IO format: %lld      Java class name: Main

Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for him. All he remembered was the paths he had taken to reach there. But since he took the lamp, all the genies in the cave became angry and they were planning to attack. As Aladdin was not afraid, he wondered how many genies were there. He summoned the Genie from the lamp and asked this.

Now you are given a similar problem. For simplicity assume that, you are given a tree (a connected graph with no cycles) with n nodes, nodes represent places, edges represent roads. In each node, initially there are an arbitrary number of genies. But the numbers of genies change in time. So, you are given a tree, the number of genies in each node and several queries of two types. They are:

  1. 0 i j, it means that you have to find the total number of genies in the nodes that occur in path from node i to j (0 ≤ i, j < n).
  2. 1 i v, it means that number of genies in node i is changed to v (0 ≤ i < n, 0 ≤ v ≤ 1000).

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a blank line. Next line contains an integer n (2 ≤ n ≤ 30000). The next line contains n space separated integers between 0 and 1000, denoting the number of genies in the nodes respectively. Then there are n-1 lines each containing two integers: u v (0 ≤ u, v < n, u ≠ v) meaning that there is an edge from node u and v. Assume that the edges form a valid tree. Next line contains an integer q (1 ≤ q ≤ 105) followed by q lines each containing a query as described above.

Output

For each case, print the case number in a single line. Then for each query 0 i j, print the total number of genies in the nodes that occur in path i to j.

Sample Input

1

4

10 20 30 40

0 1

1 2

1 3

3

0 2 3

1 1 100

0 2 3

Output for Sample Input

Case 1:

90

170

解题:树链剖分

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[maxn<<];
int head[maxn],fa[maxn],top[maxn],dep[maxn];
int siz[maxn],son[maxn],loc[maxn],clk,tot;
int c[maxn];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void update(int i,int val) {
while(i <= clk) {
c[i] += val; i += i&-i;
}
}
int sum(int i,int ret = ) {
while(i > ) {
ret += c[i];
i -= i&-i;
//cout<<ret<<"++++"<<endl;
}
return ret;
}
void FindHeavyEdge(int u,int father,int depth) {
fa[u] = father;
dep[u] = depth;
siz[u] = ;
son[u] = -;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == father) continue;
FindHeavyEdge(e[i].to,u,depth + );
siz[u] += siz[e[i].to];
if(son[u] == - || siz[e[i].to] > siz[son[u]])
son[u] = e[i].to;
}
}
void ConnectHeavyEdge(int u,int ancestor) {
top[u] = ancestor;
loc[u] = ++clk;
if(son[u] != -) ConnectHeavyEdge(son[u],ancestor);
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == fa[u] || son[u] == e[i].to) continue;
ConnectHeavyEdge(e[i].to,e[i].to);
}
}
int solve(int u,int v,int ret = ) {
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u,v);
ret += sum(loc[u]) - sum(loc[top[u]]-);
u = fa[top[u]];
}
if(dep[u] > dep[v]) swap(u,v);
ret += sum(loc[v]) - sum(loc[u]-);
return ret;
}
int val[maxn];
int main() {
int kase,n,m,u,v,op,cs = ;
scanf("%d",&kase);
while(kase--) {
scanf("%d",&n);
for(int i = ; i <= n; ++i) scanf("%d",val + i);
clk = tot = ;
memset(head,-,sizeof head);
for(int i = ; i < n; ++i) {
scanf("%d%d",&u,&v);
add(u+,v+);
add(v+,u+);
}
FindHeavyEdge(,,);
ConnectHeavyEdge(,);
memset(c,,sizeof c);
for(int i = ; i <= n; ++i)
update(loc[i],val[i]);
scanf("%d",&m);
printf("Case %d:\n",cs++);
while(m--) {
scanf("%d%d%d",&op,&u,&v);
if(op) {
int tmp = sum(loc[u+]) - sum(loc[u+]-);
update(loc[u+],-tmp + v);
} else printf("%d\n",solve(u+,v+));
}
}
return ;
}

LightOJ 1348 Aladdin and the Return Journey的更多相关文章

  1. Lightoj 1348 Aladdin and the Return Journey (树链剖分)(线段树单点修改区间求和)

    Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't wa ...

  2. LightOJ 1348(Aladdin and the Return Journey )

    题目链接:传送门 题目大意:一棵无根树,每个点上有权值,两种操作,0 x y询问x~y路径上权值和 1 x y将 节点 x 权值变为y.对于询问操作输出答案. 题目思路:树链剖分 #include & ...

  3. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  4. LightOJ 1344 Aladdin and the Game of Bracelets

    It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a ...

  5. [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))

    题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...

  6. LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

    http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...

  7. LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解

    http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...

  8. LightOJ - 1349 - Aladdin and the Optimal Invitation

    链接: https://vjudge.net/problem/LightOJ-1349 题意: Finally Aladdin reached home, with the great magical ...

  9. LightOJ 1341 - Aladdin and the Flying Carpet

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...

随机推荐

  1. UVaLive 6834 Shopping (贪心)

    题意:给定 n 个商店,然后有 m个限制,去 c 之前必须先去d,问你从0到n+1,最短路程是多少. 析:我们我们要到c,必须要先到d,那么举个例子,2 5, 3 7,如果我们先到5再到2,再到7再到 ...

  2. 搞定springboot项目连接远程服务器上kafka遇到的坑以及完整的例子

    版本 springboot 2.1.5.RELEASE kafka 2.2 遇到的坑 用最新的springboot就要用最新的kafka版本! 当我启动云服务器上的zk后,再启动kafka后台日志也没 ...

  3. $CF19A\ World\ Football\ Cup$

    炒鸡\(6\)批的模拟题. 注意的是输入 把握好空格 大小写. 根据题目的这句话来排序 积分榜是按照以下原则制作的:胜利一个队得3分,平分1分,失败0分. 首先,球队按积分顺序排在积分榜上,分数相等比 ...

  4. 全面学习ORACLE Scheduler特性(8)Application抛出的Events

    4.2 Application抛出的Events 首先要说明,这里所说的Application是个代词,即可以表示ORACLE数据库之外的应用程序,也可以是ORACLE数据库中的PROCEDURE等对 ...

  5. C++中图片重命名

    非常简单的小程序,满足自己的需求. #include <iostream> #include <fstream> #include<sstream> using n ...

  6. [ USACO 2001 OPEN ] 地震

    \(\\\) Description​ 给出一张 \(n\) 个点 \(m\) 条边的无向图,现在要建一棵生成树. 每条边都有消耗的时间 \(t_i\),也有建造的代价 \(w_i\) . 最后总金给 ...

  7. swiper3初始化/swiper-init/用data实例化swiper/data-swiper

    Framework7直接用data属性实例化swiper用起来很爽,刚好最近又用到swiper插件,自己写一个 HTML <div class="swiper-container sw ...

  8. dive into python:模块的导入和搜索文件路径的配置

    1.Python中导入模块:import sys:相当于Java中的导入包.类. 比如,我们导入sys模块,使用:import sys; 2.Python中调用函数的时候,会从默认配置的库文件夹中(s ...

  9. Farseer.net轻量级开源框架 入门篇:修改数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 ...

  10. linux下C/C++程序的内存布局

    内核空间和用户空间: 我们在编写程序时使用的内存空间叫虚拟内存,程序在运行时,要完成虚拟内存到物理内存的转换.假如在32位环境上,理论上我们可以使用的虚拟内存空间是4GB,但实际上这4GB并不能完全给 ...