题目链接:传送门

题目大意:一棵无根树,每个点上有权值,两种操作,0 x y询问x~y路径上权值和 1 x y将

     节点 x 权值变为y。对于询问操作输出答案。

题目思路:树链剖分

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 30005
#define maxn 30010
typedef pair<int,int> PII;
typedef long long LL;
LL read(){
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,k,v,head[N],a[N],hcnt,L,R;
struct Edge{int to,nxt;}node[N<<];
int son[N],siz[N],id[N],tid;
int fa[N],top[N],dep[N],posi[N];
LL seg[N<<];
void init(){
mst(head,-);mst(id,);mst(siz,);
mst(son,);hcnt=tid=;
}
void dfs1(int u,int f,int deep){
fa[u]=f,dep[u]=deep,siz[u]=;
for(int i=head[u];~i;i=node[i].nxt){
int e=node[i].to;if(e==f)continue;
dfs1(e,u,deep+);siz[u]+=siz[e];
if(!son[u]||siz[son[u]]<siz[e])son[u]=e;
}
}
void dfs2(int u,int tp){
top[u]=tp,id[u]=++tid,posi[tid]=u;
if(!son[u])return;dfs2(son[u],tp);
for(int i=head[u];~i;i=node[i].nxt){
int e=node[i].to;
if(!id[e])dfs2(e,e);
}
}
void build(int rt,int l,int r){
if(l==r){seg[rt]=a[posi[l]];return;}
int mid=l+r>>;
build(lson);build(rson);
seg[rt]=seg[rt<<]+seg[rt<<|];
}
void update(int rt,int l,int r){
if(l==r){seg[rt]=v;return;}
int mid=l+r>>;
if(L<=mid)update(lson);
else update(rson);
seg[rt]=seg[rt<<]+seg[rt<<|];
}
LL query(int rt,int l,int r){
if(L<=l&&r<=R)return seg[rt];
int mid=l+r>>;LL temp=;
if(L<=mid)temp+=query(lson);
if(R>mid) temp+=query(rson);
return temp;
}
void lca(int x,int y){
LL res=;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
L=id[top[x]],R=id[x];
res+=query(,,n);
x=fa[top[x]];
}
if(dep[x]<dep[y])swap(x,y);
L=id[y],R=id[x];
res+=query(,,n);
printf("%lld\n",res);
}
int main(){
int i,j,group,x,y,Case=;
group=read();
while(group--){
init();
n=read();
for(i=;i<=n;++i)a[i]=read();
for(i=;i<n;++i){
x=read(),y=read();
++x;++y;
node[hcnt].to=y,node[hcnt].nxt=head[x],head[x]=hcnt++;
node[hcnt].to=x,node[hcnt].nxt=head[y],head[y]=hcnt++;
}
dfs1(,,);dfs2(,);build(,,n);
m=read();
printf("Case %d:\n",++Case);
while(m--){
x=read();
if(!x){
x=read(),y=read();
++x;++y;
lca(x,y);
}
else{
x=read(),y=read();
++x;
L=id[x],v=y;
update(,,n);
}
}
}
return ;
}

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

  1. LightOJ 1348 Aladdin and the Return Journey

    Aladdin and the Return Journey Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged ...

  2. 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 ...

  3. LightOJ 1348 (树链剖分 + 线段树(树状数组))

    题目 Link 分析 典型的树链剖分题, 树链剖分学习资料 Code #include <bits/stdc++.h> using namespace std; const int max ...

  4. Aladdin and the Flying Carpet LightOJ - 1341 (素数打表 + 算术基本定理)

    题意: 就是求a的因数中大于b的有几对 解析: 先把素数打表 运用算术基本定理 求出a的所有因数的个数 然后减去小于b的因数的个数 代码如下: #include <iostream> #i ...

  5. Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)

    题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质 ...

  6. C#复习笔记(4)--C#3:革新写代码的方式(Lambda表达式和表达式树)

    Lambda表达式和表达式树 先放一张委托转换的进化图 看一看到lambda简化了委托的使用. lambda可以隐式的转换成委托或者表达式树.转换成委托的话如下面的代码: Func<string ...

  7. Pairs Forming LCM LightOJ - 1236 (算术基本定理)

    题意: 就是求1-n中有多少对i 和 j 的最小公倍数为n  (i <= j) 解析: 而这题,我们假设( a , b ) = n ,那么: n=pk11pk22⋯pkss, a=pd11pd2 ...

  8. Harmonic Number (II) LightOJ - 1245 (找规律?。。。)

    题意: 求前n项的n/i  的和 只取整数部分 暴力肯定超时...然后 ...现在的人真聪明...我真蠢 觉得还是别人的题意比较清晰 比如n=100的话,i=4时n/i等于25,i=5时n/i等于20 ...

  9. LightOJ 1151 Snakes and Ladders(概率DP + 高斯消元)

    题意:1~100的格子,有n个传送阵,一个把进入i的人瞬间传送到tp[i](可能传送到前面,也可能是后面),已知传送阵终点不会有另一个传送阵,1和100都不会有传送阵.每次走都需要掷一次骰子(1~6且 ...

随机推荐

  1. socket数据收发

    socket读写 TCP协议是面向流的,read和write调用的返回值往往小于参数指定的字节数.对于read调用,如果接收缓冲区中有20字节,请求读100个字节,就会返回20.对于write调用,如 ...

  2. busybox内置ftp服务器用法

    参考:http://blog.chinaunix.net/uid-20564848-id-74041.html 最新的busybox已集成ftp服务器层需ftpd,使用方法如下: 方法一:# tcps ...

  3. BusyBox getty

    linux的登录主要是由两个文件在控制,/usr/sbin/getty来获得用户名,并进行检查用户名是否存在,然后将用户名传递给/usr/bin/login来获取用户输入密码和检查密码是否正确. 所以 ...

  4. WHAT EXACTLY IS WASM ?!

    终于, 我入门了当初很仇视的技术.... 什么是WebAssembly? WebAssembly或WASM是一个编译器目标(由编译器生成的代码),具有二进制格式,允许我们在浏览器上执行C,C ++和R ...

  5. Windows下进程通信方式

    当线程分属于不同进程,也就是分驻在不同的地址空间时,它们之间的通讯需要跨越地址空间的边界,便得采取一些与同一进程中不同线程间通讯不同的方法.在Windows程序中,各个进程之间常常需要交换数据,进行数 ...

  6. kettle中执行sql语句

    一.直接执行sql,不添加任何参数 1.先找出执行sql语句的控件 2.打开控件,填写要执行的sql语句,主要下图中的红框中选项,后面会介绍各个选项的作用 二.执行sql,变量替换选项,变量指的是ke ...

  7. PHP打印重复的东西

    <?php echo str_repeat(" ", 8)?>//连续打印8个空格

  8. 【好文收藏】javascript中event对象详解

    event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有意义.比如,fromEleme ...

  9. Docker之删除container和image

    删除所有停止的container: docker rm $(docker ps -a -q) 删除所有未标记的image docker rmi $(docker images | grep " ...

  10. e649. 处理焦点改变事件

    component.addFocusListener(new MyFocusListener()); public class MyFocusListener extends FocusAdapter ...