fzu2028
//Accepted 7324 KB 203 ms
/*
source:fzu2028
time :2015.5.29
by :songt
*/
/*题解:
树链剖分
单点更新,求路径和
*/
#include <cstdio>
#include <cstring>
;
int max(int a,int b)
{
return a>b?a:b;
}
void swap(int &a,int &b)
{
int t=a;
a=b;
b=t;
}
struct Edge
{
int u,v;
Edge(){}
Edge(int u,int v):u(u),v(v){}
}
edge[*imax_n];
int head[imax_n];
*imax_n];
int tot;
void addEdge(int u,int v)
{
edge[tot]=Edge(u,v);
next[tot]=head[u];
head[u]=tot++;
}
int fa[imax_n],deep[imax_n],num[imax_n],son[imax_n];
int p[imax_n],fp[imax_n],top[imax_n];
int pos;
void init()
{
memset(head,-,sizeof(head));
memset(next,-,sizeof(next));
tot=;
memset(son,-,sizeof(son));
pos=;
}
void dfs1(int u,int pre,int depth)
{
deep[u]=depth;
fa[u]=pre;
num[u]=;
;i=next[i])
{
int v=edge[i].v;
if (v!=pre)
{
dfs1(v,u,depth+);
num[u]+=num[v];
|| num[son[u]]<num[v])
son[u]=v;
}
}
}
void dfs2(int u,int sp)
{
p[u]=pos++;
fp[p[u]]=u;
top[u]=sp;
) return ;
dfs2(son[u],sp);
;i=next[i])
{
int v=edge[i].v;
if (v!=fa[u] && v!=son[u])
{
dfs2(v,v);
}
}
}
struct Tree
{
int l,r;
long long sum;
int tmax;
}f[imax_n*];
void build(int t,int l,int r)
{
f[t].l=l;
f[t].r=r;
f[t].sum=;
f[t].tmax=;
if (l==r)
{
return ;
}
;
build(*t,l,mid);
build(*t+,mid+,r);
}
void update(int t,int k,int value)
{
if (f[t].l==k && f[t].r==k)
{
f[t].sum=value;
f[t].tmax=value;
return ;
}
;
*t,k,value);
*t+,k,value);
f[t].sum=f[*t].sum+f[*t+].sum;
f[t].tmax=max(f[*t].tmax,f[*t+].tmax);
}
long long query(int t,int l,int r,int &tmax)
{
if (f[t].l==l && f[t].r==r)
{
tmax=f[t].tmax;
return f[t].sum;
}
;
*t,l,r,tmax);
else
{
*t+,l,r,tmax);
else
{
int tmax1,tmax2;
long long sum1,sum2;
sum1=query(*t,l,mid,tmax1);
sum2=query(*t+,mid+,r,tmax2);
tmax=max(tmax1,tmax2);
return sum1+sum2;
}
}
}
long long find(int u,int v,int tmax)
{
int f1=top[u],f2=top[v];
;
;
int t;
while (f1!=f2)
{
if (deep[f1]<deep[f2])
{
swap(f1,f2);
swap(u,v);
}
sum+=query(,p[f1],p[u],t);
tmp=max(tmp,t);
u=fa[f1];
f1=top[u];
}
if (u==v) return sum;
if (deep[u]>deep[v]) swap(u,v);
sum+=query(,p[son[u]],p[v],t);
tmp=max(tmp,t);
tmax=tmp;
return sum;
}
];
int n,m;
int main()
{
)
{
init();
;i<n-;i++)
{
scanf(],&e[i][],&e[i][]);
addEdge(e[i][],e[i][]);
addEdge(e[i][],e[i][]);
}
dfs1(,,);
dfs2(,);
//printf("after dfs\n");
build(,,pos-);
//printf("after build\n");
;i<n-;i++)
{
]]<deep[e[i][]])
swap(e[i][],e[i][]);
update(,p[e[i][]],e[i][]);
}
//printf("after insert\n");
int kind;
int u,v,c;
int tmax;
;i<m;i++)
{
scanf("%d%d%d",&kind,&u,&v);
)
{
update(,p[e[u-][]],v);
}
else
{
printf("%lld\n",find(u,v,tmax));
}
}
}
;
}
fzu2028的更多相关文章
随机推荐
- GNS3 IOU 配置
GNS3使用视频: http://edu.51cto.com/lesson/id-25295.html GNS3 IOU 与VM http://www.mamicode.com/info-detail ...
- Oracle 表连接
Oracle 表之间的连接分为三种: 1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不 ...
- 11.10 Taolu1234组信息汇总
团队名称: Taolu1234 团队选题: <餐厅到店点餐系统>WEB版 团队博客地址: http://www.cnblogs.com/queenjuan/ 团队GITHUB地址: htt ...
- 点开看看please
- springMVC 错误页面配置
在Spring MVC应用程序中,404 error code 被合适的配置.web.xml文件中配置如下所示: <!-- spring mvc start --> <servlet ...
- Accessibility应用之focus篇
最近项目中需要应用accessibility,大量使用了focus和blur,总结如下 一.设置焦点focus 如创建无障碍对话框:当一个对话框出现时,焦点应在对话框内,这样用户才可以使用键盘继续浏览 ...
- Yann LeCun, Geoffrey E. Hinton, and Yoshua Bengio
- 如何在CentOS 7中禁止IPv6
最近,我的一位朋友问我该如何禁止IPv6.在搜索了一番之后,我找到了下面的方案.下面就是在我的CentOS 7 迷你服务器禁止IPv6的方法. 你可以用两个方法做到这个. 方法 1 编辑文件/etc/ ...
- 小游戏Step表
[Config]1|0|0|2|172,120,134|公主的记忆|1|2[Config] [Config]2|1|1|2|172,120,134|公主的记忆|1|2[Config] [Config] ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...