树链剖分裸题:

树剖点这里:传送门

代码:

#include<bits/stdc++.h>
#define sight(c) ('0'<=c&&c<='9')
#define LL long long
#define gc nc
#define add(x,y) x+=(y)
#define L(x) (x&-x)
#define eho(x) for(int i=head[x];i;i=net[i])
#define N 100007
#define abs(x) ((x)>0?(x):(-x))
LL q1[N],q2[N],gg,sum[N],a[N],T,G;
int tot,fall[N<<],net[N<<],head[N],top[N],son[N],f[N],dp[N],siz[N],be[N],ed[N],ok
,n,m,A,B,t[N],op,x,y,z,dla,OS;
inline char nc(){
static char buf[],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline void swap(int &x,int &y) {x^=y; y^=x; x^=y;}
inline void read(LL &x){
static char c; static int b;
for(b=,c=gc();!sight(c);c=gc()) if (c=='-') b=-;
for(x=;sight(c);c=gc()) x=x*+c-; x*=b;
}
inline void read(int &x){
static char c; static int b;
for(b=,c=gc();!sight(c);c=gc()) if (c=='-') b=-;
for(x=;sight(c);c=gc()) x=x*+c-; x*=b;
}
void write(LL x){if (x<) {putchar(''+x);return;}write(x/); putchar(''+x%);}
void writes(LL x){if (x<) putchar('-'); write(abs(x));}
inline void ADd(int x,int y) {
fall[++tot]=y; net[tot]=head[x]; head[x]=tot;
}
inline void Add(LL *A,int x,LL dla) {for (;x<N;x+=L(x)) add(A[x],dla);}
inline void adds(int l,int r,LL x) {
Add(q1,l,x); Add(q1,r+,-x); Add(q2,l,l*x); Add(q2,r+,-(r+)*x);
}
inline LL Query(LL *A,int x){for(G=;x;x-=L(x)) add(G,A[x]);return G;}
inline LL qurey(int l){
return (sum[l]+(l+)*1ll*Query(q1,l)-Query(q2,l));
}
void dfs(int x,int fa){
siz[x]=; son[x]=-; dp[x]=dp[fa]+; f[x]=fa;
eho(x) if (fall[i]^fa) {
dfs(fall[i],x); siz[x]+=siz[fall[i]];
if ((!(~son[x]))||siz[fall[i]]>siz[son[x]]) son[x]=fall[i];
}
}
void dfs2(int x,int las){
t[++ok]=x;top[x]=las; be[x]=ok;
if (~son[x]) dfs2(son[x],las);
eho(x) if ((fall[i]^f[x])&&(fall[i]^son[x])) dfs2(fall[i],fall[i]); ed[x]=ok;
}
void apd(int x,int y,LL dla){
while (top[x]!=top[y]) {
if (dp[top[x]]<dp[top[y]]) swap(x,y);
adds(be[top[x]],be[x],dla);
x=f[top[x]];
} if (dp[x]>dp[y]) swap(x,y);
adds(be[x],be[y],dla);
}
LL query_path(int x,int y) {
LL O=;
while (top[x]!=top[y]) {
if (dp[top[x]]<dp[top[y]]) swap(x,y);
add(O,qurey(be[x])-qurey(be[top[x]]-));
x=f[top[x]];
} if (dp[x]>dp[y]) swap(x,y);
add(O,qurey(be[y])-qurey(be[x]-));
return O;
}
signed main () {
// freopen("a.in","r",stdin);
read(n); read(m);
for (int i=;i<=n;i++) read(a[i]);
for (int i=;i<n;i++) {read(A); read(B); ADd(A,B); ADd(B,A); }
dfs(n,); dfs2(n,n);
for (int i=;i<=n;i++) sum[i]=sum[i-],add(sum[i],a[t[i]]);
while (m--) {
read(op);
switch (op) {
case : read(x); read(z); adds(be[x],be[x],z); break;
case : read(x); read(z); adds(be[x],ed[x],z); break;
case : read(x); writes(query_path(,x)); putchar('\n'); break;
}
} return ;
}

树上操作[HAOI 2015]的更多相关文章

  1. cogs 1963. [HAOI 2015] 树上操作 树链剖分+线段树

    1963. [HAOI 2015] 树上操作 ★★★☆   输入文件:haoi2015_t2.in   输出文件:haoi2015_t2.out   简单对比时间限制:1 s   内存限制:256 M ...

  2. 【BZOJ4034】[HAOI2015]树上操作 树链剖分+线段树

    [BZOJ4034][HAOI2015]树上操作 Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 ...

  3. HAOI2015 树上操作

    HAOI2015 树上操作 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根 ...

  4. bzoj千题计划242:bzoj4034: [HAOI2015]树上操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=4034 dfs序,树链剖分 #include<cstdio> #include<io ...

  5. bzoj4034[HAOI2015]树上操作 树链剖分+线段树

    4034: [HAOI2015]树上操作 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 6163  Solved: 2025[Submit][Stat ...

  6. 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作

    题面:P3178 [HAOI2015]树上操作 好像其他人都嫌这道题太容易了懒得讲,好吧那我讲. 题解:第一个操作和第二个操作本质上是一样的,所以可以合并.唯一值得讲的点就是:第二个操作要求把某个节点 ...

  7. P3178 [HAOI2015]树上操作

    P3178 [HAOI2015]树上操作 思路 板子嘛,其实我感觉树剖没啥脑子 就是debug 代码 #include <bits/stdc++.h> #define int long l ...

  8. bzoj 4034: [HAOI2015]树上操作 树链剖分+线段树

    4034: [HAOI2015]树上操作 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4352  Solved: 1387[Submit][Stat ...

  9. bzoj 4034: [HAOI2015]树上操作 (树剖+线段树 子树操作)

    4034: [HAOI2015]树上操作 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 6779  Solved: 2275[Submit][Stat ...

随机推荐

  1. codeforces 895A Pizza Separation 枚举

    codeforces 895A Pizza Separation 题目大意: 分成两大部分,使得这两部分的差值最小(注意是圆形,首尾相连) 思路: 分割出来的部分是连续的,开二倍枚举. 注意不要看成0 ...

  2. vs 2015工具栏添加Tab Order

    1. 在工具栏右键,弹出菜单,选中“Customize”菜单项. 2. 选中Commands标签页,选择Toolbar,选择自己要加入的Tab order的类别,之后点击“Add Command”按钮 ...

  3. models中的pk主键用法

    class FrontUserModel(models.Model): uid = models.UUIDField(primary_key=True,default=uuid.uuid4) emai ...

  4. 2.python的文件类型、变量数值和字符串练习

    1.python的文件类型 .源代码 -python 源代码文件以"py"为扩展名,由python程序解释,不需要编译. 2.字节代码(编译的) -python源码文件经编译后生成 ...

  5. Tabhost中Activity绑定Service

    在android中,一个Activity绑定一个Service组件我们一般用Context().bindService方法就能够.可是假设这个 Activity属于一个Tabhost的话就不行了,在网 ...

  6. SOJ--Zig-Zag

    Zig-Zag 在图形图像处理中经常须要将一个二维的图像矩阵转化为一维的向量.二维化一维的过程实际上就是将二维数组的元素按某种顺序构成一维数组. 一种经常使用的序列叫"Zig-Zag&quo ...

  7. Android中关于JNI 的学习(三)在JNI层訪问Java端对象

    前面两篇文章简介了JNI层跟Java层的一些相应关系,包含方法名,数据类型和方法名称等,相信在理论层面.可以非常好地帮助我们去了解JNI在Native本地开发中的作用,对JNI的一些概念也有了一个初步 ...

  8. 使用milang出错:LookupError: unknown encoding: idna

    今天同事安装了milang,结果发现例如以下出错: Traceback (most recent call last): File "F:\vmid.py", line 11, i ...

  9. 项目实战13—企业级虚拟化Virtualization-KVM技术

    项目实战系列,总架构图 http://www.cnblogs.com/along21/p/8000812.html KVM的介绍.准备工作和qemu-kvm 命令详解 1.介绍 (1)介绍 KVM:就 ...

  10. ssm学习(四)--完整的增删改查demo

    上一篇文章简要介绍了将sping mvc加入整个框架,算是完成了ssm的集成.本节继续前面的内容,结合spring mvc做一个简单的增删改查demo. 1.首先,重写一下GeckoList.jsp页 ...