ZOJ 1610 Count the Colors

NC65实现Restful接口开发及调试 阅读详情
//线段树涂色问题: 对每一段区间进行染色 统计出现的颜色和该颜色的长度
//这里建图时 以线段长度1为单位节点

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 20001;
int temp;

struct Node
{
	int L;
	int R;
	int kind;
};
Node segtree[N];

int color[8001];

void create_tree( int n, int l, int r )
{                    
	segtree[n].L = l;
	segtree[n].R = r;
	segtree[n].kind = -1;

	if( l + 1 == r ) return;

	int mid = ( l + r ) >> 1;
	create_tree( n * 2, l, mid );
	create_tree( n * 2 + 1, mid, r );
}

void insert_tree( int n, int l, int r, int c) 
{             
	if( l == r ) return;
	
	if( segtree[n].kind == c ) return;			//颜色相同,返回 

	if( l == segtree[n].L && segtree[n].R == r ) 
	{                
		segtree[n].kind = c;
		return;
	}
	
	if( segtree[n].kind >= 0 ) //存在颜色,做更新
	{                        
		segtree[ n * 2 ].kind = segtree[n].kind;
		segtree[ n * 2 + 1 ].kind = segtree[n].kind;
		segtree[n].kind = -2;       //传值下去,完成该整段区间更新后赋值-2
	}
	
	int mid = ( segtree[n].L + segtree[n].R ) >> 1;
	
	if( r <= mid ) 
	{
		insert_tree( n * 2, l, r, c );
	}
	else if( l >= mid ) 
	{
		insert_tree( n * 2 + 1, l, r, c );
	}
	else 
	{
		insert_tree( n * 2, l, mid, c);
		insert_tree( n * 2 + 1, mid, r, c );
	}
}

void count( int n ) //先序遍历
{                                      
	if( segtree[n].kind != -1 && segtree[n].kind != -2 )  //颜色不为-1和-2 
	{          
		if( segtree[n].kind != temp ) 
		{
			color[ segtree[n].kind ]++;
			temp = segtree[n].kind;
		}
		return;
	}
	
	if( segtree[n].L + 1 != segtree[n].R ) //非子节点
	{                 
		count( n * 2 );
		count( n * 2 + 1 );
	}
	
	else temp = -1;
	
}

int main() 
{
	int i, t;
	while( scanf("%d", &t) != EOF ) 
	{
		create_tree( 1, 0, 8000 );

		for(i=0;i<t;i++)
		{
			int a,b,c;

			scanf("%d%d%d", &a, &b, &c);

			insert_tree(1, a, b, c);
		}
		temp = -1;

		fill(color,color+8001,0);

		count(1);

		for( i = 0; i <= 8000; i++ ) 
		{
			if( color[i] ) printf("%d %d\n", i, color[i]);
		}
		printf("\n");
		
	}
	return 0;
}

//线段树指针形式 
//注释的代码 WA 未解

#include<iostream>
#include<cstdio>
using namespace std;

const int MaxN=8002;

struct Cnode
{
    int L,R;      
    Cnode *pLeft,*pRight; 
	
    int col; 
};

Cnode Tree[MaxN*2];    

int Ans[MaxN];
int Col[MaxN];

int nCount=0; 

int m;
int temp;


void BuildTree(Cnode *pRoot,int L,int R) //Notice ---> 每个节点代表一个区间
{
    pRoot->L=L;
    pRoot->R=R;
    pRoot->col=-1;
	
    if(L+1==R) 
	{
		pRoot->pLeft=NULL;
		pRoot->pRight=NULL;
		return;
	}
	
	nCount++;
	pRoot->pLeft = Tree + nCount;
	nCount++;
	pRoot->pRight = Tree + nCount;
	
	BuildTree( pRoot->pLeft, L, ( L + R )/2);
	BuildTree( pRoot->pRight, (L + R) / 2, R);
}

int Mid( Cnode * pRoot)
{
    return (pRoot->L + pRoot->R)/2;
}

