POJ 2763 Housewife Wind 树链拋分
一、前言
这破题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 树链拋分的更多相关文章
- poj 2763 Housewife Wind(树链拆分)
id=2763" target="_blank" style="">题目链接:poj 2763 Housewife Wind 题目大意:给定一棵 ...
- POJ 2763 Housewife Wind (树链剖分 有修改单边权)
题目链接:http://poj.org/problem?id=2763 n个节点的树上知道了每条边权,然后有两种操作:0操作是输出 当前节点到 x节点的最短距离,并移动到 x 节点位置:1操作是第i条 ...
- POJ - 2763 Housewife Wind (树链剖分/ LCA+RMQ+树状数组)
题意:有一棵树,每条边给定初始权值.一个人从s点出发.支持两种操作:修改一条边的权值:求从当前位置到点u的最短路径. 分析:就是在边可以修改的情况下求树上最短路.如果不带修改的话,用RMQ预处理LCA ...
- poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)²)修改与查询
/** problem: http://poj.org/problem?id=2763 **/ #include<stdio.h> #include<stdlib.h> #in ...
- POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )
POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...
- POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新
题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- HDU 3966 Aragorn's Story 树链拋分
一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
- POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)
Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10378 Accepted: 2886 D ...
随机推荐
- electron 开发记录
判断是否开发环境 安装 electron-is-dev npm install electron-is-dev // main.js const isDev = require('electron-i ...
- ashx是什么文件
ashx是什么文件 .ashx 文件用于写web handler的..ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程 ...
- ace admin 左侧菜单定位
后台模版来自:Ace Admin http://ace.jeka.by/form-elements.html 左侧菜单,通过js根据url来判断显示哪块 window.location.pathnam ...
- 阿里云部署安装redis无法访问
ps:我在linux上安装redis后发现一直端口不通连接不上,折腾一晚上.后来在阿里云官方回复(机器人)中看到下面的回复: 2019/02/28 22:50 自己一试,过完是端口监听是本 ...
- python中函数的定义与调用
1.为什么要用函数? (1)代码重复太多(2)可读性差 使用函数的好处: (1)代码重用 (2)保持一致性,易维护 (2)可扩展性 2.初始函数定义与调用 函数的定义 def test(x): ...
- form表单上传域(type="file")的使用----上传文件
一,单个文件的上传 1.html/jsp页面 <%@ page language="java" contentType="text/html; charset=UT ...
- FMCW 雷达原理(转)
FMCW(Frequency Modulated Continuous Wave),即调频连续波.FMCW技术和脉冲雷达技术是两种在高精度雷达测距中使用的技术.其基本原理为,发射波为高频连续波,其频率 ...
- Ubuntu 安装boost 库
使用 apt-get进行安装 sudo apt-get install libboost-dev
- 在window下, Java调用执行bat脚本
参考博客: https://www.cnblogs.com/jing1617/p/6430141.html 最近一段时间用到了Java去执行window下的bat脚本, 这里简单记录一下: 我这里是先 ...
- Android商城开发系列(十)—— 首页活动广告布局实现
在上一篇博客当中,我们讲了频道布局的实现,接下来我们讲解一下活动广告布局的实现,效果如下图: 这个是用viewpager去实现的,新建一个act_item.xml,代码如下所示: <?xml v ...