浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=2724

对于每次询问的答案,要么是中间整块的众数,要么是在两侧不完整的块出现过的数。

根据这个性质,我们可以\(O(n\sqrt{n})\)求出每个块的众数和\(sum[i][j]\),表示从第一块到第\(i\)块内\(j\)出现了多少次。

然后再用区间\(dp\)在\(O(n\sqrt{n})\)的复杂度内求出\(mx[i][j]\),表示第\(i\)整块到第\(j\)整块的众数是多少。

对于每次询问,出现在两侧不完整的块的数,我们可以暴力扫描两侧把它们出现的次数丢到一个桶里,在加上在整块里出现的次数。

然后直接找次数最多的那个数就行了。

注意相同比大小要比原大小而不是离散化之后的大小。

时间复杂度:\(O(n\sqrt{n}+m\sqrt{n})\)

空间复杂度:\(O(n\sqrt{n})\)

代码如下:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn=4e4+5; int n,m,cnt,block,top,lstans;
int v[maxn],tmp[maxn],bel[maxn],tot[maxn];
int L[205],R[205],stk[405],mx[205][205],sum[205][maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} int query(int l,int r,int v) {return sum[r][v]-sum[l-1][v];} int main() {
n=read(),m=read(),block=sqrt(n);
for(int i=1;i<=n;i++) {
tmp[i]=v[i]=read();
bel[i]=(i-1)/block+1;
if(bel[i]!=bel[i-1])
R[bel[i-1]]=i-1,L[bel[i]]=i;
}
R[bel[n]]=n,sort(tmp+1,tmp+n+1);
cnt=unique(tmp+1,tmp+n+1)-tmp-1;
for(int i=1;i<=n;i++)
v[i]=lower_bound(tmp+1,tmp+cnt+1,v[i])-tmp;
for(int i=1;i<=bel[n];i++) {
memcpy(sum[i],sum[i-1],sizeof(sum[i]));
int num2=0;
for(int j=L[i];j<=R[i];j++)
sum[i][v[j]]++;
for(int j=L[i];j<=R[i];j++) {
int num1=query(i,i,v[j]);
if((num1>num2)||(num1==num2&&tmp[v[j]]<tmp[mx[i][i]]))
mx[i][i]=v[j],num2=num1;
}
}
for(int len=2;len<=bel[n];len++)
for(int i=1;i+len-1<=bel[n];i++) {
int j=i+len-1,res=mx[i][j-1],num2=query(i,j,res);
for(int k=L[j];k<=R[j];k++) {
int num1=query(i,j,v[k]);
if((num1>num2)||(num1==num2&&tmp[v[k]]<tmp[res]))
res=v[k],num2=num1;
}
mx[i][j]=res;
}
while(m--) {
int l=read(),r=read(),res=0,num2=0;
l=(l+lstans-1)%n+1,r=(r+lstans-1)%n+1;
if(r<l)swap(l,r);
if(bel[l]==bel[r]) {
for(int i=l;i<=r;i++)
if((++tot[v[i]])==1)stk[++top]=v[i];
}
else {
for(int i=l;i<=R[bel[l]];i++)
if((++tot[v[i]])==1) {
stk[++top]=v[i];
tot[v[i]]+=query(bel[l]+1,bel[r]-1,v[i]);
}
for(int i=L[bel[r]];i<=r;i++)
if((++tot[v[i]])==1) {
stk[++top]=v[i];
tot[v[i]]+=query(bel[l]+1,bel[r]-1,v[i]);
}
int tmp=mx[bel[l]+1][bel[r]-1];
if(!tot[tmp]) {
stk[++top]=tmp;
tot[tmp]=query(bel[l]+1,bel[r]-1,tmp);
}
}
for(int i=1;i<=top;i++) {
int num1=tot[stk[i]];
if((num1>num2)||(num1==num2&&tmp[stk[i]]<tmp[res]))
res=stk[i],num2=num1;
}
while(top)tot[stk[top--]]=0;
lstans=tmp[res];
printf("%d\n",lstans);
}
return 0;
}

