跳到正文

题解

P1903 [国家集训队] 数颜色 / 维护队列

xixihaha2021约 8 分钟
原文
文章信息与版本

文章信息

发布
2024年12月26日
收录
2025年12月12日
更新
2026年10月27日
篇幅
约 8 分钟

版本

  1. v1当前

P1903 [国家集训队] 数颜色 / 维护队列

题意简述

给定一个序列,维护若干次操作:

  1. 单点修改。
  2. 区间查询有多少种数。

思路简述

本文中定义 [a,a][a,a] 表示集合 {a}\{a\}。

前置知识:曼哈顿距离

形象地理解成两个点在网格图中,只走网格需要经过多少条边长。即 disAB=∣xA−xB∣+∣yA−yB∣dis_{AB}=|x_A-x_B|+|y_A-y_B|。高维类似。

前置知识:莫队

对于处理区间上的问题,假如可以在 O(1)O(1) 内通过 [l,r][l,r] 的答案,转移到 [l−1,r],[l+1,r],[l,r−1],[l,r+1][l-1,r],[l+1,r],[l,r-1],[l,r+1],那么只需将询问离线下来,对询问分成 n\sqrt{n} 块。然后以左界所在块的编号为第一关键字,右界所在块的编号为第二关键字排序,处理区间时直接转移即可。时间复杂度 O(nn)O(n\sqrt{n})。

显然如上算法具有离线不带修的特点。假如只有若干次操作 2,那么该算法可以很好地解决问题。对于这道题,我们需要使用带修改莫队。

首先考虑莫队的几何意义。定义每个区间为一个整点,建立平面直角坐标系。分别以 [1,1][1,1] 和 [n,n][n,n] 为左下角和右上角作正方形,则每个区间都可用不处于正方形反对角线下方的整点表示。那么,两点的曼哈顿距离就是两个区间的转移费用。

带修改莫队

类似于 DP,考虑对于每个点增加一维 tt,表示经历了 tt 次修改的答案,类似于普通莫队算法建立空间直角坐标系,对询问分成 n23n^{\frac{2}{3}} 块,共计 n13n^{\frac{1}{3}} 块,然后以左界为第一关键字,右界为第二关键字,tt 为第三关键字排序,然后类似于普通莫队算法转移即可。具体参考代码。

块长的取值

设序列长 nn,询问个数为 mm,修改次数为 tt,块长为 BB,左端点所处块的编号为 ii,右端点所处块的编号为 jj 的询问个数为 qi,jq_{i,j}。则:

Θ(B)=∑i=1[nB]∑j=i+1[nB]B⋅qi,j+t+C=(B⋅∑i=1[nB]∑j=i+1[nB]qi,j)+∑i=1[nB]∑j=i+1[nB]t=B⋅m+t[nB]([nB]−1)2=B⋅m+t⋅n22B2−tn2B\begin{aligned} \Theta(B)=&\sum_{i=1}^{[\frac{n}{B}]}\sum_{j=i+1}^{[\frac{n}{B}]}B\cdot q_{i,j}+t+C \\ =&(B \cdot \sum_{i=1}^{[\frac{n}{B}]}\sum_{j=i+1}^{[\frac{n}{B}]}q_{i,j})+\sum_{i=1}^{[\frac{n}{B}]}\sum_{j=i+1}^{[\frac{n}{B}]}t \\ =&B\cdot m+\dfrac{t[\frac{n}{B}]([\frac{n}{B}]-1)}{2} \\ =&B\cdot m+\dfrac{t\cdot n^2}{2B^2}-\dfrac{tn}{2B} \end{aligned}

即:

Θ′(B)=m−4tn2B4B4+2tn4B2=m−tn2B3+tn2B2\Theta'(B)=m-\dfrac{4tn^2B}{4B^4}+\dfrac{2tn}{4B^2}=m-\dfrac{tn^2}{B^3}+\dfrac{tn}{2B^2}

