poj 3237 树链剖分
来源:互联网 发布:php 图片水印 编辑:程序博客网 时间:2024/06/01 21:37
这几天,看到别的队都有会树链剖分的,于是就来学了一下。。。
就是把边的权值变成了深度较深的那个点的权值
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn = 100010;int sum[maxn*4],add[maxn*4];struct si{ int f,t,c,next;}side[maxn*2];int head[maxn];int tid[maxn],ran[maxn],num[maxn],son[maxn];int top[maxn],fa[maxn],siz[maxn],dep[maxn];int tim,cnt;int n,q,s;void dfs1(int u,int father,int d){ dep[u]=d; fa[u]=father; siz[u]=1; for(int i=head[u]; i!=-1; i=side[i].next) { int v=side[i].t; if(v!=father) { dfs1(v,u,d+1); siz[u]+=siz[v]; if(son[u]==-1||siz[v]>siz[son[u]]) son[u]=v; } }}void dfs2(int u,int tp){ //cout<<888888<<endl; top[u]=tp; tid[u]=++tim; ran[tid[u]]=u; if(son[u]==-1) return; dfs2(son[u],tp); for(int i=head[u]; i!=-1; i=side[i].next) { //cout<<i<<endl; int v=side[i].t; //cout<<v<<endl; if(v!=son[u]&&v!=fa[u]) dfs2(v,v); }}void init(){ tim=cnt=0; memset(head,-1,sizeof(head)); memset(son,-1,sizeof(son));}void addside(int f,int t,int c){ side[cnt]=(si){f,t,c,head[f]}; head[f]=cnt++; side[cnt]=(si){t,f,c,head[t]}; head[t]=cnt++;}/*void change(int a,int b,int c){ while(top[a]!=top[b]){ if(dep[top[a]]<dep[top[b]]) swap(a,b); update(tid[top[a]],tid[a],c,1,n,1); a=fa[top[a]]; } if(dep[a]<dep[b]) swap(a,b); update(tid[b],tid[a],c,1,n,1);}*/void build(int l,int r,int rt){ if(l==r){ sum[rt]=num[l]; return ; } int mid=(l+r)/2; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void update(int L,int R,int c,int l,int r,int rt){ if(l>=L&&r<=R){ sum[rt]=c; return ; } int mid=(l+r)/2; if(mid>=L) update(L,R,c,l,mid,rt<<1); if(mid+1<=R) update(L,R,c,mid+1,r,rt<<1|1); sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void change(int a,int b){ int f=side[a*2-1].f,t=side[a*2-1].t; if(dep[f]<dep[t]) swap(f,t); update(tid[f],tid[f],b,2,n,1);}int query(int L,int R,int l,int r,int rt){ int ans=0; if(l>=L&&r<=R){ return sum[rt]; } int mid=(l+r)/2; if(mid>=L) ans+=query(L,R,l,mid,rt<<1); if(mid+1<=R) ans+=query(L,R,mid+1,r,rt<<1|1); return ans;}void Query(int a,int b){ int ans=0; while(top[a]!=top[b]){ if(dep[top[a]]<dep[top[b]]) swap(a,b); ans+=query(tid[top[a]],tid[a],2,n,1); a=fa[top[a]]; } if(dep[a]>dep[b]) swap(a,b); if(a!=b) ans+=query(tid[a]+1,tid[b],2,n,1); printf("%d\n",ans);}int main(){ while(cin>>n>>q>>s){ init(); int a,b,c; for(int i=0;i<n-1;i++){ scanf("%d %d %d",&a,&b,&c); addside(a,b,c); } dfs1(1,1,1); dfs2(1,1); for(int i=0;i<cnt;i+=2){ int f=side[i].f, t=side[i].t, c=side[i].c; if(dep[f]>dep[t]) num[tid[f]]=c; else num[tid[t]]=c; } build(2,n,1); while(q--){ scanf("%d",&a); // cout<<a<<endl; if(a==0){ scanf("%d",&b); //cout<<a<<' '<<b<<endl; Query(b,s); s=b; } if(a==1){ scanf("%d %d",&b,&c); //cout<<a<<' '<<b<<' '<<c<<endl; change(b,c); } } } return 0;}
0 0
- POJ 3237树链剖分
- POJ 3237 Tree 树链剖分
- 【POJ】3237 Tree 树链剖分
- POJ 3237 Tree 树链剖分
- POJ 3237 树链剖分
- 【树链剖分】 POJ 3237 Tree
- poj 3237 Tree(树链剖分)
- POJ 3237 Tree (树链剖分)
- POJ 3237 Tree 树链剖分
- poj-3237(树链剖分)
- POJ 3237 Tree 树链剖分
- POJ 3237(树链剖分)
- poj 3237 Tree 树链剖分
- poj 3237 Tree 树链剖分
- POJ 3237 - Tree(树链剖分)
- poj 3237 树链剖分
- poj 3237 Tree 树链剖分
- poj 3237(树链剖分)
- CATIA入门第一天
- 在windows上部署redis服务,以及使用jedis客户端。
- C++中的虚函数(类的向上转换,和向下转换)
- 2010年11月份软件设计师:设计模式(观察者模式,组合模式)
- 调试的错误就是编程给你最好的东西,因为在每个错误上面都标志着前进的一步。
- poj 3237 树链剖分
- 文本分类入门(三)统计学习方法
- iOS开发之理解iOS中的MVC设计模式
- MySQL事务处理
- 统计文件的代码
- 【项目管理】Sonatype Nexus,Maven私服
- Linq To Entity 的增删改查(转)
- 连通图里的深度优先和广度优先遍历
- gene ID转换