跳到正文

题解

题解:P7828 [CCO 2021] Swap Swap Sort

zyn0309约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年10月24日
收录
2025年12月12日
更新
2026年10月27日
篇幅
约 12 分钟
题目
P7828

版本

  1. v2当前
  2. v1

题目传送门

在线根号分治做法。

思路

首先答案是按目标序列重新标号后的逆序对数,这是显然的,不知道的可以去做 P1966。

然后初始状态的答案只需要求一遍整个序列的逆序对即可。

因为我们每次只会交换目标序列的临项,所以只需要考虑这量项的贡献变化。

首先把相同的交换去掉记忆化一下,然后对于交换的两个颜色,我们可以提前把所有颜色对应的下标压到一个 vector 里面,求逆序对数的变化量直接对两个跑一遍归并即可。

这样就能获得 98 分。

发现这样的问题在于同一种颜色的下标比较多的时候,我们归并一遍的复杂度就可以达到 O(n)O(n)。

于是自然考虑根号分治,对于所有下标数量大于 n\sqrt n 的颜色预处理前缀/后缀个数,在归并时如果发现其中一边是预处理过的就枚举另一边的所有下标,然后去预处理的数组里查询,代码如下:

C++
inline ll solve(int a,int b){
	ll cnt=0,now=0,len2=v[b].size(),len1=v[a].size();
	if(w[id[a]]){
	  for(int x:v[b])
		cnt+=pre[w[id[a]]][x-1];
	}
	else if(w[id[b]]){
	  for(int x:v[a])
		cnt+=suf[w[id[b]]][x+1];
	}
	else{
	  for(int i=0;i<len2;++i){
		while(now<len1&&v[a][now]<v[b][i])
		  ++now;
		cnt+=now;
	  }
	}
	return cnt;
}

分析一下操作的时间复杂度:

两个都没有被预处理过,说明大小都小于阈值,复杂度 O(n)O(\sqrt n)。

其中一个被预处理过,也就是另一个的大小小于阈值,复杂度 O(n)O(\sqrt n)。

最后是两个都被预处理过,这时不太好按照大小来分析了,但是注意到一共只预处理过 O(n)O(\sqrt n) 个颜色,因为我们去掉了相同询问,所以说每个被预处理的下标在这种情况下只会被遍历 O(n)O(\sqrt n) 次。得出这一情况的复杂度是均摊 O(nn)O(n \sqrt n) 的。

时间复杂度 O((n+q)n)O((n+q) \sqrt n),空间复杂度 O(nn)O(n \sqrt n),注意块长开大点防止被卡空间。

Code

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
namespace fast_IO{
	#define IOSIZE 100000
		char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
	#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
	#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
	#define isdigit(ch) (ch>47&&ch<58)
	#define isspace(ch) (ch<33)
		template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
		template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
		template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
		inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
		inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
		inline void print(char x) { putchar(x); }
		inline void print(char *x) { while (*x) putchar(*x++); }
		inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
		inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
		inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
		inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
		inline void print(bool b) { putchar(b+48); }
		template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
		template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
		struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
		template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
		template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
	#define cout io
	#define cin io
}using namespace fast_IO;
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pb emplace_back
#define ull unsigned long long
const int N=1e5+10,limit=800;
int n,k,q,a[N],val[N],pos[N],T[N],id[N],pre[N/limit+5][N],suf[N/limit+5][N],w[N],tot;
#define lowbit(x) (x&(-x))
#define pii  pair<int,int>
#define mk make_pair
cc_hash_table<ull,ll>mp;
inline void add(int x,int k){
	while(x<=n){
	  T[x]+=k;
	  x+=lowbit(x);
	}
}
inline int sum(int x){
	int res=0;
	while(x>0){
	  res+=T[x];
	  x-=lowbit(x);
	}
	return res;
}
vector<int>v[N];
inline ll solve(int a,int b){
	ll cnt=0,now=0,len2=v[b].size(),len1=v[a].size();
	if(w[id[a]]){
	  for(int x:v[b])
		cnt+=pre[w[id[a]]][x-1];
	}
	else if(w[id[b]]){
	  for(int x:v[a])
		cnt+=suf[w[id[b]]][x+1];
	}
	else{
	  for(int i=0;i<len2;++i){
		while(now<len1&&v[a][now]<v[b][i])
		  ++now;
		cnt+=now;
	  }
	}
	return cnt;
}
mt19937_64 rnd(time(0));
const ull x=rnd(),y=rnd();
inline ull hashs(ull a,ull b){return a*x+b*y;}
signed main(){
	cin>>n>>k>>q;
	for(int i=1;i<=n;++i)cin>>a[i],v[a[i]].pb(i);
	for(int i=1;i<=k;++i)val[i]=pos[i]=id[i]=i;
	for(int i=1;i<=k;++i){
	  if((int)v[i].size()<=limit)continue;
	  w[i]=++tot;
	  for(int x:v[i])++pre[w[i]][x],++suf[w[i]][x];
	  for(int j=1;j<=n;++j)pre[w[i]][j]+=pre[w[i]][j-1];
	  for(int j=n;j>=1;--j)suf[w[i]][j]+=suf[w[i]][j+1];
	}
	int x;
	ll ans=0;
	for(int i=k;i>=1;--i){
	  for(int x:v[i])ans+=sum(x-1);
	  for(int x:v[i])add(x,1);
	}
	fill(T+1,T+1+n,0);
	while(q--){
	  cin>>x;
	  swap(val[x],val[x+1]);
	  swap(pos[val[x]],pos[val[x+1]]);
	  swap(id[pos[val[x]]],id[pos[val[x+1]]]);
	  if(mp.find(hashs(id[pos[val[x]]],id[pos[val[x+1]]]))!=mp.end()){
	  	swap(v[pos[val[x]]],v[pos[val[x+1]]]);
		ans+=mp[hashs(id[pos[val[x]]],id[pos[val[x+1]]])];
		cout<<ans<<"\n";
		continue;
	  }
	  ll delta=0;
	  swap(id[pos[val[x]]],id[pos[val[x+1]]]);
	  if(pos[val[x]]<pos[val[x+1]])delta-=solve(pos[val[x+1]],pos[val[x]]);
	  else delta-=solve(pos[val[x]],pos[val[x+1]]);
	  swap(v[pos[val[x]]],v[pos[val[x+1]]]);
	  swap(id[pos[val[x]]],id[pos[val[x+1]]]);
	  if(pos[val[x]]<pos[val[x+1]])delta+=solve(pos[val[x+1]],pos[val[x]]);
	  else delta+=solve(pos[val[x]],pos[val[x+1]]);
	  mp[hashs(id[pos[val[x]]],id[pos[val[x+1]]])]=delta;
	  mp[hashs(id[pos[val[x+1]]],id[pos[val[x]]])]=-delta;
	  ans+=delta;
	  cout<<ans<<"\n";
	}
	return 0;
}
原文