当前位置:网站首页>洛谷P3313 [SDOI2014]旅行(树链+边权转点权)
洛谷P3313 [SDOI2014]旅行(树链+边权转点权)
2022-06-25 06:43:00 【mfy的1号小迷弟】
洛谷P3313 [SDOI2014]旅行(树链+边权转点权)
#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];//边权转点权
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];//边权转点权
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){
//边x到y的权值和
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到y区间修改
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));
}
}
边栏推荐
猜你喜欢
Basic use of ActiveMQ in Message Oriented Middleware
One "stone" and two "birds", PCA can effectively improve the dilemma of missing some ground points under the airborne lidar forest
年后求职找B端产品经理?差点把自己坑惨了......
微信小程序开通客服消息功能开发
Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
Invalid Navicat scheduled task
SCM Project Training
How much do you know about electronic components on PCB?
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
Sword finger offer II 027 Palindrome linked list
随机推荐
WinForm implementation window is always at the top level
50. pow (x, n) - fast power
Manufacturing process of PCB 2021-10-11
Import data into Matlab
单位转换-毫米转像素-像素转毫米
@Resource和@Autowired注解的不同,为什么推荐@Resource?
c# winform panel自定义图片和文字
php入门基础记录
Runtime - Methods member variable, cache member variable
Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus
VOCALOID笔记
使用Adobe Acrobat Pro调整PDF页面为统一大小
C#控件刷新
判断用户是否是第一次进入某个页面
How to use ad wiring for PCB design?
剑指 Offer II 027. 回文链表
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
TCP的那点玩意儿