Housewife Wind

参考博客:POJ2763 Housewife Wind(树剖+线段树)

差不多是直接套线段树+树剖的板子,但是也有一些需要注意的地方

建树:

void build()
{
for( int i=1;i<n;++i)
{
if(dep[e[i][1]]<dep[e[i][0]]) swap(e[i][1],e[i][0]);
update(id[e[i][1]],1,n-1,e[i][2],1);
}
}

单点更新(令某个点为 k,而不是加 k):

void update(int x,int s,int t,int k,int p)
{
if(s==t)
{
d[p]=k;return ;
}
int m=(s+t)>>1;
if(x<=m) update(x,s,m,k,lson);
else update(x,m+1,t,k,rson);
d[p]=d[lson]+d[rson];
}

线段树区间求和:

int getsum(int l,int r,int s,int t,int p)
{
if(l>r) return 0; //记得加上这个,不然会re
if(l<=s&&t<=r) return d[p];
int m=(s+t)>>1;
ll sum=0;
if(l<=m) sum+=getsum(l,r,s,m,lson);
if(r>m) sum+=getsum(l,r,m+1,t,rson);
return sum;
}

树剖区间求和:

int query(int x,int y)
{
ll ans=0;
if(x==y) return 0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans+=getsum(id[top[x]],id[x],1,n-1,1);
x=f[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ans+=getsum(id[son[x]],id[y],1,n-1,1); //要记住是son[x]的id到y的id
return ans;
}

代码:

// Created by CAD on 2019/8/13.
#include <algorithm>
#include <cstdio>
#define lson (p<<1)
#define rson ((p<<1)|1)
using namespace std;
#define ll long long
const int maxn=2e5+5;
int dep[maxn],top[maxn],son[maxn],f[maxn],siz[maxn],tot;
int id[maxn],cnt,head[maxn],nxt[maxn],to[maxn];
int d[maxn<<1],n,e[maxn][3];
inline int read() {
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * w;
}
inline void write(int x) {
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
putchar('\n');
}
void add(int u,int v)
{
nxt[++cnt]=head[u];
head[u]=cnt;
to[cnt]=v;
}
void dfs1(int u,int fa)
{
f[u]=fa;dep[u]=dep[fa]+1;siz[u]=1;
int maxson=-1;
for( int i=head[u];i;i=nxt[i])
{
int v=to[i];if(v==fa) continue;
dfs1(v,u);siz[u]+=siz[v];
if(siz[v]>maxson) maxson=siz[v],son[u]=v;
}
}
void dfs2(int u,int Top)
{
top[u]=Top;id[u]=tot++;
if(!son[u]) return ;
dfs2(son[u],Top);
for( int i=head[u];i;i=nxt[i])
{
int v=to[i];
if(v==f[u]||v==son[u]) continue;
dfs2(v,v);
}
}
void update(int x,int s,int t,int k,int p)
{
if(s==t)
{
d[p]=k;return ;
}
int m=(s+t)>>1;
if(x<=m) update(x,s,m,k,lson);
else update(x,m+1,t,k,rson);
d[p]=d[lson]+d[rson];
}
int getsum(int l,int r,int s,int t,int p)
{
if(l>r) return 0;
if(l<=s&&t<=r) return d[p];
int m=(s+t)>>1;
ll sum=0;
if(l<=m) sum+=getsum(l,r,s,m,lson);
if(r>m) sum+=getsum(l,r,m+1,t,rson);
return sum;
}
void build()
{
for( int i=1;i<n;++i)
{
if(dep[e[i][1]]<dep[e[i][0]]) swap(e[i][1],e[i][0]);
update(id[e[i][1]],1,n-1,e[i][2],1);
}
}
int query(int x,int y)
{
ll ans=0;
if(x==y) return 0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans+=getsum(id[top[x]],id[x],1,n-1,1);
x=f[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ans+=getsum(id[son[x]],id[y],1,n-1,1);
return ans;
}
int main()
{
int p,s;
n=read(),p=read(),s=read();
cnt=1,tot=0;
for( int i=1;i<n;++i)
{
e[i][0]=read(),e[i][1]=read(),e[i][2]=read();
add(e[i][0],e[i][1]),add(e[i][1],e[i][0]);
}
dfs1(1,0);dfs2(1,1);
build();
for( int i=1;i<=p;++i)
{
int op,x,y;
op=read();
if(op==0)
x=read(),write(query(x,s)),s=x;
else x=read(),y=read(),update(id[e[x][1]],1,n-1,y,1);
}
}

Housewife Wind的更多相关文章

  1. Housewife Wind(边权树链剖分)

    Housewife Wind http://poj.org/problem?id=2763 Time Limit: 4000MS   Memory Limit: 65536K Total Submis ...

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

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

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

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

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

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

  5. POJ 2763 Housewife Wind(DFS序+LCA+树状数组)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 11419   Accepted: 3140 D ...

  6. AC日记——Housewife Wind poj 2763

    Language: Default Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 10525 ...

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

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

  8. POJ2763 Housewife Wind 树链剖分 边权

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

  9. B - Housewife Wind POJ - 2763 树剖+边权转化成点权

    B - Housewife Wind POJ - 2763 因为树剖+线段树只能解决点权问题,所以这种题目给了边权的一般要转化成点权. 知道这个以后这个题目就很简单了. 怎么转化呢,就把这个边权转化为 ...

随机推荐

  1. Spring基于SchedulingConfigurer实现定时任务

    Spring 基于 SchedulingConfigurer 实现定时任务,代码如下: import org.springframework.scheduling.annotation.Schedul ...

  2. CentOS 7 配置 kcptun 实现网站加速

    目的:shadowsocks+kcptun 实现vpn加速(shadowsocks,kcptun在同一台VPS上) 一.shadowsocks安装(参考  https://www.cnblogs.co ...

  3. mongoDB数据库文件路径和数据操作

    1.查看MongoDB在电脑上的安装路径 which mongod 2.默认mongodb 数据文件是放到根目录 data/db 文件夹下,如果没有这个文件,需要自行创建 mkdir -p /data ...

  4. ELK电子书籍

    Elasticsearch in Action(英文版).pdfElasticsearch实战 in action(中文版).pdfElasticsearch技术解析与实战.pdfElasticsea ...

  5. javaScript基本使用api

    基本方法 isArray() 判断数组 isArray() 方法用于判断是否是数组(有兼容性) 语法:Array.isArray(arr) 返回值:是数组,返回true.不是数组,返回false. i ...

  6. easyui datagrid连续删除问题

    如果在datagrid中直接将index传给easyui自带的deletRow方法来删除当前点击行,一开始并没有问题,但是当连续删除的时候就或出问题了. 原因是datagrid行是根据datagrid ...

  7. python、第六篇:视图、触发器、事务、存储过程、函数

    一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...

  8. 【hdu 6071】Lazy Running

    菜鸡永远都在做着变聚的梦. 题意 有 \(4\) 个点连成一个环,连接顺序依次为 \(1-2-3-4-1\).相邻两个点之间有个距离 \(d_{i,i+1}\)(特别地,当 \(i=4\) 时为 \( ...

  9. tomcat访问日志

    * %a - Remote IP address # 远程ip地址 * %A - Local IP address # 本地ip地址 * %b - Bytes sent, excluding HTTP ...

  10. 使用jvisualvm远程监控tomcat(阿里云ECS)

    写在前面:  使用jvisualvm远程监控tomcat(阿里云ECS),连接是报错:service:jmx:rmi:////jndi/rmi:IP:端口//  连接到 IP:端口,网上找了很多资料, ...