赛氪-zeal

11 篇文章 0 订阅
4 篇文章 0 订阅

zeal

题目描述

Yassin 最近在量化投资方面很有兴趣。
为了研究哪只股票是真正的牛股,他把历史 nn 天每一天成交量最大的股票代码写成了一排,并构建了一套属于自己的“理论体系”。
成交量多说明人气好,人气好的肯定买的人多,赚钱就要靠人气! – Yassin
但是知道的人太多,这个大家都去接盘,那就都成为韭菜了 – Makik
基于这个理论,Yassin 想知道 [L, R] 区间中人气“比较”好的股票有哪些,具体而言,他会给定你 L, R, k,你则需要告诉他 [L, R] 中出现 k 次的股票有多少只。
例如 n = 5 时,假设这个代码序列为 {1, 1, 2, 3, 1} 现在他给了一个询问 (2, 5, 1),你就需要回答他 {a2,a3,a4,a5} 中恰好出现 1 次的有多少种元素。答案显然为 2,恰好出现一次的元素为 2,3。
数据保证 0<ai,k<=n<=4×104,q<=104,L<=R。

输入

第一行 2 个数字 n, q, 分别表示序列的长度和询问的个数。
接下来一行 nn 个数字,为序列 an
接下来 qq 行,每行三个数字 L, R, k, 如题目描述所述。

输出

共 q 行,每行一个数字,为对应询问的答案。

Sample Input

5 1
1 1 2 3 1
2 5 1

Sample Output

2

思路解析

这是一个基础的莫队模板题,如果没有头绪,可以看下面的GIF图你就能明确了。

10 10
5 9 4 9 10 6 1 4 6 8
3 7 1
6 8 1
1 8 1
5 10 1
1 5 1
3 7 1
2 5 1
8 10 1
1 10 1
9 10 1

因为我们是从0开始计算的,所以我们每个位置都要减一。
这个数据为例,我们来探究。
莫队第一步,分块。至于为什么分块,请看其余的博客,了解莫队算法,这里我们默认知道第一步就是要分块,做离线处理。
首先先对数据进行分块。
在这里插入图片描述
接下来对我们的数据进行排序,做分块离线处理。

//排序方法
bool cmp(node a, node b)
{
	if (block[a.l] == block[b.l])
	{
		if (block[a.l] & 1) return a.r < b.r;
		return a.r > b.r;
	}
	return block[a.l] < block[b.l];
}

我们直接看最后的结果

序号LR
104
214
326
426
507
609
749
857
979
1089

我们可以发现1-6为第0块,我们的排序方法是按照R的值升序排序,7-8是第一块我们降序排序,9-10为第三块,升序排序。排序过程就不过多赘述,下面我们就开始我们核心算法。两个指针的移动。
在这里插入图片描述
莫队的核心思想有点像dp,比如
你要算100的阶乘,如果告诉你99的阶乘那么你直接乘以100就可以,如果告诉你99的阶乘,要算98的阶乘,那么除99就可以了。
我们这里就有一个答案区间,每当指针移动的时候我们就更新这个区间,去掉的元素删除,新增的元素添加。
这里妙就妙在分块,成功的优化了暴力的复杂度,也就是说我们把这个数据变得理想化,让我们去执行起来很方便。
这里说一下,排序的时候让每一块的升降序规则不一样,这样能够保证我们可以走到最远处之后还可以沿着原路走回最近处,再沿着最近处走到最远处。

Accepted Code

//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a) {
	if (a < 0)putchar('-'), a = -a;
	if (a > 9)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}
#define read read()
const int N = 4 * 1e4;
int block[N + 7];
struct node
{
	int l, r, ask, id, ans;
}query[N + 7];
int save[N + 7];
bool cmp(node a, node b)
{
	if (block[a.l] == block[b.l])
	{
		if (block[a.l] & 1) return a.r > b.r;
		return a.r < b.r;
	}
	return block[a.l] < block[b.l];
}
int res[N + 7];
int cnt[N + 7];
void del(int num)
{
	res[cnt[num]]--;
	cnt[num]--;
	res[cnt[num]]++;
}
void add(int num)
{
	res[cnt[num]]--;
	cnt[num]++;
	res[cnt[num]]++;
}
int main()
{
	int n = read, m = read;
	int base = sqrt(n);
	for (int i = 0; i < n; i++)
		block[i] = i / base;
	for (int i = 0; i < n; i++)save[i] = read;
	for (int i = 0; i < m; i++)
	{
		query[i].l = read - 1, query[i].r = read - 1, query[i].ask = read;
		query[i].id = i;
	}
	sort(query, query + m, cmp);
	int l = 0, r = 0;
	cnt[save[0]]++;
	res[1]++;
	for (int i = 0; i < m; i++)
	{
		int nowl = query[i].l, nowr = query[i].r;
		while (l < nowl)del(save[l++]);
		while (l > nowl)add(save[--l]);
		while (r < nowr)add(save[++r]);
		while (r > nowr)del(save[r--]);
		query[i].ans = res[query[i].ask];

	}
	sort(query, query + m, [](node a, node b) {return a.id < b.id; });
	for (int i = 0; i < m; i++)
		cout << query[i].ans << endl;
	return 0;
}

By-Round Moon

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Round moon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值