P3709 大爷的字符串题【普通莫队】

传送门
给定一个长度为 N N N的数组,求区间 l ∼ r l \sim r lr任意排序后,最小严格递增序列的个数(=区间众数)

分析

就是求 l ∼ r l \sim r lr 区间的众数,离线,不修改
考虑莫队
增加一个数,计数器删除原来次数的出现次数,更新现在的次数,更新答案
删除一个属,计数器更新原来的出现次数,跟新现在的次数,再跟新答案

void add(int x) {
	--same[cnt[arr[x]]];
	++same[++cnt[arr[x]]];
	mo = max(mo, cnt[arr[x]]);
}

void del(int x) {
	--same[cnt[arr[x]]];
	++same[--cnt[arr[x]]];
	while (!same[mo]&&mo) -- mo;
}

c n t cnt cnt 记录的是,出现个数
s a m e same same 记录的是,(出现个数的值)的数量

代码

//P3709
/*
  @Author: YooQ
*/
#include <bits/stdc++.h>
using namespace std;
#define sc scanf
#define pr printf
#define ll long long
#define int long long
#define FILE_OUT freopen("out", "w", stdout);
#define FILE_IN freopen("in", "r", stdin);
#define debug(x) cout << #x << ": " << x << "\n";
#define AC 0
#define WA 1
#define INF 0x3f3f3f3f
const ll MAX_N = 1e6+5;
const ll MOD = 1e9+7;
int N, M, K;

int arr[MAX_N];
int uniarr[MAX_N];
int unicnt = 0;
int block = 1;

int idb(int x) {
	return x / block;
}

struct Qr {
	int l, r, id;
	bool operator < (const Qr& B) const {
		return idb(l) ^ idb(B.l) ? l < B.l : (idb(l)&1) ? r < B.r : r > B.r;
	}
}qr[MAX_N];

int cnt[MAX_N];
int same[MAX_N];

int mo = 0;

void add(int x) {
	--same[cnt[arr[x]]];
	++same[++cnt[arr[x]]];
	mo = max(mo, cnt[arr[x]]);
}

void del(int x) {
	--same[cnt[arr[x]]];
	++same[--cnt[arr[x]]];
	while (!same[mo]&&mo) -- mo;
}

int ans[MAX_N];

void solve(){
	sc("%lld%lld", &N, &M);
	block = max(sqrt(N), sqrt((N*N)/M));
	
	for (int i = 1; i <= N; ++i) {
		sc("%lld", &arr[i]);
		uniarr[i] = arr[i];
	}
	unicnt = N;
	
	int l, r;
	for (int i = 1; i <= M; ++i) {
		sc("%lld%lld", &l, &r);
		qr[i] = {l, r, i};
	}
	
	sort(qr+1, qr+1+M);
	
	sort(uniarr+1, uniarr+1+unicnt);
	unicnt = unique(uniarr+1, uniarr+1+unicnt) - uniarr - 1;
	
	for (int i = 1; i <= N; ++i) {
		arr[i] = lower_bound(uniarr+1, uniarr+1+unicnt, arr[i]) - uniarr;
	}
	
	l = 1, r = 0;
	for (int i = 1; i <= M; ++i) {
		while (l > qr[i].l) add(--l);
		while (r < qr[i].r) add(++r);
		while (l < qr[i].l) del(l++);
		while (r > qr[i].r) del(r--);
		ans[qr[i].id] = mo;
	}
	
	for (int i = 1; i <= M; ++i) {
		pr("%lld\n", -ans[i]);
	}
}

signed main()
{
	#ifndef ONLINE_JUDGE
	//FILE_IN
	FILE_OUT
	#endif
	int T = 1;//cin >> T;
	while (T--) solve();

	return AC;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Hexrt

客官,请不要给我小费!

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

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

打赏作者

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

抵扣说明:

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

余额充值