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 因为树剖+线段树只能解决点权问题,所以这种题目给了边权的一般要转化成点权. 知道这个以后这个题目就很简单了. 怎么转化呢,就把这个边权转化为 ...
随机推荐
- [Bzoj1001][BeiJing2006]狼抓兔子(网络流/对偶图)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 看到大佬们都是对偶图过的,写了个最大流水过去了QAQ,网络流的无向图直接建双向边( ...
- 坦克大战--Java类型 ---- (3)实现socket通信
一.实现思路 使用socket通信的一些方法来实现socket通信,客户端和服务端两边需要约定好通信的接口Port(尽量选高的),客户端需要服务端的IP地址,以实现数据交流. 同时,客户端和服务端需要 ...
- HDU 4253-Two Famous Companies(二分+最小生成树)
Description In China, there are two companies offering the Internet service for the people from all ...
- sublime集成MinGW,打造C/C++开发环境
MinGW是是将GCC编译器和GNU Binutils移植到Win32平台下的产物,包括一系列头文件(Win32API).库和可执行文件.MinGW是从Cygwin(1.3.3版)基础上发展而来.GC ...
- CentOS 7 配置 kcptun 实现网站加速
目的:shadowsocks+kcptun 实现vpn加速(shadowsocks,kcptun在同一台VPS上) 一.shadowsocks安装(参考 https://www.cnblogs.co ...
- Head First PHP&MySQl第一章代码
HTML: <!doctype html> <html lang="zh-cn"> <head> <meta charset=" ...
- Java小程序—录屏小程序(下半场)
下半场. 上半场,我们我们写了录屏的程序,那么下半场我们的任务是写一个播放器. 设计思路:播放器的思路就是将图片放在一个JScrollPane中顺序播放,所以还是得使用swing组件,并且仍然要使用线 ...
- asp.net core在发布时排除配置文件
使用命令发布 dotnet restore dotnet publish -c Release -r win-x64 -o "D:\services" 这样发布总是是将配置文件覆盖 ...
- Centos固定IP
centos7 联网 在虚拟机中以最小化方式安装centos7,后无法上网,因为centos7默认网卡未激活. 而且在sbin目录中没有ifconfig文件,这是因为centos7已经不使用 ifco ...
- O014、云计算与OpenStack
参考https://www.cnblogs.com/CloudMan6/p/5334760.html 云计算 基本概念 所有的新事物都不是突然冒出来的,都有前世和今生.云计算也是IT技术不断发 ...