分块总结
zzr8178541919 · · 个人记录
分块:是一种很暴力的数据结构。
其实分块所体现的是一种思维,隐含着一种分治的思想。
往往看到其他大佬的分块题的代码都比较长,实际上,分块的代码套路是很明显的,它并没有想像中的那么难。只要思路明确,分块其实是很好入手的一种数据结构。相比于线段树之类的数据结构,它本应该是很好理解的。
这是我第二次学分块了,有必要做一个记录。
第一次学分块的时候,由于学习方式不太正确,只是对着别人的代码抄写,导致学完后对分块的概念相当模糊。感觉分块很难,包括
知道今天重学后,发现这些步骤很模板,很套路。。也很好用。
分块题目的步骤无非就是几个步骤:
有的时候,如果没有
这里以区间求众数(loj)为例,介绍一下分块具体操作。
首先我们先对原序列离散化,然后分块。
通过预处理,我们可以求出以下的信息:第
然后需要注意到一个性质:对于每次询问,我们会将询问的区间分成三段,两端不完整块的和中间的整块。那我说答案一定是中间的整块的最小众数或者是两端的任意一个数。
也就是说,我们真正要管的只有
为什么呢?
可以这么想:假设两端的数不存在,那最小众数一目了然,就是
利用这个性质,本题迎刃而解。
关于分块的相关的步骤流程在代码中都有体现,加上少量的注释,理解分块是很容易的。
最后要提醒自己一件事情:关于对照代码学习这件事情,众说纷纭。以我看来,刚接触一个新的算法的时候,借鉴他人的代码并没有问题,有的时候可以提高自己学习和理解的效率。但真正最重要的是:理解算法的本质与核心。
通过学懂他人的代码之后,你需要重新审视这个算法或题目,并能够通过自己的逻辑和思维构建出一篇自己写的,符合自己逻辑的,属于自己的代码来,这样才算是真正理解了。
不要总是担心自己写出来的东西和别人不一样就有问题,只要是自己能通过理性的思维证明的,自己能够说明是符合正确性的,那它就是一篇无懈可击的代码!或许,通过自己的逻辑构建出来的精品,更有可能成为最优解呢?
#include<bits/stdc++.h>
//#define int long long
#define reg register
using namespace std;
const int maxn=1e5+5;
const int mod=1e9+7;
const int INF=2e9;
inline int read()
{
int x=0,f=1;
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;
}
int n;
int block[maxn];
int tot=0;
int len=0;
int a[maxn];
int L[maxn];
int R[maxn];
int b[maxn];
int s[maxn];
int tot1=0;
int val[maxn];
int dp[325][325];
int sum[325][325];
int Get(int x)
{
return lower_bound(s+1,s+tot1+1,x)-s;
}
int wns[319][100002];
//prework
void Work()
{
for(int i=1;i<=tot;i++)
{
for(int j=L[i];j<=R[i];j++)
{
wns[i][a[j]]++;
}
}
for(int i=1;i<=tot1;i++)
{
for(int j=1;j<=tot;j++)
{
wns[j][i]=wns[j-1][i]+wns[j][i];
}
}
}
int cnt[maxn];
int col[maxn];
int col_tot=0;
int Find(int l,int r)
{
int max_tot=0;
int res=-INF;
for(int i=1;i<=tot1;i++)
cnt[i]=0;
int p=block[l];
int q=block[r];
//the same block
if(p==q)
{
for(int i=l;i<=r;i++)
{
cnt[a[i]]++;
if(cnt[a[i]]>max_tot || (cnt[a[i]]==max_tot && a[i]<res))
{
max_tot=cnt[a[i]];
res=a[i];
}
}
return val[res];
}
//brute force
col_tot=0;
for(int i=l;i<=R[p];i++)
{
cnt[a[i]]++;
if(cnt[a[i]]>max_tot || (cnt[a[i]]==max_tot && a[i]<res))
{
max_tot=cnt[a[i]];
res=a[i];
}
col[++col_tot]=a[i];
}
for(int i=L[q];i<=r;i++)
{
cnt[a[i]]++;
if(cnt[a[i]]>max_tot || (cnt[a[i]]==max_tot && a[i]<res))
{
max_tot=cnt[a[i]];
res=a[i];
}
col[++col_tot]=a[i];
}
// make use of the blocks
for(reg int i=1;i<=col_tot;i++)
{
int wns1=wns[q-1][col[i]]-wns[p][col[i]];
if(cnt[col[i]]+wns1>max_tot || (cnt[col[i]]+wns1==max_tot && col[i]<res))
{
max_tot=cnt[col[i]]+wns1;
res=col[i];
}
}
if(sum[p+1][q-1]>max_tot || (sum[p+1][q-1]==max_tot && dp[p+1][q-1]<res))
{
max_tot=sum[p+1][q-1];
res=dp[p+1][q-1];
}
return val[res];
}
signed main()
{
n=read();
//divide blocks
len=sqrt(n);
tot=n/len;
for(int i=1;i<=tot;i++)
{
L[i]=(i-1)*len+1;
R[i]=i*len;
}
if(n%len)
{
tot++;
L[tot]=R[tot-1]+1;
R[tot]=n;
}
for(reg int i=1;i<=tot;i++)
{
for(reg int j=L[i];j<=R[i];j++)
{
block[j]=i;
}
}
for(reg int i=1;i<=n;i++)
{
a[i]=read();
b[i]=a[i];
}
// li-san-hua
sort(b+1,b+1+n);
b[0]=-INF;
for(reg int i=1;i<=n;i++)
{
if(b[i]!=b[i-1])
s[++tot1]=b[i];
}
for(reg int i=1;i<=n;i++)
{
int wns=Get(a[i]);
val[wns]=a[i];
a[i]=wns;
}
Work();
//prework
for(int i=1;i<=tot;i++)
{
memset(cnt,0,sizeof(cnt));
int max_tot=0;
int res=-INF;
for(int j=i;j<=tot;j++)
{
for(int k=L[j];k<=R[j];k++)
{
cnt[a[k]]++;
if(cnt[a[k]]>max_tot || (cnt[a[k]]==max_tot && a[k]<res))
{
max_tot=cnt[a[k]];
res=a[k];
}
}
dp[i][j]=res;
sum[i][j]=max_tot;
}
}
//Query
for(reg int i=1;i<=n;i++)
{
int l=read(),r=read();
printf("%d\n",Find(l,r));
}
return 0;
}
/*
4
1 3 3 5
1 2
1 4
2 4
3 4
*/