HDU-5893 树链剖分+区间合并

Applied Energy期刊小白论文投稿模板-流程经验分享 Applied Energy期刊小白论文投稿模板-流程经验分享,Elsevier期刊(爱思唯尔)投稿流程 阅读详情

List wants to travel

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 446    Accepted Submission(s): 101


Problem Description
A boy named List who is perfect in English. Now he wants to travel and he is making a plan. But the cost of living in same citie always changes. Now he wants to know how many different kinds of continuous same cost he has to pay for living between two cities. Can you help him? (He is so lazy to do this by himself.)
 

Input
There are multiple cases. The first line contains two positive numbers N and M(N (N<=40000) where N is the amount of cities and M (M<=50000)) is the amount of operations.Then N-1 lines where each line have 3 integers a b and c, representing that there is a bidirectionoal road between city a and city b, and the cost is c.(a != b and c <= 100000). Then there are M lines of operation. For example, "Change a b c" means changing all the costs of the road which are passed by him when he travels from city a to city b to c. "Query a b" means he wants you to tell him how many different kinds of continuous same cost he has to pay traveling from city a to city b.(if a == b, the cost is 0).
 

Output
He insure you that there is exactly one route between every two different cities.
 

Sample Input
  
9 3 1 2 2 2 3 1 1 7 2 1 4 2 3 5 2 3 6 1 5 8 2 5 9 3 Query 1 8 Change 2 6 3 Query 1 6
 

Sample Output
  
3 2
 

Source


题目链接:

题目大意:
给出一棵树上的两种操作:
1.将节点a到节点b路径上的边权全部修改为c;
2.查询节点a到节点b路径上边权的段数。

解题思路:
先进行树链剖分,将问题转化到区间上,之后统计段数是经典的线段树区间合并问题。修改操作直接修改就好了,对于查询操作,需要注意链的合并,可以先求一次a与b的LCA,之后将路径分为两段分别查询,最后看拼接处权值是否相同。

AC代码:
import java.io.*;
import java.util.*;

