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 因为树剖+线段树只能解决点权问题,所以这种题目给了边权的一般要转化成点权. 知道这个以后这个题目就很简单了. 怎么转化呢,就把这个边权转化为 ...
随机推荐
- Java 错误:Constructor call must be the first statement in a constructor
今天用学校里的黑马程序员通Java语法 想到了:在有参构造函数中调用无参构造函数 语法是这样的: class Person{ private int age; public Person() { Sy ...
- 【NOIP2017】跳房子
这题我0分. 比赛时,我一眼出正解,哈哈,太水了! 这题不就是一个二分+DP+单调队列吗? 然而,细节决定成败. 我错了许多细节,就挂了. 我只考了0分... 首先,这题满足一个条件: 保证g变大后, ...
- IDEA2018.2.6激活(可用)
破解插件下载: 链接:https://pan.baidu.com/s/1j2_kEm_Akcph6Qb8hr6soQ 提取码:hv64 将下载包放入bin文件夹下,修改bin中的两个文件 idea.e ...
- 禁用Win10自带截图工具快捷键(Shift+Win+S)
由于在微信之前,多年使用QQ的缘故,已经习惯了使用Ctrl+Alt+A进行截图,虽然QQ后来还专门提供了TIM(Office-QQ),但仍然渐渐的以微信为主,TIM甚至已经很少登录,之前登录也仅仅是为 ...
- linux下安装php的lua扩展
1. 进入管理员权限使用yum安装 readline(也可以使用wget下载后./configure 然后 make && make install进行安装) yum install ...
- server001
- maven的配置以及使用
1.下载并配置 下载之后解压,并配置系统环境变量(网上的方法很多),配置maven的环境变量之前确保java的环境变量已经配置成功. 2.eclipse安装maven插件 eclipse安装maven ...
- nginx+php设置大文件请求上传(502及504问题处理)
502问题 php-fpm 修改项: request_terminate_timeout 位置: eg: /etc/php5/fpm2/pool.d/www.conf ; The timeout fo ...
- 使用GDB和GEF进行调试
使用GDB进行调试 这是编译ARM二进制文件和使用GDB进行基本调试的简单介绍.在您按照教程进行操作时,您可能需要按照自己的习惯使用ARM程序集.在这种情况下,你要么需要一个备用的ARM设备,或者你只 ...
- ELK监控交换机日志
一.首先部署logstash监控UDP514端口,新建一个配置文件cisco.conf 交换机是通过配置rsyslog服务器来将日志发送到日志服务器的,所以需要在logstash上配置rsyslog监 ...