正好练练熟练度。。(刷水题谋财害命QAQ)

#include<cstdio>
#include<iostream>
#define ll long long
#define R register ll
#define ls (tr<<1)
#define rs (tr<<1|1)
const int M=;
using namespace std;
inline ll g() {
R ret=; register char ch; while(!isdigit(ch=getchar())) ;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret;
}
int n,m,tr,cnt,num,mod;
int vr[M<<],nxt[M<<],fir[M],dfn[M],pre[M],d[M],sz[M],son[M],top[M],rw[M],w[M];
ll tg[M<<],sum[M<<];
inline void add(int u,int v) {vr[++cnt]=v,nxt[cnt]=fir[u],fir[u]=cnt;}
void dfs(int u) { sz[u]=; R mx=;
for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
if(!d[v]) {
pre[v]=u; d[v]=d[u]+; dfs(v); sz[u]+=sz[v];
if(sz[v]>mx) son[u]=v,mx=sz[v];
}
}
}
void dfs_(int u,int tp) {
top[u]=tp,dfn[u]=++num,rw[num]=u;
if(son[u]) dfs_(son[u],tp);
for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
if(v!=pre[u]&&v!=son[u]) dfs_(v,v);
}
}
inline void build(int tr,int l,int r) {
if(l==r) {sum[tr]=; return ;}
R md=(l+r)>>; build(ls,l,md),build(rs,md+,r);
sum[tr]=sum[ls]+sum[rs];
}
inline void spread(int tr,int len) {
if(tg[tr])
tg[ls]+=tg[tr],sum[ls]+=(len-(len>>))*tg[tr],
tg[rs]+=tg[tr],sum[rs]+=(len>>)*tg[tr],tg[tr]=;
}
inline ll query(int tr,int l,int r,int LL,int RR) { R ret=;
if(LL<=l&&r<=RR) {return sum[tr];} spread(tr,r-l+); R md=(l+r)>>;
if(LL<=md) ret+=query(ls,l,md,LL,RR);
if(RR>md) ret+=query(rs,md+,r,LL,RR); return ret;
}
inline void update(int tr,int l,int r,int LL,int RR,int inc) {
if(LL<=l&&r<=RR) {tg[tr]+=inc,sum[tr]+=(r-l+)*inc; return ;}
spread(tr,r-l+); R md=(l+r)>>;
if(LL<=md) update(ls,l,md,LL,RR,inc);
if(RR>md) update(rs,md+,r,LL,RR,inc);
sum[tr]=sum[ls]+sum[rs];
}
inline void change(int u,int v,int inc) {
while(top[u]!=top[v]) {
if(d[top[u]]<d[top[v]]) swap(u,v);
update(,,n,dfn[top[u]],dfn[u],inc);
u=pre[top[u]];
} if(dfn[u]>dfn[v]) swap(u,v);
update(,,n,dfn[u],dfn[v],inc);
}
signed main() {
n=g();
for(R i=,u,v;i<n;++i) u=g(),v=g(),add(u,v),add(v,u); cnt=;
d[tr]=; dfs(tr),dfs_(tr,tr); build(,,n); m=g();
for(R i=;i<=m;++i) { register char ch; while(!isalpha(ch=getchar()));
R u=g(),v,inc;
if(ch=='A') v=g(),inc=g(),change(u,v,inc);
else if(ch=='Q') printf("%lld\n",query(,,n,dfn[u],dfn[u]+sz[u]-));
}
}

2019.04.22

BZOJ 2836 魔法树 链剖裸题~~的更多相关文章

  1. bzoj 2836 魔法树 —— 树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2836 树链剖分裸题: 写码五分钟,调码两小时,RE不断,狂交二十五遍,终于找到一处小细节—— ...

  2. hdu_3966_Aragorn's Story(树链剖分裸题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给你一棵树,然后给定点之间的路径权值修改,最后单点查询 题解:树链剖分裸题,这里我用树状数 ...

  3. BZOJ 2243 染色 | 树链剖分模板题进阶版

    BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...

  4. bzoj 2836 魔法树——树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2836 树剖裸题.然而WA.RE了好久…… 原来是跳 top 的那个地方! top 不相等的时 ...

  5. BZOJ 2836: 魔法树 (树链剖分+线段树)

    板题-记得开longlong #include <cstdio> #include <cctype> #include <cstring> #include < ...

  6. HDU 5029 Relief grain --树链剖分第一题

    题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...

  7. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  8. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  9. UESTC 1591 An easy problem A【线段树点更新裸题】

    An easy problem A Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

随机推荐

  1. mysql case

    1.table CREATE TABLE `lee`(`id` INT(10) NOT NULL auto_increment,`name` varchar(20) DEFAULT null,`bir ...

  2. solr增量数据配置说明

    转帖地址:http://www.blogjava.net/conans/articles/379546.html 以下资料整理自网络,觉的有必要合并在一起,这样方便查看.主要分为两部分,第一部分是对& ...

  3. 数据存储的两种方式:Cookie 和Web Storage(转)

    数据存储的两种方式:Cookie 和Web Storage   数据存储的两种方式:Cookie 和Web Storage 1.Cookie Cookie的作用就像你去超市购物时,第一次给你办张购物卡 ...

  4. 第一次WM_PAINT事件执行前显示白色框 的解决办法

    界面显示前,总是会显示白色或白加黑的窗体, 开始以为是图片加载慢的原因,后来发现这个框是在第一次WM_PAINT执行前显示的. 解决办法很简单,在CreateWindow的时候,加上WS_POPUP样 ...

  5. 用C++的基本算法实现十个数排序

    冒泡排序法 原理: 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成. 冒泡排序算法的运作如下 ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化

    一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /*********************************************** ...

  7. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

  8. 神奇的overflow:hidden及其背后的原理

    先来看两个overflow:hidden的使用例子 1.嵌套布局内部块元素设置float:left时,导致外部元素塌方,高度为0的问题. <div class="wrap"& ...

  9. [学习笔记]fork深入理解

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include <unistd.h> ...

  10. java获取Excel的导出

    import java.io.*; import org.apache.commons.io.FileUtils; import org.apache.poi.hssf.usermodel.HSSFC ...