一、前言

  这破题WA了一天,最后重构还是WA,最后通过POJ讨论版得到的数据显示,我看上去是把某个变量写错了。。于是,还是低级错误背锅啊。。。。代码能力有待进一步提升2333333

二、题意

  某家庭主妇住在一棵树上,他的若干个孩子在树的若干个节点上有分布,主妇同学需要从某给定节点出发,飞到树上的制定节点,在过程中,边权可能会发生改变,问从当前节点到指定节点的边权和。

三、解法

  树链拋分,点更新查区间。

// #include<bits/stdc++.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std; #define ll long long
#define pp pair<int ,int> const ll MAXN=1e5+; class Node
{
public:
int next,to,cost;
void init(int a,int b,int c)
{
this->next=a;
this->to=b;
this->cost=c;
}
};
Node G[MAXN*];
int tree[MAXN],child[MAXN],deep[MAXN],number[MAXN],top[MAXN],points[MAXN],father[MAXN];
int size,summ,n,q,s;
void insert(int pos,int key)
{
while(pos<MAXN)
{
tree[pos]+=key;
pos+=pos&(-pos);
}
}
int getSum(int pos)
{
int ans=;
while(pos)
{
ans+=tree[pos];
pos-=pos&(-pos);
}return ans;
}
void add(int from,int to,int cost)
{
G[summ].init(points[from],to,cost);
points[from]=summ++;
}
void dfs_1(int now,int last,int dep)
{
deep[now]=dep;
father[now]=last;
child[now]=;
for(int i=points[now];i!=-;i=G[i].next)
{
int tar=G[i].to;
int cost=G[i].cost;
if(tar==last)continue;
dfs_1(tar,now,dep+);
child[now]+=child[tar];
}
}
void dfs(int now,int last,int first,int cc=)
{
top[now] = first ? first : now;
insert(size,cc);
// cout<<"check_number: "<<now<<ends<<size<<endl;
number[now]=size++;
int maxx,pos,coS;
maxx=pos=-;
for(int i=points[now];i!=-;i=G[i].next)
{
int tar=G[i].to;
int cost=G[i].cost;
if(tar==last)continue;
if(maxx<child[tar])
{
maxx=child[tar];
pos=tar;
coS=cost;
}
}if(pos!=-)dfs(pos,now,top[now],coS);
for(int i=points[now];i!=-;i=G[i].next)
{
int tar=G[i].to;
int cost=G[i].cost;
if(tar==last||tar==pos)continue; dfs(tar,now,,cost);
}
}
void update(int num,int key)
{
int a=G[num*].to;
int b=G[num*+].to;
if(father[a]==b)
{
key=key-(getSum(number[a])-getSum(number[a]-));
insert(number[a],key);
}else
{
key=key-(getSum(number[b])-getSum(number[b]-));
insert(number[b],key);
}
}
int query(int from,int to)
{
int ans=;
int t1=top[from];
int t2=top[to];
// cout<<"check_top: "<<t1<<ends<<t2<<endl;
// cout<<"check_point: "<<from<<ends<<to<<endl;
while(t1!=t2)
{
if(deep[t1]<deep[t2])
{
swap(t1,t2);
swap(from,to);
}
ans+=getSum(number[from])-getSum(number[t1]-);
from=father[t1];
t1=top[from];
}
int star=min(number[from],number[to]);
int endd=max(number[from],number[to]);
// cout<<ans<<"check_Number: "<<star<<ends<<endd<<endl;
// cout<<"checkSum: "<<getSum(endd)<<ends<<getSum(star)<<endl;
ans+=getSum(endd)-getSum(star);
return ans;
}
void init()
{
size=;summ=;
memset(points,-,sizeof(points));
for(int i=;i<n;++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
// cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}dfs_1(,,);
dfs(,,);
for(int i=;i<q;++i)
{
int a,b,c;
// cin>>c;
scanf("%d",&c);
if(c)
{
// cin>>a>>b;
scanf("%d%d",&a,&b);
a--;
update(a,b);
}else{
// cin>>a;
scanf("%d",&a);
cout<<query(s,a)<<"\n";
s=a;
}
}
} int main()
{
// cin.sync_with_stdio(false);
cin>>n>>q>>s;init(); return ;
}

POJ 2763 Housewife Wind 树链拋分的更多相关文章

  1. poj 2763 Housewife Wind(树链拆分)

    id=2763" target="_blank" style="">题目链接:poj 2763 Housewife Wind 题目大意:给定一棵 ...

  2. POJ 2763 Housewife Wind (树链剖分 有修改单边权)

    题目链接:http://poj.org/problem?id=2763 n个节点的树上知道了每条边权,然后有两种操作:0操作是输出 当前节点到 x节点的最短距离,并移动到 x 节点位置:1操作是第i条 ...

  3. POJ - 2763 Housewife Wind (树链剖分/ LCA+RMQ+树状数组)

    题意:有一棵树,每条边给定初始权值.一个人从s点出发.支持两种操作:修改一条边的权值:求从当前位置到点u的最短路径. 分析:就是在边可以修改的情况下求树上最短路.如果不带修改的话,用RMQ预处理LCA ...

  4. poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)²)修改与查询

    /** problem: http://poj.org/problem?id=2763 **/ #include<stdio.h> #include<stdlib.h> #in ...

  5. POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )

    POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...

  6. POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

    题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...

  7. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

  8. HDU 3966 Aragorn's Story 树链拋分

    一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...

  9. POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 10378   Accepted: 2886 D ...

随机推荐

  1. idea关闭sonar自动扫描

    file-setting-other setting-sonar相关的setting全部关闭

  2. elasticsearch.yml基本配置说明

    一.基本配置 elasticsearch的config文件夹里面有两个配置文 件:elasticsearch.yml和logging.yml,第一个是es的基本配置文件,第二个是日志配置文件,es也是 ...

  3. Handler: Service中使用Toast

    Handler 的使用在 android App 开发中用的颇多,它的作用也很大,使用 Handler 一般也会使用到多线程,相信大家对 Handler 不会陌生,在这里,重点说一下 android ...

  4. windows下使用VM虚拟机安装linux

    转载地址:http://blog.csdn.net/u013142781/article/details/50529030 安装过程中发现与下面的顺序有点不同,遇到的问题是: 在选择中文进行安装时,一 ...

  5. 设置mapcontrol的鼠标样式

    http://blog.itpub.net/14999074/viewspace-586515/ mapcontrol的鼠标样式 this.axMapControl1.MousePointer=esr ...

  6. Java JDBC链接Oracle数据库

    package com.test.test; import java.io.FileInputStream;import java.io.FileNotFoundException;import ja ...

  7. linux 命令——52 ifconfig(转)

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  8. myeclipse引入工程后运行出错

    An internal error occurred during: Launching efax on Tomcat 7.x . 项目运行时报错 因为你项目建的时候用的是Tomcat5.x 服务器 ...

  9. 【BZOJ1833】[ZJOI2010] count 数字计数(数位DP)

    点此看题面 大致题意: 求在给定的两个正整数\(a\)和\(b\)中的所有整数中,\(0\sim9\)各出现了多少次. 数位\(DP\) 很显然,这是一道数位\(DP\)题. 我们可以用前缀和的思想, ...

  10. BZOJ 4128: Matrix

    BZOJ 4128: Matrix 标签(空格分隔): OI BZOJ 大步小步 矩阵 费马小定理 Time Limit: 10 Sec Memory Limit: 128 MB Descriptio ...