public class Main {
	static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static int nextInt() throws IOException    
	{  
	    in.nextToken();    
	    return (int)in.nval;     
	}
	static String next() throws IOException    
	{  
	    in.nextToken();  
	    return in.sval;
	}
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	static int n,m,ee,a,b,c,ct,op,co;
	static int[] aa=new int[40005];
	static int[] fa=new int[40005];
	static int[] id=new int[40005];
	static int[] top=new int[40005];
	static int[] son=new int[40005];
	static int[] dep=new int[40005];
	static int[] size=new int[40005];
	static int[] head=new int[40005];
	static int[] edge=new int[80005];
	static int[] next=new int[80005];
	static int[] cost=new int[80005];
	static int[] ll=new int[320005];
	static int[] rr=new int[320005];
	static int[] mm=new int[320005];
	static int[] sum=new int[320005];
	static void add(int a,int b,int c)
	{
		edge[ee]=b;next[ee]=head[a];
		cost[ee]=c;head[a]=ee++;
	}
	static void dfs(int u,int f,int d)  
    {  
        size[u]=1;dep[u]=d;  
        son[u]=-1;fa[u]=f;  
        int v;  
        for(int i=head[u];i!=-1;i=next[i])  
        {  
            v=edge[i];  
            if(v==f) continue;
            aa[v]=cost[i];
            dfs(v,u,d+1);  
            size[u]+=size[v]; 
            if(son[u]==-1||size[son[u]]<size[v])  
                son[u]=v;  
        }  
    }  
    static void dfs2(int u,int f,int t)  
    {  
        id[u]=++ct;top[u]=t;
        if(son[u]!=-1) dfs2(son[u],u,t);  
        int v;
        for(int i=head[u];i!=-1;i=next[i])  
        {  
            v=edge[i];  
            if(v==f||v==son[u]) continue;
            dfs2(v,u,v);  
        }  
    }
    static void prepare()  
    {  
        dfs(1,0,1);  
        ct=0;dfs2(1,0,1);
        Arrays.fill(mm,-1);
        Arrays.fill(ll,0);
        Arrays.fill(rr,0);
        Arrays.fill(sum,0);
        for(int i=1;i<=n;i++)
        	updata(id[i],id[i],1,1,n,aa[i]);
    }
    static int Query(int u,int v)  
    {
    	int p=LCA(u,v),ans=0;
    	if(dep[u]>dep[v]) { c=u;u=v;v=c;}
    	if(u==p) ans=query(v,u);
    	else
    	{
    		int co1,co2;
    		ans+=query(v,p);co1=co;
    		ans+=query(u,p);co2=co;
    		if(co1==co2) ans--;
    	}
        return ans;
    }
    static int LCA(int u,int v)
    {
    	while(top[u]!=top[v])
        {
    		a=top[u];b=top[v];
    		if(dep[a]<dep[b])
    		{
    			c=u;u=v;v=c;
    			c=a;a=b;b=c;
    		}
    		u=fa[a];
    	}
    	return dep[u]>dep[v]?v:u;
    }
    static int query(int u,int v)
    {
    	int ans=0;  
        while(top[u]!=top[v])  
        {  
            a=top[u];
            ans+=sum(id[a],id[u],1,1,n);
            co=get(id[a],1,1,n);
            if(v!=fa[a]&&co==get(id[fa[a]],1,1,n))
            	ans--;
            u=fa[a];  
        }
        if(u!=v)
        {
        	co=get(id[son[v]],1,1,n);
        	ans+=sum(id[son[v]],id[u],1,1,n);
        }
        return ans;
    }
    static void Change(int u,int v,int x)  
    {  
        while(top[u]!=top[v])  
        {  
            a=top[u];b=top[v];  
            if(dep[a]<dep[b])  
            {
                c=u;u=v;v=c;  
                c=a;a=b;b=c;  
            }
            updata(id[a],id[u],1,1,n,x);
            u=fa[a];
        }  
        if(u!=v)
        {
        	if(dep[u]>dep[v]) { c=u;u=v;v=c;}
            updata(id[son[u]],id[v],1,1,n,x);
        }
    }
	static void updata(int a,int b,int k,int l,int r,int x)
	{
		if(a>r||b<l) return;
		if(a<=l&&r<=b)
		{
			sum[k]=1;
			mm[k]=x;
			ll[k]=rr[k]=x;
		}
		else
		{
			int mid=(l+r)/2;
			pushdown(k);
			updata(a,b,k*2,l,mid,x);
			updata(a,b,k*2+1,mid+1,r,x);
			pushup(k);
		}
	}
	static void pushdown(int k)
	{
		if(mm[k]==-1) return;
		mm[k*2]=mm[k*2+1]=mm[k];
		ll[k*2]=ll[k*2+1]=mm[k];
		rr[k*2]=rr[k*2+1]=mm[k];
		sum[k*2]=sum[k*2+1]=1;
		mm[k]=-1;
	}
	static void pushup(int k)
	{
		sum[k]=sum[k*2]+sum[k*2+1];
		if(rr[k*2]==ll[k*2+1]) sum[k]--;
		ll[k]=ll[k*2];
		rr[k]=rr[k*2+1];
	}
	static int get(int a,int k,int l,int r)
	{
		if(l==r) return mm[k];
		int mid=(l+r)/2;
		pushdown(k);
		if(a<=mid) return get(a,k*2,l,mid);
		return get(a,k*2+1,mid+1,r);
	}
	static int sum(int a,int b,int k,int l,int r)
	{
		if(a>r||b<l) return 0;
		if(a<=l&&r<=b) return sum[k];
		int mid=(l+r)/2,res=0;
		pushdown(k);
		res+=sum(a,b,k*2,l,mid);
		res+=sum(a,b,k*2+1,mid+1,r);
		if(mid>=a&&mid<b&&rr[k*2]==ll[k*2+1])
			res--;
		return res;
	}

	public static void main(String[] args) throws IOException {
		while(in.nextToken()!=StreamTokenizer.TT_EOF)
		{
			n=(int)in.nval;m=nextInt();
			Arrays.fill(head,-1);ee=0;
			for(int i=1;i<n;i++)
			{
				a=nextInt();b=nextInt();
				c=nextInt();
				add(a,b,c);add(b,a,c);
			}
			prepare();
			while(m-->0)
			{
				op=next().charAt(0);
				a=nextInt();b=nextInt();
				if(op=='Q')
					out.println(Query(a,b));
				else
				{
					c=nextInt();
					Change(a,b,c);
				}
			}
			out.flush();
		}
		out.flush();
	}
}


