bzoj2724: [Violet 6]蒲公英(离散化+分块)
我好弱啊。。这题调了2天QwQ
题目大意:给定一个长度为n(n<=40000)的序列,m(m<=50000)次询问l~r之间出现次数最多的数。(区间众数)
这题如果用主席树就可以不用处理一堆前缀和。。蓝鹅我不会~T_T~
把序列n分成sqrt(n)块,先把所有数离散化,预处理出poi[i][j]表示第i块到第j块的众数(即出现次数最多的数)。
询问有两种情况:
第一种情况是l~r在某个块中,那么直接扫一遍求出众数,效率O(sqrt(n))。
第二种情况是l~r在多个块中,l在x块,r在y块,那么我们可以把它分为三部分:①l~x块最后一个数②x+1块~y-1块③y块第一个数~r。
因为我们求出了poi数组,所以我们可以知道第二部分的众数,显然我们只要统计一下第一部分、第三部分每个数出现的次数,将它们和第二部分的众数出现的次数进行比较,出现次数最多的数就是l~r的众数。
而第一部分、第三部分数的个数不超过2*sqrtn(n)个,所以扫一遍第一部分、第三部分的数,效率O(sqrt(n)),问题就是怎么O(1)求出第一部分、第三部分的每个数在l~r出现的次数了。如果是O(1),那么可以想到的就是前缀和,于是我们再预处理出qzh[i][a]表示前i块中a出现的次数,qzh2[i][j][a]表示第i块前j个数中a出现的次数。那么设第一部分某个数为a,l在x块,r在y块,它在l~r中出现的次数就是:
qzh[y-1][a]-qzh[x-1][a]-qzh2[x][(l-1)%sqrtn==0?sqrtn:(l-1)%sqrtn][a]
+qzh2[x][(r-1)%sqrtn+1==sqrtn?0:(r-1)%sqrtn+1][a]
【写的好丑哇QAAAAQ
第三部分同理,然后和第二部分预处理出来的众数在l~r出现的次数(求法同理)比较找出最大的就行了。
预处理O(nsqrt(n)),询问O(qsqrt(n)),总的时间复杂度O((n+q)sqrt(n))。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
map<int,int>M[];
struct zs{int num,pos;}b[];
int n,m,l,r,cnt,ans,poi[][],a[],pos[],sum[],qzh[][],qzh2[][][],num[],ppos[][];
bool v[];
bool cmp(zs a,zs b){return a.num<b.num;}
int main()
{
scanf("%d %d",&n,&m);
int sqrtn=(int)ceil(sqrt(n));
for(int i=;i<=n;i++)scanf("%d",&a[i]),b[i].num=a[i],b[i].pos=i;
sort(b+,b++n,cmp);
for(int i=;i<=n;i++)
{
if(b[i].num!=b[i-].num)cnt++;
pos[b[i].pos]=cnt;
}
for(int i=;i<=sqrtn;i++)
for(int j=;j<=sqrtn;j++)
{
if((i-)*sqrtn+j<=n)if(M[i].find(a[(i-)*sqrtn+j])==M[i].end())
M[i][a[(i-)*sqrtn+j]]=++num[i];
ppos[i][pos[(i-)*sqrtn+j]]=M[i][a[(i-)*sqrtn+j]];
}
for(int i=;i<=n;i++)qzh[((i-)/sqrtn)+][pos[i]]++;
for(int i=;i<=n;i++)for(int j=;j<=sqrtn;j++)qzh[j][i]+=qzh[j-][i];
for(int i=;i<=sqrtn;i++)for(int j=;j<=sqrtn;j++)if((i-)*sqrtn+j<=n)qzh2[i][j][ppos[i][pos[(i-)*sqrtn+j]]]++;
for(int i=;i<=sqrtn;i++)
{
for(int k=;k<=sqrtn;k++)if((i-)*sqrtn+k<=n)v[pos[(i-)*sqrtn+k]]=;
for(int k=;k<=sqrtn;k++)
if((i-)*sqrtn+k<=n)
if(!v[pos[(i-)*sqrtn+k]])
{
for(int j=;j<=sqrtn;j++)
if((i-)*sqrtn+j<=n)qzh2[i][j][ppos[i][pos[(i-)*sqrtn+k]]]+=qzh2[i][j-][ppos[i][pos[(i-)*sqrtn+k]]];
v[pos[(i-)*sqrtn+k]]=;
}
}
for(int i=;i<=sqrtn;i++)
{
memset(sum,,sizeof(sum));
int max=,maxi=;
for(int j=i;j<=sqrtn;j++)
{
for(int k=;k<=sqrtn;k++)
{
int x=(j-)*sqrtn+k;
if(x>n)break;
if(++sum[pos[x]]>max||((sum[pos[x]]==max)&&(pos[x]<pos[maxi])))max=sum[pos[x]],maxi=x;
}
poi[i][j]=maxi;
}
}
for(int i=;i<=m;i++)
{
scanf("%d %d",&l,&r);
l=(l+ans-)%n+;r=(r+ans-)%n+;if(r<l)swap(l,r);
if((int)ceil(r/sqrtn)==(int)ceil(l/sqrtn))
{
int maxx=,maxi=;
memset(sum,,sizeof(sum));
for(int j=l;j<=r;j++)
if(++sum[pos[j]]>maxx||((sum[pos[j]]==maxx)&&(pos[j]<pos[maxi])))maxx=sum[pos[j]],maxi=j;
ans=a[maxi];
printf("%d\n",a[maxi]);
}else
{
int ll=-,rr=;
for(int j=;j<=sqrtn;j++)
{
if(j*sqrtn+>=l&&(ll==-))ll=j*sqrtn;
if(j*sqrtn<=r)rr=j*sqrtn;
}
int y=poi[ll/sqrtn+][rr/sqrtn];
for(int j=l;j<=ll;j++)
{
int lll=y;
int x=qzh[rr/sqrtn][pos[j]]-qzh[max(ll/sqrtn-,)][pos[j]]-qzh2[ll/sqrtn][(l-)%sqrtn==?sqrtn:(l-)%sqrtn][ppos[ll/sqrtn][pos[j]]]+qzh2[rr/sqrtn+][(r-)%sqrtn+==sqrtn?:(r-)%sqrtn+][ppos[rr/sqrtn+][pos[j]]];
y=qzh[rr/sqrtn][pos[lll]]-qzh[max(ll/sqrtn-,)][pos[lll]]-qzh2[ll/sqrtn][(l-)%sqrtn==?sqrtn:(l-)%sqrtn][ppos[ll/sqrtn][pos[lll]]]+qzh2[rr/sqrtn+][(r-)%sqrtn+==sqrtn?:(r-)%sqrtn+][ppos[rr/sqrtn+][pos[lll]]];
if(x>y||((x==y)&&pos[j]<pos[lll]))y=j;else y=lll;
}
for(int j=rr+;j<=r;j++)
{
int lll=y;
int x=qzh[rr/sqrtn][pos[j]]-qzh[max(ll/sqrtn-,)][pos[j]]-qzh2[ll/sqrtn][(l-)%sqrtn==?sqrtn:(l-)%sqrtn][ppos[ll/sqrtn][pos[j]]]+qzh2[rr/sqrtn+][(r-)%sqrtn+==sqrtn?:(r-)%sqrtn+][ppos[rr/sqrtn+][pos[j]]];
y=qzh[rr/sqrtn][pos[lll]]-qzh[max(ll/sqrtn-,)][pos[lll]]-qzh2[ll/sqrtn][(l-)%sqrtn==?sqrtn:(l-)%sqrtn][ppos[ll/sqrtn][pos[lll]]]+qzh2[rr/sqrtn+][(r-)%sqrtn+==sqrtn?:(r-)%sqrtn+][ppos[rr/sqrtn+][pos[lll]]];
if(x>y||((x==y)&&pos[j]<pos[lll]))y=j;else y=lll;
}
printf("%d\n",a[y]);ans=a[y];
}
}
}
bzoj2724: [Violet 6]蒲公英(离散化+分块)的更多相关文章
- bzoj2724: [Violet 6]蒲公英(分块)
传送门 md调了一个晚上最后发现竟然是空间开小了……明明算出来够的…… 讲真其实我以前不太瞧得起分块,觉得这种基于暴力的数据结构一点美感都没有.然而今天做了这道分块的题才发现分块的暴力之美(如果我空间 ...
- [BZOJ2724][Violet 6]蒲公英
[BZOJ2724][Violet 6]蒲公英 试题描述 输入 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1 输出 输入示 ...
- BZOJ_2724_[Violet 6]蒲公英_分块
BZOJ_2724_[Violet 6]蒲公英_分块 Description Input 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod ...
- BZOJ2724 [Violet 6]蒲公英 分块
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2724.html 题目传送门 - BZOJ2724 题意 求区间最小众数,强制在线. $n$ 个数,$m ...
- 【分块】bzoj2724 [Violet 6]蒲公英
分块,离散化,预处理出: ①前i块中x出现的次数(差分): ②第i块到第j块中的众数是谁,出现了多少次. 询问的时候,对于整块的部分直接获得答案:对于零散的部分,暴力统计每个数出现的次数,加上差分的结 ...
- bzoj2724: [Violet 6]蒲公英 分块 区间众数 论algorithm与vector的正确打开方式
这个,要处理各个数的话得先离散,我用的桶. 我们先把每个块里的和每个块区间的众数找出来,那么在查询的时候,可能成为[l,r]区间的众数的数只有中间区间的众数和两边的数. 证明:若不是这里的数连区间的众 ...
- BZOJ.2724.[Violet 6]蒲公英(静态分块)
题目链接 区间众数 强制在线 考虑什么样的数会成为众数 如果一个区间S1的众数为x,那么S1与新区间S2的并的众数只会是x或S2中的数 所以我们可以分块先预处理f[i][j]表示第i到第j块的众数 对 ...
- [BZOJ 2724] [Violet 6] 蒲公英 【分块】
题目链接:BZOJ - 2724 题目分析 这道题和 BZOJ-2821 作诗 那道题几乎是一样的,就是直接分块,每块大小 sqrt(n) ,然后将数字按照数值为第一关键字,位置为第二关键字排序,方便 ...
- 【BZOJ2724】[Violet 6]蒲公英 分块+二分
[BZOJ2724][Violet 6]蒲公英 Description Input 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n ...
随机推荐
- apache+php开发环境搭建步骤
apache 卸载apache服务命令:sc delete apache 1.在D盘下面新建文件夹php7 2.解压apache到php7文件夹下面 3.修改配置文件 4.安装apache服务C:\w ...
- unity实现框选效果
思路: 在uinity中既可以将屏幕坐标转换为世界坐标,也可以将世界坐标转换为屏幕坐标.这样的话我们就可以通过判断物体在世界坐标转换为平幕坐标是否在鼠标框选的矩形区域坐标内,来判断物体是否在框选范围. ...
- 82. Single Number [easy]
Description Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Example Given [1, ...
- 浙江天搜科技落棋人工智能,加速AI产业布局
8月31日,2018年IFA大展在德国柏林正式开幕.IFA是全球三大消费电子展之一,在世界范围内久负盛名,被誉为“未来科技风向标”.在这个万众瞩目的展会上,号称“给智能世界铺上云的跑道,装上智能发动机 ...
- Java Web开发框架Spring+Hibernate整合效果介绍(附源码)(已过期,有更好的)
最近花了一些时间整合了一个SpringMVC+springAOP+spring security+Hibernate的一套框架,之前只专注于.NET的软件架构设计,并没有接触过Java EE,好在有经 ...
- JQuery文本框验证
<" CODEPAGE="936"%><!--#include file="conncon.asp"--><!--#in ...
- codeforces 301D Yaroslav and Divisors(树状数组)
Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he ...
- HDU 3062 Party(2-SAT模版题)
Problem Description 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是 ...
- StrBlobPtr类——weak_ptr访问vector元素
#include <iostream> #include <memory> #include <string> #include <initializer_l ...
- Android开发随笔5
昨天: 对界面的进一步设计补充 可以在界面之间的跳转 研究了对图标等的操作 今天: 实现对库的相关操作 学习视视频内容‘ 复习java的一些知识.