树上操作[HAOI 2015]
树链剖分裸题:
树剖点这里:传送门
代码:
#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]的更多相关文章
- cogs 1963. [HAOI 2015] 树上操作 树链剖分+线段树
1963. [HAOI 2015] 树上操作 ★★★☆ 输入文件:haoi2015_t2.in 输出文件:haoi2015_t2.out 简单对比时间限制:1 s 内存限制:256 M ...
- 【BZOJ4034】[HAOI2015]树上操作 树链剖分+线段树
[BZOJ4034][HAOI2015]树上操作 Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 ...
- HAOI2015 树上操作
HAOI2015 树上操作 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根 ...
- bzoj千题计划242:bzoj4034: [HAOI2015]树上操作
http://www.lydsy.com/JudgeOnline/problem.php?id=4034 dfs序,树链剖分 #include<cstdio> #include<io ...
- bzoj4034[HAOI2015]树上操作 树链剖分+线段树
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 6163 Solved: 2025[Submit][Stat ...
- 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作
题面:P3178 [HAOI2015]树上操作 好像其他人都嫌这道题太容易了懒得讲,好吧那我讲. 题解:第一个操作和第二个操作本质上是一样的,所以可以合并.唯一值得讲的点就是:第二个操作要求把某个节点 ...
- P3178 [HAOI2015]树上操作
P3178 [HAOI2015]树上操作 思路 板子嘛,其实我感觉树剖没啥脑子 就是debug 代码 #include <bits/stdc++.h> #define int long l ...
- bzoj 4034: [HAOI2015]树上操作 树链剖分+线段树
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4352 Solved: 1387[Submit][Stat ...
- bzoj 4034: [HAOI2015]树上操作 (树剖+线段树 子树操作)
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 6779 Solved: 2275[Submit][Stat ...
随机推荐
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- session与cookie的区别与联系
session与cookie是在做项目中很常用的会话技术,session与cookie也是面试中被问到频率最高的问题,有一次我去面试,面试官就怼着我session与cookie一直问(头都大了),下面 ...
- Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构
用python搭建web服务器,与ajax交互,接收处理Get和Post请求:简单实用,没有用框架,适用于简单需求,更多功能可进行扩展. python有自带模块BaseHTTPServer.CGIHT ...
- HTML,CSS学习笔记
<p>元素,代表是一个段落,单独另起一行</p> <h1>的意思就是主标题</h1> <h1><h2><h3>... ...
- 【技术分析】DowginCw病毒家族解析
作者:钱盾反诈实验室 0x1.背景 近期,钱盾反诈实验室通过钱盾恶意代码智能监测引擎感知并捕获一批恶意应用.由于该批病毒会联网加载"CWAPI"插件,故将其命名为"D ...
- POJ-1250
#include<iostream> #include<string> #include<list> #include<algorithm> using ...
- 自学Python4.1-文件操作
文件操作 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 关闭文件(非必须) 一.打开文件 open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式打开文件,打 ...
- vertical-align 和 img属性 和 鼠标样式
一.vertical-align 一)定义:定义行内元素的基线相对于该所在基线的垂直对齐.(只针对行类块inline/inline-block/<img>,块级不适用!) 二)语法: 三 ...
- scrapy里的selector,不能有正则提取
参考:http://blog.csdn.net/dawnranger/article/details/50037703 Selector 有一个 .re() 方法,用来通过正则表达式来提取数据. 不同 ...
- 如何在VUE项目中添加ESLint
如何在VUE项目中添加ESLint 1. 首先在项目的根目录下 新建 .eslintrc.js文件,其配置规则可以如下:(自己小整理了一份),所有的代码如下: // https://eslint.or ...