国科大《人工智能原理与算法》2021年期末试题+2024经验(2025指南) 2021年期末试题+24年复习指南 阅读详情

相关推荐

大疆OcuSync系统背后的黑科技:跳频扩频技术如何让你的无人机飞得更稳

本文深入解析了大疆OcuSync系统如何运用跳频扩频技术等先进无线通信方案,显著提升无人机在复杂环境下的飞行稳定性。系统通过跳频扩频与直接序列扩频技术协同工作,结合双频智能切换与多天线MIMO,有效对抗信号干扰与多径效应,确保控制指令与高清图传的可靠、低延迟传输,为用户带来更安心、更流畅的飞行体验。

ol7890123的博客 630

Hdu5893树链剖分(2016沈阳网络赛B)

Hdu5893树链剖分(2016沈阳网络赛B)          在另一道题目的基础上的改编题目,只不过把点权改成了边权,因为在边上,所以在合并区间时候注意lca点处的答案不需要合并。写法很多,当时这个check一直没想明白,wa了9发,到最后算是绝杀了吧。树链剖分题目还是完全理解概念以后再碰吧,否则会wa的很惨的。不过这个算法算是很精髓的了,可以解决大部分树链问题。   //#includ

hit_buxiaoyu的博客 874

ANSYS 2022R2 安装教程:系统准备、许可证配置与故障排查

ANSYS 是面向工程仿真的多物理场商业软件平台,其安装远非普通应用软件的图形化向导流程,而涉及操作系统版本、运行库精确版本、硬件虚拟化支持、许可证服务架构等系统级配置。理解 ANSYS License Manager 的服务机制与端口依赖、掌握 Visual C++ 和 .NET Framework 的启用逻辑,是保障 Fluent、Mechanical、HFSS 等求解器稳定调用的基础。该技术栈广泛应用于高校教学、航空航天结构分析、电子设备电磁兼容仿真等场景。本指南聚焦 ANSYS 2022R2 版本在

weixin_28559803的博客 600

HDU 5893 List wants to travel(树链剖分+区间合并)

问你一段路上,不同数字的连续段有多少问你一段路上,不同数字的连续段有多少 就是连续的相同的数字要合并,这就是典型的区间合并问题,然后加上区间赋值就是连续的相同的数字要合并,这就是典型的区间合并问题,然后加上区间赋值 然后每一段查询的时候,区间右端点都要和原来的数字比一下是否相同然后每一段查询的时候,区间右端点都要和原来的数字比一下是否相同 最后两个点都在重链上的时候,最后一个区间,不仅右端点要

Miracle_ma的专栏 584

STM32F103VET6实战:用W25Q64外部Flash实现Bootloader远程升级(含完整代码解析)

本文详细介绍了基于STM32F103VET6与W25Q64外部Flash的Bootloader远程升级实现方案,包含硬件选型、存储分区策略、双校验机制及完整代码解析。通过SPI接口实现固件存储与升级,适用于物联网设备开发,提升OTA功能可靠性。

weixin_42620340的博客 358

STM32 HAL库 实现基于SPI模式的SD卡、TF卡FATS文件系统+模拟U盘的应用笔记

很多单片机不带SDIO接口,所以在使用SD卡时必须使用SPI模式进行读写。 想做一个基于SPI的SD卡文件系统读写SD卡上的文件,再配合USB接口做一个模拟U盘。只能利用业余时间慢慢更新,也不知道能不能坚持下去。 我会把从零开始开发一个程序需要经历的过程一一的记录下来。做成一个奶妈级的单片机开发笔记。 一、硬件简介 1.SD卡和MicroSD卡(TF卡)的区别 由下图可以很容易的看出TF卡只是比SD卡少了一个3脚Vss脚,其它的管脚顺序

KyleKuang的博客 8335

HDU 5893 List wants to travel 【线段树+树链剖分】

