大意: 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. 题解——洛谷P3390 【模板】矩阵快速幂(矩阵乘法)

    模板题 留个档 #include <cstdio> #include <algorithm> #include <cstring> #define int long ...

  2. Windows常用的CMD命令

    mspaint 打开画图 write 打开写字板 explorer 打开文件资源管理器 notepad 打开记事本 devmgmt.msc 打开设备管理器 regedit 打开注册表编辑器 Mscon ...

  3. Git回顾

    抄自廖雪峰的官方网站 完整图文请访问https://github.com/Mrlution/study/tree/master/git 关于repository 我认为repository是一个存放代 ...

  4. javascript创建函数的20种方式汇总

    http://www.jb51.net/article/68285.htm 工作中常常会创建一个函数来解决一些需求问题,以下是个人在工作中总结出来的创建函数20种方式,你知道多少? function ...

  5. HDU 5119 Happy Matt Friends(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5119 题意:给出n个数和一个上限m,求从这n个数里取任意个数做异或运算,最后的结果不小于m有多少种取法. 思路: ...

  6. JQ遇到$(‘.xxx’).attr(‘display’)一直返回undefined

    jq attr && jq css 1.1 attr() 方法设置或返回被选元素的属性值 我们就题目遇到的问题做一个测试 //html <div class="div1 ...

  7. 中国地区免费注册bitcointalk论坛教程

    bitcointalk论坛是著名的老牌比特币论坛,中本聪当年也在这里和各路大神探讨.但现在国家的高墙禁止网民访问. 你可能会用一个国外的代理工具来看贴,看贴确实可以,但是如果想注册,注册完后就会发现帐 ...

  8. 关于Test类中不能使用Autowired注入bean的问题

    在测试类中使用AutoWired注解一直不能获取到Bean,调用方法时一直报空指针异常,我有在其他类中使用AutoWired试了下,发现能够生效.问题应该就是处在Test类中,后面找了半天终于找到问题 ...

  9. CentOS6.X、7.X下Jenkins的安装及使用

    一.相关概念 1.1 Jenkins概念: Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenk ...

  10. STL_map.插入

    环境:Win7x64.vs08x86 1.类中这样声明:map<string, list<string>> FmapTagAttr; 2.插入数据时这样: list<st ...