大意: n节点树, 每个点有权值, 三种操作: 1,换根. 2, lca(u,v)的子树权值全部增加x. 3, 查询子树权值和.

先不考虑换根, 考虑子树x加v的贡献

(1)对fa[x]到根的树链贡献为sz[x]*v;

(2)对x子树内的点y贡献为sz[y]*v;

步骤(1)可以用单点更新子树求和实现, 步骤(2)可以子树更新单点求和实现

然后就是换根板子题了.

感觉蠢得不行的题啊, 还是打了好久, 怎么能这么菜啊

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) {REP(i,1,n) cout<<a[i]<<' ';hr;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, a[N];
vector<int> g[N];
int sz[N], dep[N], fa[N], L[N], R[N];
int son[N], top[N];
ll c[N], c2[N];
void add(int x, ll v) {
for (; x<=n; x+=x&-x) c[x]+=v;
}
void add2(int x, int v) {
for (; x; x^=x&-x) c2[x]+=v;
}
void add2(int l, int r, int v) {
add2(r,v),add2(l-1,-v);
}
ll qry(int x) {
ll ret = 0;
for (; x; x^=x&-x) ret += c[x];
return ret;
}
ll qry(int l, int r) {
return qry(r)-qry(l-1);
}
ll qry2(int x) {
ll ret = 0;
for (; x<=n; x+=x&-x) ret += c2[x];
return ret;
}
void dfs(int x, int d, int f) {
sz[x] = 1, dep[x]=d, fa[x]=f, L[x]=++*L, add(L[x],a[x]);
for (int y:g[x]) if (y!=f) {
dfs(y,d+1,x); sz[x]+=sz[y];
if (sz[y]>sz[son[x]]) son[x]=y;
}
R[x]=*L;
}
void dfs2(int x, int tf) {
top[x]=tf;
if (son[x]) dfs2(son[x],tf);
for (int y:g[x]) if (y!=fa[x]&&y!=son[x]) dfs2(y,y);
}
int lca(int x, int y) {
while (top[x]!=top[y]) {
if (dep[top[x]]<dep[top[y]]) swap(x,y);
x = fa[top[x]];
}
if (dep[x]>dep[y]) swap(x,y);
return x;
}
int lca(int u, int v, int rt) {
int L = lca(u,v);
if (lca(rt,L)!=L) return L;
int x=lca(u,rt),y=lca(v,rt);
if (dep[x]<dep[y]) swap(x,y);
return x;
}
int calc(int x, int y) {
int f = x, pre = 0, lca;
while (top[x]!=top[y]) {
if (dep[top[x]]<dep[top[y]]) swap(x,y);
pre = top[x], x = fa[pre];
}
if (x==y) lca=x;
else lca=dep[x]<dep[y]?x:y;
if (lca!=f) return 0;
return fa[pre]==f?pre:son[f];
}
void update(int x, int v) {
add(L[fa[x]],(ll)sz[x]*v);
add2(L[x],R[x],v);
}
void update(int x, int v, int rt) {
if (x==rt) return add2(1,n,v);
int t = calc(x,rt);
if (!t) return update(x,v);
add2(1,n,v),update(t,-v);
}
ll query(int x) {
return qry(L[x],R[x])+sz[x]*qry2(L[x]);
}
ll query(int x, int rt) {
if (x==rt) return query(1);
int t = calc(x,rt);
if (!t) return query(x);
return query(1)-query(t);
} int main() {
int q, rt = 1;
scanf("%d%d", &n, &q);
REP(i,1,n) scanf("%d", a+i);
REP(i,2,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
dfs(1,0,0),dfs2(1,1);
while (q--) {
int op, u, v, w;
scanf("%d%d", &op, &u);
if (op==1) rt=u;
else if (op==2) {
scanf("%d%d", &v, &w);
update(lca(u,v,rt),w,rt);
}
else printf("%lld\n", query(u,rt));
}
}

Jamie and Tree CodeForces - 916E (换根)的更多相关文章

  1. CF1187E Tree Painting【换根dp】

    题目传送门 题意 一棵$N$个节点的树,初始时所有的节点都是白色,第一次可以选择任意一个把它涂成黑色.接下来,只能把与黑色节点原来相连的白色节点涂成黑色(涂成黑色的点视为被删去,与其它节点不相连).每 ...

  2. Codeforces 916E Jamie and Tree (换根讨论)

    题目链接  Jamie and Tree 题意  给定一棵树,现在有下列操作: $1$.把当前的根换成$v$:$2$.找到最小的同时包含$u$和$v$的子树,然后把这棵子树里面的所有点的值加$x$: ...

  3. CodeForces 916E Jamie and Tree(树链剖分+LCA)

    To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with n vertices, numb ...

  4. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  5. codeforces 916E Jamie and Tree dfs序列化+线段树+LCA

    E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces 1092 F Tree with Maximum Cost (换根 + dfs)

    题意: 给你一棵无根树,每个节点有个权值$a_i$,指定一个点u,定义$\displaystyle value = \sum^v a_i*dist(u,v)$,求value的最大值 n,ai<= ...

  7. E. Tree Painting(树形换根dp)

    http://codeforces.com/contest/1187/problem/E 分析:问得分最高,实际上就是问以哪个节点出发得到的分数最多,而呈现成代码形式就变成了换根,max其得分!!!而 ...

  8. Codeforces 891D - Sloth(换根 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 换根 dp 好题. 为啥没人做/yiw 首先 \(n\) 为奇数时答案显然为 \(0\),证明显然.接下来我们着重探讨 \(n\) 是偶数 ...

  9. Codeforces 997D - Cycles in product(换根 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 一种换根 dp 的做法. 首先碰到这类题目,我们很明显不能真的把图 \(G\) 建出来,因此我们需要观察一下图 \(G\) 有哪些性质.很 ...

随机推荐

  1. P4137 Rmq Problem / mex

    目录 链接 思路 线段树 莫队 链接 https://www.luogu.org/problemnew/show/P4137 思路 做了好几次,每次都得想一会,再记录一下 可持久化权值线段树 区间出现 ...

  2. 【做题】arc070_f-HonestOrUnkind——交互+巧妙思维

    做的第一道交互题-- 首先,有解的一个必要条件是\(a>b\).否则,即当\(a<=b\)时,可以有\(a\)个unkind的人假装自己就是那\(a\)个honest的人.(彼此之间都说是 ...

  3. HBase底层存储原理

    HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已! 首先HBase不同于一般的关系数据库, 它是一个适合于非结构化数据存储的数 ...

  4. Hadoop技术内幕1——源代码环境准备

    Hadoop核心 1.HDFS:高容错性.高伸缩性……,允许用户将Hadoop部署在廉价的硬件上,构建分布式系统 2.MapReduce:分布式计算框架,允许用户在不了解分布式系统底层细节的情况下,开 ...

  5. 51nod 1215 数组的宽度(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 题意: 思路: 计算出以第i个数为最大值的区间范围,l_max[i ...

  6. javaScript 内置对象-Array数组

    Array 对象方法 方法 描述 concat() 连接两个或更多的数组,并返回结果. join() 把数组的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. pop() 删除并返回数组的最后一 ...

  7. 【Python】图形界面

    # [[图形界面]]'''Python支持多种图形界面的第三方库,包括TkwxWidgetsQtGTK但是Python自带的库是支持Tk的Tkinter,无需安装任何包,可直接使用.''' #[Tki ...

  8. 【BZOJ】3295: [Cqoi2011]动态逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3295 mamaya,弱鸡xrdog终于会写树套树啦.... 将树状数组中每一个节点看成一棵 ...

  9. SHU oj 422 风力观测 线段树

    风力观测 发布时间: 2017年7月9日 18:17   最后更新: 2017年7月9日 21:04   时间限制: 1000ms   内存限制: 128M 描述 小Y正在观测y地区的风力情况,他在一 ...

  10. ubuntu 安装pip3 遇到Ignoring ensurepip failure: pip 8.1.1 requires SSL/TLS错误

    3.5版本之后的会自动安装pip,所以我们直接从官网下载3.5.2,下载地址:https://www.python.org/ftp/python/ 下载以后,可以用命令解压,也可以右键进行解压, ta ...