LightOJ 1348 Aladdin and the Return Journey
Aladdin and the Return Journey
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:
- 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).
- 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的更多相关文章
- 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 ...
- LightOJ 1348(Aladdin and the Return Journey )
题目链接:传送门 题目大意:一棵无根树,每个点上有权值,两种操作,0 x y询问x~y路径上权值和 1 x y将 节点 x 权值变为y.对于询问操作输出答案. 题目思路:树链剖分 #include & ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- 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 ...
- [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))
题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...
- LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...
- LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解
http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...
- LightOJ - 1349 - Aladdin and the Optimal Invitation
链接: https://vjudge.net/problem/LightOJ-1349 题意: Finally Aladdin reached home, with the great magical ...
- LightOJ 1341 - Aladdin and the Flying Carpet
题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...
随机推荐
- javascript实现页面跳转
这里指的页面跳转是将浏览器标签页转到新的网址. 只需要使用 window.location.href="url" 就行了 示例 <!DOCTYPE html> < ...
- WKWebView 和 UIWebView 允许背景音乐自动播放(记录)
WKWebView WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.allowsInlin ...
- C++小项目-吃豆子游戏
GMap.h #pragma once //保证头文件只被编译一次 #include "stdafx.h" #define MAP_LEN 19 //逻辑地图大小 (逻辑地图由行. ...
- JS数组、数组和学生对象
<html> <head> <meta charset="utf-8"> <title>JS</title> </ ...
- python程序展现图片
突然想写一个python程序能够显示图片的 ,展示文字的已经实现了 现在就搞一搞这个吧 相信也是很简单 首先是放一张图片在e盘下面 等会程序打包的时候将会用到 就决定是你啦 皮卡丘: 然后就写代码吧:
- 获取Sprite上某一个点的透明度
转载[ http://www.cnblogs.com/Androider123/p/3795050.html] 本篇文章主要讲一下怎么做一个不规则的按钮,比如如下图的八卦,点击绿色和点击红色部分,需要 ...
- Django基础之admin功能
Django默认开起了后台 1.访问admin后台 2.用户和密码进行登录 ============================================================== ...
- UVM基础之------uvm_port_base
Port Base Classes uvm_port_component_base This class defines an interface for obtaining a port ...
- OC语言Block
OC语言Block 一.Block (一)简介 Block是什么?苹果推荐的比较特殊的数据类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. Block和 ...
- ArcGIS 坐标系 整理
刚使用ArcGIS的时候,对坐标系的点一直很混乱,今天想要整理整理. 一.地理坐标系与投影坐标系的区分 首先要能区分地理坐标系(GCS)和投影坐标系(PCS). 上面的是地理坐标系的举例,简单理解为不 ...