当前位置:网站首页>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));
}
}
边栏推荐
- bat启动.NET Core
- SCM Project Training
- Anaconda navigator启动慢的一个解决方法
- 基于STM32MP157调试MIPI-DSI屏幕
- Electronics: Lesson 012 - Experiment 13: barbecue LED
- WebSocket的理解以及应用场景
- What are the problems with traditional IO? Why is zero copy introduced?
- Advantages and differences of three kinds of vias in PCB 2021-10-27
- 牛客:飞行路线(分层图+最短路)
- Pychart's wonderful setting: copy immediately after canceling the comment and bring #
猜你喜欢

What are the problems with traditional IO? Why is zero copy introduced?
![[little knowledge] PCB proofing process](/img/bf/f66677294a14baf08cc35d1e8c1e31.jpg)
[little knowledge] PCB proofing process

使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障

一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药

深度学习系列45:图像恢复综述

Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test

socket问题记录

Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus

网络模型——OSI模型与TCP/IP模型

深度学习系列48:DeepFaker
随机推荐
將數據導入到MATLAB
Buckle 78: subset
静态网页服务器
洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)
产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
传统的IO存在什么问题?为什么引入零拷贝的?
The fourth floor is originally the fourth floor. Let's have a look
环网冗余式CAN/光纤转换器的CAN光端机在消防火灾联网报警系统中的应用
CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据
Six causes of PCB disconnection 2021-10-20
Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries
挖掘微生物暗物质——新思路
allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
[daily training] 207 Class Schedule Card
电子学:第014课——实验 15:防入侵报警器(第一部分)
协议和服务的区别?
Pychart's wonderful setting: copy immediately after canceling the comment and bring #
Vscode is good, but I won't use it again
牛客:飞行路线(分层图+最短路)
电子学:第010课——实验 8:继电振荡器