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. c++ class does not name a type (转载)

    转载:http://blog.csdn.net/typename/article/details/7173550 declare class does not name a type 出现这个编译错误 ...

  2. E20170610-hm

    presence  n. 出席; 仪表; 风度; 鬼魂,神灵; defence   n. 防御; 辩护; 防御工事; 后卫; phyle  n. 种族,宗族; race  n. 赛跑; 民族; 人种; ...

  3. bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】

    bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...

  4. robotframework - 框架做接口自动化get请求

    1.做get请求之前先安装 Request库,参考github上链接 :https://github.com/bulkan/robotframework-requests/#readme 2.请求&a ...

  5. JPA中关联关系(OneToOne、OneToMany、ManyToMany,ManyToOne)映射代码片段

    在使用Hibernate的时候我们常常会在类里边配置各种的关联关系,但是这个并不是很好配置,配置不当会出现各种各样的问题,下面具体来看一下: 首先我们来看User类里边有一个IdentityCard类 ...

  6. Linux学习之01_基础命令介绍

    初学Linux,还在摸索中,在这个过程中希望能记录下学习到的东西,参考的的书籍为<鸟哥的Linux私房菜> 在这里学到的主要命令有这几个: data cal bc man shutdown ...

  7. tomcat8的session共享实现方案

    tomcat8的session共享实现 下载tomcat 版本:apache-tomcat-8.0.53.zip 实现步骤,只需要两步 两个tomcat的server.xml都增加一样cluster配 ...

  8. JavaScript编程艺术-第7章代码汇总(2)

    [7.4节] 重回“JavaScript美术馆”代码 ***亲测可用*** HTML: JS:

  9. SVN配置详解

    原文:http://swjr.blog.com.cn/archives/2006/TheRoadToSubversion1authz.shtml http://www.dayuer.com/freeb ...

  10. service: no such service mysqld 与MySQL的开启,关闭和重启

    1.问题原因与解决办法 因为修改了MySQL临时文件的目录后,使用service mysqld restart重启MySQL出现如下错误: service: no such service mysql ...