【树链剖分+树状数组区间修改区间和】BZOJ4034(LOJ#2125)[HAOI2015]树上操作
来源:互联网 发布:课件制作软件director 编辑:程序博客网 时间:2024/06/16 18:11
题面在这里
还可以双倍经验
其实就是树链剖分裸题……
只要注意到树链剖分序就是DFS序就可做了
示例程序:
#include<cstdio>#include<algorithm>#define lowbit(x) ((x)&-(x))typedef long long ll;using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline int red(){ int res=0,f=1;char ch=nc(); while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();} while ('0'<=ch&&ch<='9') res=res*10+ch-48,ch=nc(); return res*f;}const int maxn=100005,maxe=200005;int n,q,w[maxn];int tot,son[maxe],lnk[maxn],nxt[maxe];inline void add(int x,int y){ son[++tot]=y;nxt[tot]=lnk[x];lnk[x]=tot;}ll c[maxn],cc[maxn];void ist(ll *BIT,int x,ll w){ for (int i=x;i<=n;i+=lowbit(i)) BIT[i]+=w;}ll qry(ll *BIT,int x){ ll res=0; for (int i=x;i;i-=lowbit(i)) res+=BIT[i]; return res;}void insert(int l,int r,ll w){ ist(c,r+1,-w);ist(c,l,w); ist(cc,r+1,-w*r);ist(cc,l,w*(l-1));}ll query(int l,int r){ l--; return qry(c,r)*r-qry(cc,r)-(qry(c,l)*l-qry(cc,l));}int dep[maxn],H_son[maxn],siz[maxn],fa[maxn],tim;void dfs1(int x){ dep[x]=dep[fa[x]]+1;siz[x]=1; for (int j=lnk[x];j;j=nxt[j]) if (son[j]!=fa[x]){ fa[son[j]]=x;dfs1(son[j]); siz[x]+=siz[son[j]]; if (siz[H_son[x]]<siz[son[j]]) H_son[x]=son[j]; }}int top[maxn],in[maxn],out[maxn];void dfs2(int x,int tp){ top[x]=tp;in[x]=++tim; if (H_son[x]) dfs2(H_son[x],tp); for (int j=lnk[x];j;j=nxt[j]) if (son[j]!=fa[x]&&son[j]!=H_son[x]) dfs2(son[j],son[j]); out[x]=tim;}ll query_path(int x,int y){ ll res=0; while (top[x]!=top[y]){ if (dep[top[x]]<dep[top[y]]) swap(x,y); res+=query(in[top[x]],in[x]); x=fa[top[x]]; } if (in[x]>in[y]) swap(x,y); res+=query(in[x],in[y]); return res;}int main(){ n=red(),q=red(); for (int i=1;i<=n;i++) w[i]=red(); for (int i=1,x,y;i<n;i++) x=red(),y=red(),add(x,y),add(y,x); fa[1]=1;dfs1(1);dfs2(1,1); for (int i=1;i<=n;i++) insert(in[i],in[i],w[i]); while (q--){ int t=red(); if (t==1){ int x=red(),a=red(); insert(in[x],in[x],a); }else if (t==2){ int x=red(),a=red(); insert(in[x],out[x],a); }else{ int x=red(); printf("%lld\n",query_path(1,x)); } } return 0;}
阅读全文
1 0
- 【树链剖分+树状数组区间修改区间和】BZOJ4034(LOJ#2125)[HAOI2015]树上操作
- [bzoj4034][HAOI2015]树上操作 dfs序+树状数组
- 【BZOJ4034】树上操作(HAOI2015)-树链剖分
- 【bzoj4034】【HAOI2015】【树上操作】【树链剖分】
- bzoj4034 [HAOI2015]树上操作(树链剖分)
- 【树链剖分】bzoj4034: [HAOI2015]树上操作
- bzoj4034 [HAOI2015]树上操作
- 【bzoj4034】[HAOI2015]树上操作
- BZOJ4034: [HAOI2015]树上操作
- [bzoj4034][HAOI2015]树上操作
- bzoj4034: [HAOI2015]树上操作
- 【BZOJ4034】【HAOI2015】树上操作
- bzoj4034 [HAOI2015]树上操作
- bzoj4034: [HAOI2015]树上操作
- BZOJ4034 [HAOI2015]树上操作
- 【bzoj4034】[HAOI2015]树上操作(树链剖分+dfs序)
- bzoj4034: [HAOI2015]树上操作(树链剖分+线段树)
- BZOJ4034: [HAOI2015]树上操作(洛谷P3178)
- SQL 聚集索引和非聚集索引 案例分析讲解
- 【性能学习资料之一】
- aws 的dynamodb 通过case class方式保存
- shell 多个程序 log 的备份(二)
- 移动端车牌识别技术原理分析
- 【树链剖分+树状数组区间修改区间和】BZOJ4034(LOJ#2125)[HAOI2015]树上操作
- 【最佳实践】SequoiaDB数据分区实践与经验
- quill——简单的富文本编辑器
- Matlab与C/C++混合编程接口及应用
- 自定义进度条样式progressBar
- 间隔单元装置主要功能
- R交互图_shiny包
- QQ连连看神眼制作
- final关键字