C - Count Color


Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. 

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board: 

1. "C A B C" Color the board from segment A to segment B with color C. 
2. "P A B" Output the number of different colors painted between segment A and segment B (including). 

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your. 

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2
1
 
  
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 100005

struct node 
{
	int l;
	int r;
	int color;
}t[MAXN*3];
bool r[50];
void make(int st,int ed,int i)
{
	t[i].l=st;
	t[i].r=ed;
	t[i].color=1;
	if(st==ed)
	{
		return ;
	}
	int mid=(t[i].l+t[i].r)/2;
	make(st,mid,i*2);
	make(mid+1,ed,i*2+1);
}

void updata(int st,int ed,int i,int c)
{
	if(st==t[i].l&&ed==t[i].r)
	{
		t[i].color=c;
		return ;
	}
	if(t[i].color>0)
	{
		t[i*2+1].color=t[i*2].color=t[i].color;
		t[i].color=0;
	}
	int mid=(t[i].l+t[i].r)/2;
	if(st>mid)
		updata(st,ed,i*2+1,c);
	else if(ed<=mid)
		updata(st,ed,i*2,c);
	else 
	{
		updata(st,mid,i*2,c);
		updata(mid+1,ed,i*2+1,c);
	}
}


void query(int st,int ed,int i)
{
	if(t[i].color>0)
	{
		r[t[i].color]=1;
		return ;
	}
	int mid=(t[i].l+t[i].r)/2;
	if(st>mid)
		query(st,ed,i*2+1);
	else if(ed<=mid)
		query(st,ed,i*2);
	else
	{
		query(st,mid,i*2);
		query(mid+1,ed,i*2+1);
	}
}

int main()
{
	int  L,T,Q;
	char ch[2];
	int count;
	int start,end ,c;
	scanf("%d%d%d",&L,&T,&Q);
	make(1,L,1);
	while(Q--)
	{
		scanf("%s",ch);
		if(strcmp(ch,"C")==0)
		{
			scanf("%d%d%d",&start,&end,&c);
			updata(start,end,1,c);
		}
		if(strcmp(ch,"P")==0)
		{
			count=0;
			memset(r,0,sizeof(r));
			scanf("%d%d",&start,&end);
			query(start,end,1);
			for(int i=1;i<=30;i++)
				if(r[i]==1)
					count++;
			printf("%d\n",count);
		}
	}
	return 0;
}