#include<bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef unsigned long long ull; typedef long long ll; typedef double db; const ll mod=1000000007; const int N=2e5+10; const int inf=

禅境花园 469

用FPGA控制步进电机是种什么体验?从状态机到分频器,详解Verilog驱动A4988全流程

本文详细介绍了如何使用FPGA通过Verilog语言控制步进电机,从状态机设计到A4988驱动模块的精准控制全流程。FPGA的并行处理能力和纳秒级响应速度使其在步进电机控制中展现出显著优势,特别适合工业自动化和精密仪器应用。通过实战代码示例,展示了如何实现高效的状态机设计和可编程分频器,为开发者提供了一套完整的FPGA驱动步进电机解决方案。

weixin_30983965的博客 329

hdu 5893 List wants to travel 树链剖分 +线段树

http://acm.hdu.edu.cn/showproblem.php?pid=5893 题意: 题意:一棵无根树,两种操作:改变路径上的颜色,和询问路径上有多少段颜色。 裸的树链剖分+线段树维护颜色段, 唯一难点就是合并的时候 如果两个区间段颜色一样,ans--,链同理。   http://blog.youkuaiyun.com/viphong/article/details/5

缺氧 1079

BIDS数据存储格式介绍与dcm2bids安装与使用

BIDS格式&dcm2bids安装与使用

machinelearning_er的博客 7319

Java SSH框架学习

Java SSH框架学习

私信可回答各种问题~ 1792

hdu 5893 List wants to travel 树链剖分求区间段数

用到树链剖分和线段树合并,线段数存储段数即需要包含左端点颜色右端点颜色和段数和,但是难点在树上的查询的时候会牵扯到 不同链之间的交接处是否重色,参考了别人的写法但是代码写的还是冗余度很高,好累,先放着吧,看以后有没有时间来精简下。#pragma comment(linker, "/STACK:1024000000,1024000000") #include <stdio.h> #include <s

Kblacksmith的博客 862

《MPDIoU:边界框回归的高效革新与YOLOv8集成实战指南》

摘要: MPDIoU(多优先级距离IoU)通过引入中心距离、形状差异和尺度变化的三步优化策略,解决了传统IoU在目标检测中的尺度敏感、收敛低效和场景局限问题。相较于GIoU、DIoU等主流方法,MPDIoU在精度、速度和鲁棒性上实现全面提升,尤其在复杂场景和小目标检测中表现突出。通过代码集成和训练优化,MPDIoU在YOLOv8中显著提升COCO数据集AP指标,并在智能安防(密集行人检测)和工业质检(微小缺陷定位)等垂直领域验证了其应用价值。未来研究方向包括动态权重调整和多模态融合,进一步拓展边界框回归的精

m0_64661770的博客 159

[HDU 5893] List wants to travel (树链剖分+区间合并)

链接HDU 5893题意沈阳网赛的第二题。。。 给出一棵N个点的树,边含权,然后是M个操作,每个操作“Change a b c”改变节点a到节点b的路径上权值为c,“Query a b”询问节点a到节点b路径上有多少个权值相同的段。 差不多是裸的树链剖分,维护好树链以后就可以利用线段树记录区间左右端点值做合并了。 这里需要注意的是树上的链的合并计数也需要链的端点值,u、v两点可能需要跨很多链最

mysterious 343

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

HDU 5893 List wants to travel(树链剖分-区间合并-区间更新-入边)

题意: 找书上路径的有多少种不同颜色的更换 思路: 和网上说的一样。。先刷了染色(树链剖分-入点)再来刷这题。。然而发现和直接给点权值不同,给边权值显然麻烦的多,做了两个小时,也没讨论清楚。找了篇思路清晰的博客改了改查询部分就AC了。查询部分有详细注释,就不多记录了,当板子直接上代码 #include #include #include #include #include #in

GameRoad 294

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

HDU 5893 List wants to travel (树链剖分,线段树区间合并)

题意: 给出2个操作, 第一个操作。更改x->y的路径点的权值为w 第二个操作,查询这段路径上有多少段不同的颜色 思路: 之前做过一道染色的题,和这题类似,但是当时是给出点的权值,此题是给出边的权值,按如下代码的操作两题是无差别的。 时刻记录左右端点,按线段树的思想来逐步去掉相同颜色的值 #include #include #include #include #include

