当前位置:网站首页>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));
}
}
边栏推荐
猜你喜欢

Requirements for Power PCB circuit board design 2021-11-09

Machine learning notes linear regression of time series

Vscode is good, but I won't use it again

Debugging mipi-dsi screen based on stm32mp157
![[deep learning lightweight backbone] 2022 edgevits CVPR](/img/13/139d28621403020e3475a30f6148f8.png)
[deep learning lightweight backbone] 2022 edgevits CVPR

挖掘微生物暗物质——新思路

Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line

共话云原生数据库的未来

Mining microbial dark matter -- a new idea

Importer des données dans MATLAB
随机推荐
To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity
Pycharm的奇葩设定:取消注释后立马复制会带上#
深度学习系列48:DeepFaker
洛谷P3313 [SDOI2014]旅行(树链+边权转点权)
Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line
洛谷P2839 [国家集训队]middle(二分 + 主席树 + 区间合并)
1742. maximum number of small balls in the box
Do you know why the PCB produces tin beads? 2021-09-30
navicat定时任务无效
Matlab代码格式一键美化神器
Analysis of kinsing dual platform mining family virus
Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
洛谷P6822 [PA2012]Tax(最短路+边变点)
电子学:第008课——实验 6:非常简单的开关
MySQL simple permission management
Buckle 78: subset
c#ColorDialog更改文本颜色和FontDialog更改文本字体的使用示例
Electronics: Lesson 014 - Experiment 15: intrusion alarm (Part I)
將數據導入到MATLAB
Authority design of SaaS system based on RBAC