牛客网暑期ACM多校训练营(第一场)J.Different Integers (莫队算法)

题目链接

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.

输入描述:

The input consists of several test cases and is terminated by end-of-file.
The first line of each test cases contains two integers n and q.
The second line contains n integers a1, a2, ..., an.
The i-th of the following q lines contains two integers li and ri.

输出描述:

For each test case, print q integers which denote the result.

 

示例1

输入

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

输出

2
1
3

备注:

* 1 ≤ n, q ≤ 105
* 1 ≤ ai ≤ n
* 1 ≤ li, ri ≤ n
* The number of test cases does not exceed 10.

题意:给出N个数(1~N),M次询问,随后M行是询问的L和R,要求输出每个询问的答案([1,L]和[R,N]中不同种类数字的个数)

题解:对于这道题我最初的做法是使用莫队算法+输入挂卡过去的.时间复杂度为O(nsqrt(n)),理论上是卡不过去的,但是如果数据乐观还是可以玄学卡过去的(表示成功了)

        对于莫队算法的使用,我们只需要维护每个数出现的频率即可,当出现频率减到0时tmp值减一,当频率从0到1是,tmp值加一.

        (tmp是莫队L,R移动过程中维护区间ans的值)

代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
#define ll long long
const  int maxn = 1e5 + 5;
int read() {                    //输入挂
	int x = 0, f = 1; register char ch = getchar();
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x*f;
}
struct  Query                   //询问区间
{                               //和编号
	int  L, R, id;
}q[maxn];
int ans[maxn];
int a[maxn];
int f[maxn];                    //f[i]为(L,R)区间外每个元素出现的频率
int  n, m, unit;
int tmp;
bool  cmp(Query a, Query b)     //左端点的块编号作为第一关键字
{                               //右端点的编号作为第二关键字
	if (a.L / unit != b.L / unit)return a.L / unit < b.L / unit;
	else  return a.R < b.R;
}
void add(int x) {               //(L,R)区间大小变大
	f[a[x]]--;                  //区间[1,L]和区间[R,n]减小
	if (f[a[x]] == 0)tmp--;
}
void del(int x) {                //(L,R)区间大小变小
	if (f[a[x]] == 0)tmp++;      //区间[1,L]和区间[R,n]增大
	f[a[x]]++;
}
void solve() {                   //离线处理函数
	tmp = 0;
	int L = 0, R = 0, MAX;
	for (int i = 1; i <= n; i++) {
		if (f[a[i]] == 0)tmp++;
		f[a[i]]++;
	}
	MAX = tmp;
	for (int i = 0; i < m; i++)
	{
		if (q[i].L + 1 > q[i].R - 1) {
			ans[q[i].id] = MAX;
			continue;
		}
		while (R < q[i].R)
		{
			add(R);
			R++;
		}
		while (R > q[i].R)
		{
			R--;
			del(R);
		}
		while (L < q[i].L)
		{
			L++;
			del(L);
		}
		while (L > q[i].L)
		{
			add(L);
			L--;
		}
		ans[q[i].id] = tmp;
	}
}
int  main() {
	while (~scanf("%d%d", &n, &m)) {
		for (int i = 1; i <= n; i++) a[i] = read();
		for (int i = 0; i < m; i++) {
			q[i].id = i;
			q[i].L = read();
			q[i].R = read();
		}

		unit = sqrt(n);            //块距
		sort(q, q + m, cmp);

		memset(f, 0, sizeof(f));
		solve();

		for (int i = 0; i < m; i++)
			printf("%d\n", ans[i]);
	}
	return  0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值