当前位置:网站首页>Luogu p3313 [sdoi2014] travel (tree chain + edge weight transfer point weight)
Luogu p3313 [sdoi2014] travel (tree chain + edge weight transfer point weight)
2022-06-25 08:02:00 【Mfy's little brother 1】
Luogu P3313 [SDOI2014] travel ( Tree chain + Edge power to point power )
#include <bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=2e5+100;
int n,m,k,tot,a[maxn],siz[maxn],fa[maxn],son[maxn],id[maxn],idd[maxn],top[maxn],deep[maxn];
int lazy[maxn<<2],sum[maxn<<2],mx[maxn<<2],mn[maxn<<2];
int head[maxn],to[maxn<<1],nex[maxn<<1],w[maxn<<1],vw[maxn*2],tmp[maxn*2];
struct E{
int to,next,val;}edge[maxn*2];
struct node{
int x,y,val;
}e[maxn*2];
void add(int u,int v,int z)
{
to[++k]=v;
nex[k]=head[u];
vw[k]=z;
head[u]=k;
}
void dfs1(int x,int f,int d){
siz[x]=1;
fa[x]=f;
deep[x]=d;
son[x]=0;
for(int i=head[x];i;i=nex[i]){
int y=to[i];
if(y==f)continue;
dfs1(y,x,d+1);
tmp[y]=vw[i];// Edge power to point power
siz[x]+=siz[y];
if(siz[son[x]]<siz[y])son[x]=y;
}
}
void dfs2(int x,int root){
id[x]=++tot;
top[x]=root;
w[tot]=tmp[x];// Edge power to point power
if(son[x])dfs2(son[x],root);
for(int i=head[x];i;i=nex[i]){
int y=to[i];
if(y!=fa[x]&&y!=son[x])dfs2(y,y);
}
}
void build(int rt,int l,int r){
if(l==r){
sum[rt]=mn[rt]=mx[rt]=w[l];
return;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
int getdis(int x,int y){
// edge x To y Weight sum of
int ans=0;
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
ans+=query1(1,1,n,id[top[x]],id[x]);
x=fa[top[x]];
}
if(deep[x]>deep[y])swap(x,y);
if(x!=y)ans+=query1(1,1,n,id[x]+1,id[y]);
return ans;
}
void upday(int x,int y){
//x To y Interval modification
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
update(1,1,n,id[top[x]],id[x]);
x=fa[top[x]];
}
if(deep[x]>deep[y])swap(x,y);
if(x!=y)update(1,1,n,id[x]+1,id[y]);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].val);
add(e[i].x,e[i].y,e[i].val),add(e[i].y,e[i].x,e[i].val);
}
dfs1(1,0,1);
dfs2(1,1);
build(1,1,n);
while(m--){
int x,y;
char op[6];
scanf("%s%d%d",op,&x,&y);
x++,y++;
if(op[0]=='C'){
x--,y--;
int t;
if(deep[e[x].x]>deep[e[x].y])t=e[x].x;
else t=e[x].y;
updian(1,1,n,id[t],y);
}
else if(op[0]=='N')upday(x,y);
else if(op[0]=='S')printf("%d\n",getdis(x,y));
}
}
边栏推荐
- How to use ad wiring for PCB design?
- bat启动.NET Core
- 取消word文档中某些页面的页眉
- 唐老师讲运算放大器(第七讲)——运放的应用
- Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line
- Atlassian confluence漏洞分析合集
- Pycharm的奇葩设定:取消注释后立马复制会带上#
- 电子学:第012课——实验 11:光和声
- CAN总线工作状况和信号质量“体检”
- 洛谷P2048 [NOI2010] 超级钢琴(RMQ+优先队列)
猜你喜欢
Ubuntu18下登录mysql 5.7设置root密码
Solving some interesting problems with recurrence of function
FM信号、调制信号和载波
共话云原生数据库的未来
Tips on how to design soft and hard composite boards ~ 22021/11/22
What are the problems with traditional IO? Why is zero copy introduced?
深度学习系列45:图像恢复综述
产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
PCB board design - automatic layout 2021-10-15
Share the process requirements for single-layer flexible circuit board
随机推荐
C control refresh
What are the benefits of reserving process edges for PCB production? 2021-10-25
50. pow (x, n) - fast power
【日常训练】207. 课程表
Apache CouchDB 代码执行漏洞(CVE-2022-24706 )批量POC
RMQ区间最大值下标查询,区间最值
How to select lead-free and lead-free tin spraying for PCB? 2021-11-16
SCM Project Training
ffmpeg+SDL2实现音频播放
基于Anaconda的模块安装与注意事项
Buckle 78: subset
@The difference between resource and @autowired annotation, why recommend @resource?
Analysis of kinsing dual platform mining family virus
Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
饮食干预减轻癌症治疗相关症状和毒性
CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据
C#控件刷新
Pychart's wonderful setting: copy immediately after canceling the comment and bring #
57. insert interval
c#搭建ftp服务器并实现文件上传和下载