BZOJ2724:[Violet 6]蒲公英的更多相关文章

  1. [BZOJ2724][Violet 6]蒲公英

    [BZOJ2724][Violet 6]蒲公英 试题描述 输入 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1 输出 输入示 ...

  2. BZOJ2724 [Violet 6]蒲公英 分块

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2724.html 题目传送门 - BZOJ2724 题意 求区间最小众数,强制在线. $n$ 个数,$m ...

  3. bzoj2724: [Violet 6]蒲公英(离散化+分块)

    我好弱啊..这题调了2天QwQ 题目大意:给定一个长度为n(n<=40000)的序列,m(m<=50000)次询问l~r之间出现次数最多的数.(区间众数) 这题如果用主席树就可以不用处理一 ...

  4. bzoj2724: [Violet 6]蒲公英 分块 区间众数 论algorithm与vector的正确打开方式

    这个,要处理各个数的话得先离散,我用的桶. 我们先把每个块里的和每个块区间的众数找出来,那么在查询的时候,可能成为[l,r]区间的众数的数只有中间区间的众数和两边的数. 证明:若不是这里的数连区间的众 ...

  5. 【分块】bzoj2724 [Violet 6]蒲公英

    分块,离散化,预处理出: ①前i块中x出现的次数(差分): ②第i块到第j块中的众数是谁,出现了多少次. 询问的时候,对于整块的部分直接获得答案:对于零散的部分,暴力统计每个数出现的次数,加上差分的结 ...

  6. bzoj2724: [Violet 6]蒲公英(分块)

    传送门 md调了一个晚上最后发现竟然是空间开小了……明明算出来够的…… 讲真其实我以前不太瞧得起分块,觉得这种基于暴力的数据结构一点美感都没有.然而今天做了这道分块的题才发现分块的暴力之美(如果我空间 ...

  7. 【BZOJ2724】[Violet 6]蒲公英 分块+二分

    [BZOJ2724][Violet 6]蒲公英 Description Input 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n ...

  8. BZOJ 2724: [Violet 6]蒲公英

    2724: [Violet 6]蒲公英 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 1633  Solved: 563[Submit][Status ...

  9. BZOJ 2724: [Violet 6]蒲公英( 分块 )

    虽然AC了但是时间惨不忍睹...不科学....怎么会那么慢呢... 无修改的区间众数..分块, 预处理出Mode[i][j]表示第i块到第j块的众数, sum[i][j]表示前i块j出现次数(前缀和, ...

  10. BZOJ_2724_[Violet 6]蒲公英_分块

    BZOJ_2724_[Violet 6]蒲公英_分块 Description Input 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod ...

随机推荐

  1. AngularJS 视图和路由

    在AngularJS之后引用angular-route  路由   ngRoute模块加载声明   AngularJS提供的when和otherwise两个方法来定义应用的路由   otherwise ...

  2. 理解Android Build系统【转】

    本文转载自:http://www.ibm.com/developerworks/cn/opensource/os-cn-android-build/ Android Build 系统是用来编译 And ...

  3. vmxnet3 丢包处理

    https://vswitchzero.com/2017/09/26/vmxnet3-rx-ring-buffer-exhaustion-and-packet-loss/

  4. Centos6.5使用yum安装软件的时候 Another app is currently holding the yum lock; waiting for it to exit...

    Loaded plugins: fastestmirror, refresh-packagekit, security Existing . Another app is currently hold ...

  5. HDU 5884 Sort(2016年青岛网络赛 G 二分+贪心+小优化)

    好题 题意:给你n<=100000个数,每个数范围[0,1000],然后给你一个最大的代价T,每次最多合并k个数成为一个数,代价为k个数的总和.问最后合成1个数的总代价不大于T的最小k 题解:我 ...

  6. skynet中动态库的处理

    skynet中的.so动态库由service-src中的c文件编译完后生成,其中最重要的是snlua.c. 源码地址:https://github.com/cloudwu/skynet/service ...

  7. ie 元素兼容性总结

    css 属性元素 2.z-index 正常按自身层级决定显示顺序,在ie6 7 还需要依赖于父级的层级决定,排布在后边的元素排在前面.后来者居上覆盖前者. IE6,7支持inline元素转换成inli ...

  8. mysql数据简单去重

    我有一个 foo 表,定义了如下几个字段:id / a / b,其中 id 是主键,a,b 原本应该具有唯一性, 但因为程序 bug 导致 a,b 内容有重复,现在我要在 a,b 上加唯一索引,请问如 ...

  9. 理解面向消息的中间件和 JMS

    本章知识点: 企业消息和面向消息的中间件 理解 Java Message Service(JMS) 使用 JMS APIs 发送和接收消息 消息驱动 bean 的一个例子 简介 一般来说,掌握了企业级 ...

  10. Deep Learning(Ian Goodfellow) — Chapter1 Introduction

    Deep Learning是大神Ian GoodFellow, Yoshua Bengio 和 Aaron Courville合著的深度学习的武功秘籍,涵盖深度学习各个领域,从基础到前沿研究.因为封面 ...