GameRoad 299

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

hdu5893 List wants to travel

裸的树链剖分加线段树区间修改 区间合并时需要多注意一点 当时写的很慢 理解不深刻 #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 40005; #define lson l,m,rt<<1 #define rs...

diankeng5481的博客 182

HDU -5893 List wants to travel

List wants to travel Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1521 Accepted Submission(s): 439 Problem Description A boy named List...

EIP_silly的博客 297

[树链剖分] HDU 5893 List wants to travel

List wants to travel题意:给一棵树,每条边有个颜色,两种操作: Change a b c,把a到b的简单路径的边颜色变成c Query a b,查询a到b的简单路径有多少段颜色,连续相邻的同色边算一段。 思路:先建立树剖然后用线段树维护,记录一个区间内的颜色段数,最左颜色和最右颜色,然后就可以区间合并了,需要注意合并的顺序,没想清楚很容易就wa了,我想的是总是按查询的时间顺序把区

穹妹的家 604

2016 ACM/ICPC Reginal Shengyang hdu 5893 List wants to travel(树链剖分 线段树区间更新真蛋疼)★

题意: 一棵无根树,两种操作:改变路径上的颜色,和询问路径上有多少段颜色。 题解: 裸的树链剖分+线段树维护颜色段, 唯一难点就是合并的时候 如果两个区间段颜色一样,ans--,链同理。 所有的区间更新都需要pushdown,好蛋疼啊,WAWAWAWAWA #include #include #include #include #include #

Ezereal的博客 1004

HDU 5893 List wants to travel(树链剖分)

【题目链接】http://acm.hdu.edu.cn/showproblem.php?pid=5893 【题目大意】   给出一棵树,每条边上都有一个边权,现在有两个操作,操作一要求将x到y路径上所有边更改为c权值,操作二要求查询x到y的路径上有几段连续的权值相同的。 【题解】   首先由于是边权,所以把所有边的存下来,做一遍剖分,将权值保存在每条边深度较深的点上,作...

weixin_30302609的博客 120

2016shenyang-1002-HDU5893-List wants to travel-树链剖分+线段树维护不同区间段个数

肯定先无脑树链剖分,然后线段树维护一段区间不同个数,再维护一个左右端点的费用。 线段树更新,pushDown,pushUp的时候要注意考虑链接位置的费用是否相同 还有就是树链剖分操作的时候,维护上一个更新的位置的费用。 总之就是出现区间合并,就考虑总数是否要减一 好想不好写 //场上根本写不完啊 1 /*-------------------------------------...

weixin_30732487的博客 124

HDU 5893 List wants to travel (树链剖分+线段树维护+踩坑)

#include<bits/stdc++.h> using namespace std; #define debug cout<<"YES\n"; #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #define ll long long #define lrt int l,int r,int rt #define ...

满月照亮的路 290

hdu5893 List wants to travel(树链剖分+线段树)

hdu5893http://blog.youkuaiyun.com/carryheart/article/details/52625012 参考队友的博客,总算看懂了,二次学习了,留个记录。

天道酬勤 388

HDU 5893 List wants to travel(树链剖分+线段树)

Description 给出一棵nnn个点的树,每条边有颜色,mmm次操作,操作分两种 Change&nbsp;a&nbsp;b&nbsp;c:Change&nbsp;a&nbsp;b&nbsp;c:Change\ a\ b\ c:把从aaa点到bbb点路径上所有边的颜色变成ccc Query&nbsp;a&nbsp;b:Query&nbsp;a&nbsp;b:Query\ a\ b:查询a...

ZSQ 422

Hdu-5893 List wants to travel(树链剖分)

Problem Description A boy named List who is perfect in English. Now he wants to travel and he is making a plan. But the cost of living in same citie always changes. Now he wants to know how many diff

WZJRJ28 470
上一篇: UVA-11990 动态逆序对
下一篇: POJ-2778 ac自动机+矩阵快速幂
AC_Machine
博客等级 码龄11年 1粉丝 19原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值