Housewife Wind
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的更多相关文章
- Housewife Wind(边权树链剖分)
Housewife Wind http://poj.org/problem?id=2763 Time Limit: 4000MS Memory Limit: 65536K Total Submis ...
- 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距离为 ...
- POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)
Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10378 Accepted: 2886 D ...
- POJ 2763 Housewife Wind(DFS序+LCA+树状数组)
Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11419 Accepted: 3140 D ...
- AC日记——Housewife Wind poj 2763
Language: Default Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10525 ...
- poj 2763 Housewife Wind(树链拆分)
id=2763" target="_blank" style="">题目链接:poj 2763 Housewife Wind 题目大意:给定一棵 ...
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- B - Housewife Wind POJ - 2763 树剖+边权转化成点权
B - Housewife Wind POJ - 2763 因为树剖+线段树只能解决点权问题,所以这种题目给了边权的一般要转化成点权. 知道这个以后这个题目就很简单了. 怎么转化呢,就把这个边权转化为 ...
随机推荐
- 洛谷P1192台阶问题
题目描述 有NN级的台阶,你一开始在底部,每次可以向上迈最多KK级台阶(最少11级),问到达第NN级台阶有多少种不同方式. 输入格式 两个正整数N,K. 输出格式 一个正整数,为不同方式数,由于答案可 ...
- linux 安装 python 最全教程
环境:centos6.5 centos6.5 自带的 python 版本是 2.6.6,需要重新安装 2.7: centos7 自带的 python 版本是 2.7.5 基本操作 在安装新版本之前,一 ...
- 安装gitlab ce
切换到root用户,安装相关依赖 yum install curl policycoreutils openssh-server openssh-clients service sshd restar ...
- DockerFile 编译语法详解
Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile来快速创建自定义的镜像,本小结首先介绍Dockerfile典型的基本结构和它支持的众多指令,并具体讲解通过这些指令来编写定 ...
- 使用openresty实现按照流量百分比控制的灰度分流控制
安装好以后直接就可以配置实践了,openresty将lua都集成好了,nginx配置文件无需特殊声明引入lua file. 1.nginx.conf 添加两个灰度发布的环境 #grey 灰度环境地址 ...
- Redis原子计数器incr,防止并发请求
转自:https://blog.csdn.net/Roy_70/article/details/78260826 一.前言在一些对高并发请求有限制的系统或者功能里,比如说秒杀活动,或者一些网站返回的当 ...
- [转载]布隆过滤器(Bloom Filter)
[转载]布隆过滤器(Bloom Filter) 这部分学习资料来源:https://www.youtube.com/watch?v=v7AzUcZ4XA4 Filter判断不在,那就是肯定不在:Fil ...
- Python 第一个程序
第一个程序 打开pycharm,新建一个工程,新建一个文件(后缀为.py) 书写最简单的代码:print(人生苦短,我用python!) 执行python代码 使用pycharm的运行按钮 终端下输入 ...
- centos7 部署haproxy
第一章 :haproxy介绍 1.1 简介 HAProxy 是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. ...
- Delphi 监视数据的值