void update(Cnode *pRoot,int a,int b,int c)
{	
	if(pRoot->col == c) return; //同色不处理
	
    if(pRoot->L==a && pRoot->R==b) //完全覆盖才涂色 
    {
        pRoot->col=c;
        return ;
    }
	
    if(pRoot->col>=0)
    {
        pRoot->pLeft->col = pRoot->pRight->col = pRoot->col;
        pRoot->col=-2;
    }
	
    if(b<=Mid(pRoot)) update(pRoot->pLeft,a,b,c);
	
    else if(a>= (Mid(pRoot)) ) update(pRoot->pRight,a,b,c);
	
    else 
    {
        update(pRoot->pLeft, a, Mid(pRoot), c);
        update(pRoot->pRight, Mid(pRoot), b, c);
    }
}

/*void Query(Cnode *pRoot,int a,int b)
{
	if(pRoot == NULL) return;

    if(pRoot->col!=-1 && pRoot->col!=-2)
    {
        for(int i=pRoot->L; i<=pRoot->R; i++) 
		{
			Col[i]=pRoot->col;
		}
		return;
    }

    if(b<= Mid(pRoot) ) 
		
        Query(pRoot->pLeft,a,b);
	
    else if(a >=(Mid(pRoot)) ) 
		
        Query(pRoot->pRight,a,b);
	
    else 
    {
		Query(pRoot->pLeft, a, Mid(pRoot));
		Query(pRoot->pRight, Mid(pRoot), b);
    }
}*/

void count( Cnode *pRoot ) //先序遍历
{  
	if( pRoot->col != -1 && pRoot->col != -2 )  //颜色不为-1和-2 
	{          
		if( pRoot->col != temp ) 
		{
			Ans[ pRoot->col ]++;
			temp = pRoot->col;
		}
		return;
	}
	
	if( pRoot->L + 1 != pRoot->R ) //非子节点
	{                 
		count( pRoot->pLeft );
		count( pRoot->pRight );
	}
	
	else temp = -1;
	
}

void run()
{
    int i;
	
    nCount=0;
    BuildTree(Tree,0,MaxN);
	
	fill(Ans,Ans+MaxN,0);
	fill(Col,Col+MaxN,-1);
	
    for(i=0;i<m;i++) 
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        update(Tree,a,b,c);
    }
	
    /*Query(Tree,0,MaxN);
	
	int init=-1;
	
	for(i=0;i<MaxN;i++) if(Col[i]!=-1)
	{
		if(Col[i]!=init)
		{
			Ans[Col[i]]++;
			init=Col[i];
		}
	}*/

	temp = -1;

	count(Tree);
	
	for(i=0;i<MaxN;i++) if(Ans[i])
	{
		printf("%d %d\n",i,Ans[i]);
	}
	printf("\n");
}

int main()
{
	while(scanf("%d",&m)!=EOF) run();
    return 0;
}


Example

4

0 4 4
0 3 1
3 4 2
0 2 2
0 2 3

-1---->未赋值

-2---->该节点已经结束lazy操作往下跟新



电磁场仿真软件:COMSOL Multiphysics_(17).时域与频域分析 时域和频域分析是电磁场仿真中非常重要的两种方法,每种方法都有其独特的应用场景和优势。时域分析适用于瞬态响应和宽带信号的分析,而频域分析则适用于稳态响应和窄带信号的分析。在COMSOL Multiphysics中,通过合理设置物理场接口、材料属性、边界条件、网格划分和求解器,可以高效地进行这两种分析,并通过傅里叶变换将时域结果转换为频域结果,进一步拓展仿真应用的范围。合理的性能优化措施,如网格划分优化和并行计算,可以显著提高仿真的效率和准确性。 阅读详情

相关推荐

零基础玩转华为AP 4050DN-HD:FAT模式家庭无线网络配置实战

