BZOJ 2836 魔法树 链剖裸题~~
正好练练熟练度。。(刷水题谋财害命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 魔法树 链剖裸题~~的更多相关文章
- bzoj 2836 魔法树 —— 树链剖分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2836 树链剖分裸题: 写码五分钟,调码两小时,RE不断,狂交二十五遍,终于找到一处小细节—— ...
- hdu_3966_Aragorn's Story(树链剖分裸题)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给你一棵树,然后给定点之间的路径权值修改,最后单点查询 题解:树链剖分裸题,这里我用树状数 ...
- BZOJ 2243 染色 | 树链剖分模板题进阶版
BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...
- bzoj 2836 魔法树——树链剖分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2836 树剖裸题.然而WA.RE了好久…… 原来是跳 top 的那个地方! top 不相等的时 ...
- BZOJ 2836: 魔法树 (树链剖分+线段树)
板题-记得开longlong #include <cstdio> #include <cctype> #include <cstring> #include < ...
- HDU 5029 Relief grain --树链剖分第一题
题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- UESTC 1591 An easy problem A【线段树点更新裸题】
An easy problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others ...
随机推荐
- python爬虫框架(2)--PySpider框架安装配置
1.安装 1.phantomjs PhantomJS 是一个基于 WebKit 的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速.原生支持各种Web标准:DOM 处理 ...
- Windows版本Apache+php的Xhprof应用__[2]
[计划] “Windows版本Apache+php的Xhprof应用__[1]”中已经解决了下载,配置的问题,所以这里的工作是接着进行的,我们以调试一个 php代码的文件来看看是怎么用xhprof的. ...
- cd命令无效
原因是没有切换盘符步骤一:C:\Documents and Settings\Administrator>d:步骤二:cd D:\Program Files\Python35-32\Script ...
- Node.js的__dirname,__filename,process.cwd(),./的含义
简单说一下这几个路径的意思,: __dirname: 获得当前执行文件所在目录的完整目录名 __filename: 获得当前执行文件的带有完整绝对路径的文件名 process.cwd():获得当前执行 ...
- C++输出斐波那契数列的几种方法
定义: 斐波那契数列指的是这样一个数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 这个数列从第三项开始,每一项都等于前两项之和. 以输出斐波那 ...
- win10 requireAdministrator设置开机自启动无效的解决方案
开发了一个wpf程序,需要管理员权限,设置了requireAdministrator 同时需要开机自启动,所以添加了注册表: using (RegistryKey key = Registry.Cur ...
- GNU Gettext
一.简介 当前,无论是商业还是免费软件都是英文的,并用英文做为文档.直到现在,为使其它非英语语言用户也能够进行交互所做的工作仍然不足,所以这对非英语语言的国家很不利.然而,随着GNU gettext工 ...
- Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at ExTestDrive.main ...
- niginx隐藏入口文件index.php
location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$ last; break; } }
- Entity Relationships
Entity Relationships: Here, you will learn how entity framework manages the relationships between en ...