2018湘潭大学-H统计颜色

链接:https://www.nowcoder.com/acm/contest/105/H
来源:牛客网

时间限制:C/C++ 1秒,其他语言2
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

n个桶按顺序排列,我们用1~n给桶标号。有两种操作:
1 l r c
区间[l,r]中的每个桶中都放入一个颜色为c的球 (1≤l,r ≤n,l≤r,0≤c≤60)
2 l r   
查询区间[l,r]的桶中有多少种不同颜色的球     (1≤l,r ≤n,l≤r)

输入描述:

有多组数据,对于每组数据:
第一行有两个整数n,m(1≤n,m≤100000)
接下来m行,代表m个操作,格式如题目所示。

输出描述:

对于每个2号操作,输出一个整数,表示查询的结果。

示例1

输入

10 10

1 1 2 0

1 3 4 1

2 1 4

1 5 6 2

2 1 6

1 7 8 1

2 3 8

1 8 10 3

2 1 10

2 3 8

输出

2

3

2

4

3
题解:将每一个桶定义一个vector  就好了
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
struct node
{
	int l;
	int r;
} plane;

vector<node>a[65];
int main()
{
	int n,m;
	int l,r,c;
	int flag;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
	for(int i=0;i<=60;i++)
	{
		a[i].clear();
	}
	while(m--)
	{
	int cnt=0;
	scanf("%d",&flag);
	
	if(flag==1)
	{
		scanf("%d%d%d",&l,&r,&c);
		plane.l=l;
		plane.r=r;
		a[c].push_back(plane);
		
	}
	if(flag==2)
	{
		scanf("%d%d",&l,&c);
		for(int i=0;i<=60;i++)
		{
			for(int j=0;j<a[i].size();j++)
			if(!(r<a[i][j].l || l>a[i][j].r))
			{
				cnt++;
				break;
			}
		}
		printf("%d\n",cnt);
	}
		
	}
		
	}
	
	return 0;
	
}

猜你喜欢

转载自blog.csdn.net/qq_40816078/article/details/80196700