本文为零基础用户提供华为AP 4050DN-HD从FIT模式转换为FAT模式的完整实战教程。详细介绍了固件升级、模式转换以及配置家庭无线网络(包括路由、DHCP、Wi-Fi和NAT)的每一步操作,帮助用户将企业级瘦AP改造为高性能家庭胖AP,打造稳定可靠的家庭网络环境。

weixin_29196613的博客 630

ZOJ--1610-Count the Colors

题目链接:ZOJ--1610-Count the Colors 要注意 更新区间的时候 如果是 1 3 1 1 2 2 3 4 3 这种情况的时候应该是三种颜色 要注意 0 #include<iostream> #include<algorithm> #include<cmath> #include<cstri...

dianzanji4884的博客 145

基于FSL的DTI数据预处理流程

最近在学习处理DTI数据,总结了一份应用FSL做DTI数据预处理的流程与大家交流交流。如果有错误的地方欢迎大家指正!        我用的数据是Philips的数据,如果是GE或者西门子的数据可能会有所不同。   原始数据:包括3DT1数据和高分辨率DTI数据,均是dicom格式。 工具:MRICRON和FSL,所有的命令均在Linux的终端中运行。   1.格式转换:将后缀名为dcm

zaza_zaza的博客 2万+

线段树增强训练题 ZOJ1610 Count the Colors

线段树增强训练题 ZOJ1610 Count the Colors 线段树增强训练,啊啊啊,我还是太菜了 这道题调试起码4个小时,明明一道很简单的题目。。。。 题意也看了好久,与一开始还读错题意了,但是问题不大,在原来的代码上改改也可 题意:对一段长为8000的线段进行染色,颜色有8000种,每次对区间[l,r]进行染色,进行n次操作后,问你每一种颜色有几段。 很明显的区间修改和查询,不假思索就...

qq_33437973的博客 232

内网渗透—横向移动&IPC横移&Impacket套件&CS插件

以上便是IPC横移的知识。最后,以上仅为个人的拙见,如何有不对的地方,欢迎各位师傅指正与补充,有兴趣的师傅可以一起交流学习。

2301_76227305的博客 1890

LTX-2.3 V1.6:8G显存本地AI视频生成工具与int8量化加速实践

这次我们来看一个本地部署的AI视频生成工具——LTX-2.3文字图片生成带音频视频工具V1.6版本。这个工具最大的特点是支持int8量化加速,相比原版提速2-4倍,效果更好,而且8G显存就能运行,不需要搭建复杂环境,解压即可使用。 对于想要在本地电脑上快速生成带音频视频内容的用户来说,这个工具解决了几个关键痛点:首先是硬件门槛低,8G显存意味着大多数主流游戏显卡都能胜任;其次是部署简单,避免了Python环境配置、依赖包冲突等常见问题;最后是性能优化明显,int8加速让生成速度大幅提升。 本文会重点演示如何

hqmln的博客 1234

ZOJ 1610 Count the Colors(线段树经典)

Count the Colors Time Limit: 2000 msMemory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can see at last.

深秋无痕 611

从SolidWorks到URDF:插件使用、关节配置与PyBullet仿真避坑指南

本文提供了一份从SolidWorks导出URDF模型并导入PyBullet进行仿真的完整实战指南。详细讲解了SolidWorks插件的正确安装与配置、关节类型的选择与坐标系对齐的核心技巧,并针对PyBullet仿真中常见的模型散架、路径错误、物理属性异常等问题提供了深度避坑解决方案,帮助机器人开发者高效完成从设计到仿真的全流程。

weixin_29081101的博客 542

ZOJ - 1610 Count the Colors(模拟,线段树)

vjudge链接https://vjudge.csgrandeur.cn/problem/ZOJ-1610 ZOJ官方链接https://zoj.pintia.cn/problem-sets/91827364500/problems/91827365109 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N = 8010; int a[N], colo

00后的博客 455

AI落地困局与破局者FDE:从技术神话到价值实干的战略转型