<think>根据提供的CSS代码,我们分析为什么布局不是左右两列展示。 主要问题可能出现在以下几个方面: 1. `.icon-item` 的宽度设置:`width: calc(50% - 5px);` 并且 `.icon-item:nth-child(odd)` 设置了 `margin-right: 10px;`。这样奇数项右边有10px的外边距,而偶数项没有,可能导致换行问题。 2. 容器 `.icons-box` 的 `flex-wrap: wrap;` 和 `gap: 10px;`,但是注意在 `.icon-item` 上还设置了 `margin-bottom: 10px;`,同时因为使用了 `gap`,所以垂直方向上的间隙可能会叠加(实际不会,因为gap是行间距和列间距)。 3. 计算宽度时,`calc(50% - 5px)` 减去5px是因为左右两列之间期望的间距是10px,然后每个元素占一半减去一半的间距。但是注意,在设置`gap: 10px;`的情况下,这个计算可能就不准确了,因为flex容器的gap会在每个项目之间添加间隙。 实际上,flex布局中,使用`gap`属性可以设置项目之间的间隙,所以我们不需要再手动设置项目的左右margin来制造间隙。而且,由于设置了`gap: 10px;`,那么每个项目之间的水平间隙和垂直间隙都是10px。 因此,我们可以简化: - 每个项目设置宽度为50%,然后利用gap来设置间隙。 - 但是注意,50%加上间隙可能会导致换行,因为总宽度可能超过100%。正确的做法是:设置每个项目为`flex: 0 0 calc(50% - 10px * 1/2);`?实际上,在gap存在的情况下,我们可以直接设置每个项目为`flex: 0 0 calc(50% - 5px);`?或者更合理的做法是直接设置`width: calc(50% - 5px);`,因为gap会占据10px,所以两个项目加上中间的gap就是100%。但是注意,由于gap是分布在两个项目之间的,所以每个项目实际上需要减去的是半个gap?不对,因为两个项目之间只有1个gap,所以每个项目宽度应该是:`(100% - gap) / 2`。即:`width: calc(50% - 5px);` 这里减去的5px就是10px(gap)的一半,所以是合理的。 但是,为什么没有左右两列展示呢?可能的原因: 1. 容器宽度不够,或者有其他内边距影响,导致实际计算后的项目宽度加上间隙超过了50%,从而换行?但这里每个项目宽度设置为50%减去5px,两个项目加上中间的10px间隙正好是100%:`(50% - 5px)*2 + 10px = 100%`。所以理论上不会换行,应该是并排的。 2. 另外一个可能的原因是,`.icon-item:nth-child(odd)` 设置 `margin-right: 10px;`,这会在奇数项右边再加10px,这样就会导致奇数项的总宽度为:`calc(50% - 5px) + 10px`,超过了50%,导致换行。所以这个设置是多余的,因为我们已经通过gap设置了10px的间距。 因此,解决方案是: 1. 去掉 `.icon-item:nth-child(odd)` 的 `margin-right` 设置。 2. 确保使用gap来设置项目之间的间隙。 修改后的CSS建议: ```css .dz-list { --image-model_icon-item: url('@/assets/images/home-green/1600.png'); width: 100%; height: 100%; } .icons-box { width: 100%; height: 100%; display: flex; flex-wrap: wrap; gap: 10px; /* 设置行和列的间隙为10px */ overflow-y: hidden; padding: 5px; box-sizing: border-box; } .icons-box:hover { overflow-y: auto; } .icon-item { /* 每个项目宽度:50%减去一半的间隙(10px的一半是5px) */ width: calc(50% - 5px); /* 或者使用flex属性,但这里固定宽度也可以,因为每行两个 */ height: 46px; background: var(--image-model_icon-item) no-repeat; background-size: 100% 100%; position: relative; display: flex; align-items: center; justify-content: center; /* 不需要设置margin-right和margin-bottom,因为gap已经设置 */ } /* 去掉这个规则,因为我们已经用gap代替 */ /* .icon-item:nth-child(odd) { margin-right: 10px; } */ .level-text { width: 76%; display: flex; justify-content: space-around; position: absolute; top: 10px; left: 76px; color: #ffffffe0; font-size: 14px; text-align: center; letter-spacing: 1px; } .level-text .num-count, .level-text .num-count1 { font-family: 'douyuFont'; font-size: 12px; } .level-text .warn-count .num-count, .level-text .close-count .num-count { color: #ffffffe0; } .level-text .warn-count .num-count1, .level-text .close-count .num-count1 { color: #ff0000; } .icon-item img { width: 100%; height: 60px; } ``` 这样修改后,每个项目宽度为`calc(50% - 5px)`,加上gap的10px,两个项目刚好占满一行(因为两个项目的宽度加上一个间隙:`(50%-5px)*2 + 10px = 100%`)。并且由于gap的存在,项目之间会自动有10px的间隙。 注意:如果容器有内边距(padding),则需要考虑。这里容器有`padding:5px`,那么内部项目的宽度计算是基于容器内容区域的(即减去padding后的宽度)。所以计算是合理的。 如果仍然不是两列,请检查: 1. 容器是否确实有足够的宽度? 2. 是否有其他CSS规则覆盖? 另外,还可以考虑使用`flex-basis`来设置项目的基础宽度,并利用gap,这样更符合flex布局的特性: ```css .icon-item { flex: 0 0 calc(50% - 5px); /* 其他样式 */ } ``` 但以上修改应该可以解决问题。 </think>根据您提供的CSS代码,我来分析为什么布局没有呈现左右两列效果: ### 问题原因分析: 1. **宽度计算冲突**: ```css .icon-item { width: calc(50% - 5px); /* 每个元素宽度设为容器一半减5px */ margin-right: 10px; /* 但奇数项有10px右边距 */ } ``` 实际宽度计算:`(50% - 5px) + 10px = 50% + 5px`,导致奇数项实际宽度超过50%,无法并排显示。 2. **布局逻辑冲突**: - Flex容器的`gap: 10px`已经提供元素间距 - 额外添加的`margin-right: 10px`和`margin-bottom: 10px`导致宽度计算混乱 ### 修复后的代码: ```css .icons-box { display: flex; flex-wrap: wrap; gap: 10px; /* 用gap统一控制间距 */ } .icon-item { width: calc(50% - 5px); /* 每个项目占一半宽度减去一半gap */ height: 46px; /* 移除所有margin设置 */ background: var(--image-model_icon-item) no-repeat; background-size: 100% 100%; position: relative; display: flex; align-items: center; justify-content: center; } /* 删除以下冲突规则 */ /* .icon-item:nth-child(odd) { margin-right: 10px; } */ /* .icon-item { margin-bottom: 10px; } */ ``` ### 关键修改说明: 1. **移除所有margin设置**: - 使用`gap`属性统一控制行列间距 - 避免margin导致的宽度计算溢出 2. **正确的宽度计算**: - 公式:`(容器宽度 - gap) / 列数` - 此处:`calc(50% - 5px)` (因为gap=10px,每项分摊5px) 3. **布局逻辑简化**: ```mermaid graph LR A[Flex容器] --> B[gap:10px] A --> C[项目1 width:50%-5px] A --> D[项目2 width:50%-5px] ``` ### 完整修复示例: ```html <style> .dz-list { --image-model_icon-item: url('@/assets/images/home-green/1600.png'); width: 100%; height: 100%; } .icons-box { width: 100%; height: 100%; display: flex; flex-wrap: wrap; gap: 10px; /* 统一控制行列间距 */ overflow-y: hidden; padding: 5px; box-sizing: border-box; } .icons-box:hover { overflow-y: auto; } .icon-item { flex: 0 0 calc(50% - 5px); /* 关键修复 */ height: 46px; background: var(--image-model_icon-item) no-repeat; background-size: 100% 100%; position: relative; display: flex; align-items: center; justify-content: center; } .level-text { width: 76%; display: flex; justify-content: space-around; position: absolute; top: 10px; left: 76px; color: #ffffffe0; font-size: 14px; text-align: center; letter-spacing: 1px; } /* ...其他样式保持不变... */ </style> ``` ### 布局原理: 1. **flex容器**:通过`display: flex` + `flex-wrap: wrap`启用换行 2. **间隙控制**:`gap: 10px` 自动处理行列间距 3. **项目尺寸**:`calc(50% - 5px)` 确保两个项目加一个间隙正好等于100%宽度 ``` (50% - 5px) + 10px + (50% - 5px) = 100% ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值