LightOJ 1348(Aladdin and the Return Journey )
题目链接:传送门
题目大意:一棵无根树,每个点上有权值,两种操作,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 )的更多相关文章
- LightOJ 1348 Aladdin and the Return Journey
Aladdin and the Return Journey Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged ...
- 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 (树链剖分 + 线段树(树状数组))
题目 Link 分析 典型的树链剖分题, 树链剖分学习资料 Code #include <bits/stdc++.h> using namespace std; const int max ...
- Aladdin and the Flying Carpet LightOJ - 1341 (素数打表 + 算术基本定理)
题意: 就是求a的因数中大于b的有几对 解析: 先把素数打表 运用算术基本定理 求出a的所有因数的个数 然后减去小于b的因数的个数 代码如下: #include <iostream> #i ...
- Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)
题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质 ...
- C#复习笔记(4)--C#3:革新写代码的方式(Lambda表达式和表达式树)
Lambda表达式和表达式树 先放一张委托转换的进化图 看一看到lambda简化了委托的使用. lambda可以隐式的转换成委托或者表达式树.转换成委托的话如下面的代码: Func<string ...
- Pairs Forming LCM LightOJ - 1236 (算术基本定理)
题意: 就是求1-n中有多少对i 和 j 的最小公倍数为n (i <= j) 解析: 而这题,我们假设( a , b ) = n ,那么: n=pk11pk22⋯pkss, a=pd11pd2 ...
- Harmonic Number (II) LightOJ - 1245 (找规律?。。。)
题意: 求前n项的n/i 的和 只取整数部分 暴力肯定超时...然后 ...现在的人真聪明...我真蠢 觉得还是别人的题意比较清晰 比如n=100的话,i=4时n/i等于25,i=5时n/i等于20 ...
- LightOJ 1151 Snakes and Ladders(概率DP + 高斯消元)
题意:1~100的格子,有n个传送阵,一个把进入i的人瞬间传送到tp[i](可能传送到前面,也可能是后面),已知传送阵终点不会有另一个传送阵,1和100都不会有传送阵.每次走都需要掷一次骰子(1~6且 ...
随机推荐
- BeamNG.drive物理引擎评鉴
BeamNG.drive是一款由BeamNG公司开发并于2013年首次发布的软体物理模拟游戏.作为模拟游戏,特别是物理模拟的粉丝,我早早就开始使用BeamNG.drive.我立即对崩溃的准确性和细节印 ...
- ddddddd
尊敬的老师们: 我在各方面表现优异.在学习方面,始终将学习放在首位,学习成绩名列前茅,在以往考试中从没有挂科记录,并积极参加校内.校外比赛,且多次获奖:在思想方面,积极向党组织靠拢,一直以一名优秀党员 ...
- rails下mysql出错问题mysql_api,blog/text
问题一:提示出错:cannot load such file -- mysql/mysql_api (LoadError) 此时我们回来看gem install mysql 时提示 At the ti ...
- laravel 5.1 性能优化对比 - 框架提供的方法
写了一个项目发现性能不如人意. 于是便测试下, 看下性能瓶颈在什么地方. 使用 ab -n 20 http://www.lartest.com/ 软件环境: OS : windows 8.1 CPU: ...
- r语言,安装外部包 警告: 无法将临时安装
安装R语言中的外部包时,出现错误提示 试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/3.3/ggplot2_2 ...
- android jni aotf 错误
在jni中希望将字符串转成浮点型数据,使用了atof函数.出现错误: failed: Cannot load library: soinfo_relocate(linker.cpp:975): can ...
- (转)学习linux的几本书
成为一名精通 Linux 程序设计的高级程序员一直是不少朋友孜孜以求的目标.根据中华英才网统计数据,北京地区 Linux 程序员月薪平均为 Windows 程序员的 1.8 倍.Java 程序员的 2 ...
- Onject.Instantiate实例
该函数有两个函数原型: Object Instantiate(Object original,Vector3 position,Quaternion rotation); Onject Instant ...
- CentOS服务器上搭建Gitlab安装步骤、中文汉化详细步骤、日常管理以及异常故障排查
一, 服务器快速搭建gitlab方法 可以参考gitlab中文社区 的教程centos7安装gitlab:https://www.gitlab.cc/downloads/#centos7centos6 ...
- 如何强制关闭Tomcat
用Myeclipse打开后启动Tomcat提示信息为:Address already in use: JVM_Bind:80 ,表示该地址和端口已经被占用显示已经打开了.但是关不掉他...所以只能去关 ...