AI落地困局与破局者FDE:从技术神话到价值实干的战略转型 当前AI企业级应用面临三大核心挑战:业务场景深度非标化、技术落地"最后一公里"困局、以及市场竞争焦点转向实用价值。前线部署工程师(FDE)作为破局关键,通过技术-业务-产品三维交汇的复合能力,将AI从实验室推向生产环境。FDE不仅是技术架构师,更是业务价值挖掘机和产品进化传感器,通过"碎石路-高速公路"双循环模式,将定制化需求沉淀为可复用的平台能力。这种模式颠覆了传统SaaS的销售逻辑,形成基于价值的J型增长

随笔记录-分享&记忆 3993

指纹浏览器指纹工厂(下):基于真实设备数据特征的马尔可夫链生成与校验

本文揭示了静态指纹生成算法在高级风控时序追踪下的致命缺陷,指出违背设备演进马尔可夫性质(如无状态跳跃、因果断裂)是导致指纹存活率骤降的核心原因。通过分析风控系统的时序状态机与多维特征验证机制,提出基于隐马尔可夫模型(HMM)的动态指纹架构:以真实设备的状态转移链为蓝本,构建异步演进的“数字基因熔炉”,通过非均匀时间步长、突发事件响应及微观因果绑定,模拟软硬件独立更新的物理法则。最终实现指纹在时间维度与微观特征上的自洽,规避风控的时序陷阱。

数据知道的博客 416

Count the Colors (ZOJ-1610)

Count the Colors (ZOJ-1610) Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different col...

菜鸡的博客 324

CANoe/CAPL ,远程邮件通知

CANoe/CAPL ,远程邮件通知

蚂蚁小兵 3036

ZOJ1610 Count the Colors

思路:区间更新,另外千万不能拿n建树,不然就会segmentation fault,必须拿8000建树,也就是树是固定的 #include<bits/stdc++.h> #define fi first #define se second #define INF 0x3f3f3f3f #define ll long long #define ld long double #defi...

Endeavor_G的博客 310

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

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

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【ZOJ 1610】Count the Colors

问题描述 Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can see at last.

Fallen_Angel001的博客 423

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

ZOJ 1610 Count the Colors(线段覆盖着色:离散化)

ZOJ 1610 Count the Colors(线段着色:离散化) http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 题意:        给你n条[0,8000]范围内的带不同颜色的线段,问你最终该线段都有哪些颜色,且这些颜色分别出现了多少次(即在多少段线段内出现了)? 分析:        本题可以

code 1219

zoj1610——Count the Colors

题目大意:给一个分成n段的木板涂色,后序涂色可能会覆盖之前的涂色,问最终露出来的颜色和他们的段数 输入:(可以有很多case)             n(1             第i次涂色的左端点 右端点  所涂的颜色序号(共n次涂色各占一行,所有数据的范围都是 [0, 8000]) 输出:(每个case的输出中间有一个空行)             最终漏出的颜色序号  涂了它

tzyshiwolaogongya的博客 278

ZOJ1610Count the Colors

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can see at last. Inp...

Mr.zhou 235

ZOJ - 1610 Count the Colors

题目: Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can see at last. I...

qq_41998938的博客 226

ZOJ 1610 - Count the Colors

Count the ColorsTime Limit: 2 Seconds      Memory Limit: 65536 KB Paintingsome colored segments on a line, some previously painted segments may becovered by some the subsequent ones.Your task iscounti...

Change the world by program. 229

zoj 1610 Count the Colors

/* zoj_1610 模拟 线段树的典型题,不过好久没碰线段树了。发现数据才8000试了下模拟,果然 没有TLE。 */ #include #include #include #include #include #define N 8010 using namespace std; struct segment { int l,r,v; segment( int

I am coming! 2354

Count the Colors ZOJ - 1610

GO 题意:给一个区间,有n次染色操作,每次将[x1, x2]染为c,求最后每种颜色各有多少线段可以看到。 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include&...

weixin_30578677的博客 167
上一篇: VMware 8虚拟机安装 Mac OS X 10.7 Lion正式版
下一篇: HDU 1892 See you~ 二维树状数组
Matrix_Reloaded
博客等级 码龄14年 18粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值