[BZOJ4034][HAOI2015]T2 做题笔记
2016-03-22 23:01
387 查看
·· / ·– ·· ·-·· ·-·· / ·–· · ·-· ··· ·· ··· - / ··- -· - ·· ·-·· / ·· / ·– ·· -·
题目来源:http://www.lydsy.com/JudgeOnline/problem.php?id=4034
[BZOJ4196][NOI2015]软件包管理器的既视感。。
这题是树链剖分+dfs序比较裸的题目,唯一需要注意的就是要开long long
题目来源:http://www.lydsy.com/JudgeOnline/problem.php?id=4034
[BZOJ4196][NOI2015]软件包管理器的既视感。。
这题是树链剖分+dfs序比较裸的题目,唯一需要注意的就是要开long long
#include <cstdio> #include <algorithm> #define lch now<<1 #define rch now<<1|1 const int N=200000; typedef long long ll; using namespace std; ll C ; int size ,son ,fa ,dep ; int w ,ed ,top ,id[N<<2],cnt=0; int ver[N<<1],nxt[N<<1],head ,tot=1; int n,m; struct Tree { ll sum,del; }I[N<<2]; void add (int u,int v) { ver[++tot]=v;nxt[tot]=head[u];head[u]=tot; } void dfs_1 (int u,int from) { int v;size[u]=1;son[u]=0;fa[u]=from; for (int i=head[u];i;i=nxt[i]) { v=ver[i]; if (v==fa[u]) continue; dep[v]=dep[u]+1; dfs_1(v,u); size[u]+=size[v]; if (!son[u]||size[son[u]]<size[v]) son[u]=v; } } void dfs_2 (int u,int st) { int v;w[u]=++cnt;top[u]=st;id[cnt]=u; if (son[u]) dfs_2(son[u],st); for (int i=head[u];i;i=nxt[i]) { v=ver[i]; if (v==son[u]||v==fa[u]) continue; dfs_2(v,v); } ed[u]=cnt; } inline void pushup (int now) { I[now].sum=I[lch].sum+I[rch].sum; } inline void pushdown (int now,int l,int r) { if (I[now].del!=0) { int mid=(l+r)>>1; I[lch].del+=I[now].del;I[rch].del+=I[now].del; I[lch].sum+=I[now].del*(mid-l+1); I[rch].sum+=I[now].del*(r-mid); I[now].del=0; } } void build (int now,int l,int r) { if (l==r) { I[now].sum=C[id[l]]; I[now].del=0; return ; } int mid=(l+r)>>1; build(lch,l,mid); build(rch,mid+1,r); pushup(now); } void change (int now,int l,int r,int x,int y,ll del) { if (x<=l&&r<=y) { I[now].del+=del; I[now].sum+=del*(r-l+1); return ; } int mid=(l+r)>>1; pushdown(now,l,r); if (x<=mid) change(lch,l,mid,x,y,del); if (y>mid) change(rch,mid+1,r,x,y,del); pushup(now); } ll query (int now,int l,int r,int x,int y) { if (x<=l&&r<=y) return I[now].sum; int mid=(l+r)>>1;ll ans=0; pushdown(now,l,r); if (x<=mid) ans+=query(lch,l,mid,x,y); if (y>mid) ans+=query(rch,mid+1,r,x,y); return ans; } ll getans (int x,int y) { ll ans=0; while (top[x]!=top[y]) { ans+=query(1,1,n,w[top[x]],w[x]); x=fa[top[x]]; } ans+=query(1,1,n,w[y],w[x]); return ans; } int main () { int u,v,opt,x,y; scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%lld",&C[i]); for (int i=1;i<n;i++) { scanf("%d%d",&u,&v); add(u,v); add(v,u); } dfs_1(1,0); dfs_2(1,1); build(1,1,n); for (int i=1;i<=m;i++) { scanf("%d",&opt); if (opt==1) { scanf("%d%d",&x,&y); change(1,1,n,w[x],w[x],y); } else if (opt==2) { scanf("%d%d",&x,&y); change(1,1,n,w[x],ed[x],y); } else if (opt==3) { scanf("%d",&x); printf("%lld\n",getans(x,1)); } } return 0; }
相关文章推荐
- 使用 history 对象
- QT 模拟点击网页
- Android中的软引用、弱引用
- 不使用库函数实现字符串复制函数strCopy
- angular-API
- Spring1
- 软考信息系统监理师,2016年3月18日作业
- 如何提高循环效率
- Phantomjs+Nodejs+Mysql数据抓取(2.抓取图片)
- LeetCode 143. Reorder List
- lua gc 优化方案
- Android数据库框架
- 无线AP和无线路由器区别
- Spring IOC
- angular-输入验证
- 无线AP和无线路由器区别
- 分析与设计数据库模型的简单过程
- 【真题索引】2016/03/22知识点整理
- [Java] 实验4参考代码
- eclipse 安装maven 插件