令 Θ′(B)=0\Theta'(B)=0,解得 B=−−3m2tn+12mnt324m2n2+6mtn3+−3m2tn−12mnt324m2n2+6mtn324mB=-\dfrac{\sqrt[3]{-3m^2tn+12mnt\sqrt{324m^2n^2+6mtn}}+\sqrt[3]{-3m^2tn-12mnt\sqrt{324m^2n^2+6mtn}}}{24m} 且为极小值点。

当 n,m,tn,m,t 同阶时,时间复杂度可以近似地认为是 O(n53)O(n^{\frac{5}{3}}),此时 Bmin=23n23B_{min}=\sqrt[3]{2}n^{\frac{2}{3}}。空间复杂度 O(B)O(B)。

代码实现

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
ll n,q,L = 1,R,T,l,r,tot,otp[1000005],a[1000005],b[1000005],pos[1000005],ne[1000005],pre[1000005],sum[1000005],ans,cnt;
pair<pair<ll,ll>,pair<ll,ll> > query[1000005];
char op;
ll read(){
	ll x = 0;
	bool f = true;
	char ch = getchar();
	while(!isdigit(ch)){
		if(ch == '-')f = !f;
		ch = getchar();
	}
	while(isdigit(ch))x = x * 10 + ch - '0',ch = getchar();
	return x * f * 2 - x;
}
ll write(ll x,string ch){
	ll f = 1,L = ch.size(),tmp = x;
	if(tmp < 0){
		putchar('-');
		tmp *= -1;
	}
	while(f <= tmp)f *= 10;
	if(tmp)f /= 10;
	while(f){
		putchar(tmp / f + '0');
		tmp -= (tmp / f * f),f /= 10;
	}
	for(ll i = 0;i <= L - 1;i++)putchar(ch[i]);
	return x;
}
ll f(ll x){
	return (x - 1) / 2615 + 1;
}
bool cmp(pair<pair<ll,ll>,pair<ll,ll> > query1,pair<pair<ll,ll>,pair<ll,ll> > query2){
	if(f(query1.first.first) != f(query2.first.first))return f(query1.first.first) < f(query2.first.first);
	if(f(query1.first.second) != f(query2.first.second))return f(query1.first.second) < f(query2.first.second);
	return query1.second.first < query2.second.first;
}
void add(ll w){
	if(!sum[w])ans++;
	sum[w]++;
}
void del(ll w){
	sum[w]--;
	if(!sum[w])ans--;
}
int main(){
	n = read(),q = read();
	for(ll i = 1;i <= n;i++)a[i] = b[i] = read();
	for(ll i = 1;i <= q;i++){
		scanf(" %c",&op);
		l = read(),r = read();
		if(op == 'Q')cnt++,query[cnt] = make_pair(make_pair(l,r),make_pair(tot,cnt)),query[cnt].second.second = cnt;
		else pos[++tot] = l,ne[tot] = r,pre[tot] = b[l],b[l] = r;
	}
	sort(query + 1,query + cnt + 1,cmp);
	for(ll i = 1;i <= cnt;i++){
		while(L < query[i].first.first)del(a[L++]);
		while(L > query[i].first.first)add(a[--L]);
		while(R < query[i].first.second)add(a[++R]);
		while(R > query[i].first.second)del(a[R--]);
		while(T < query[i].second.first){
			T++;
			if(L <= pos[T] && pos[T] <= R){
				del(a[pos[T]]);
				add(ne[T]);
			}
			a[pos[T]] = ne[T];
		}
		while(T > query[i].second.first){
			if(L <= pos[T] && pos[T] <= R){
				del(a[pos[T]]);
				add(pre[T]);
			}
			a[pos[T]] = pre[T],T--;
		}
		otp[query[i].second.second] = ans;
	}
	for(ll i = 1;i <= cnt;i++)write(otp[i],"\n");
	return 0;
}
原文