题意

在线区间众数

思路

预处理出 f[i][j] 即从第 i 块到第 j 块的答案。
对于每个询问,中间的整块直接用预处理出的,两端的 sqrtn 级别的数暴力做,用二分查找它们出现的次数。
每次询问的复杂度是 sqrtn * logn 。

 #include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N=;
vector<int> vec[N];
int n,m,a[N],b[N],block[N],ans,Block,L[N],R[N],cnt[N],top,stack[N],f[][],ff[][],tot;
int find1(int x,int y){
int l=;int r=vec[x].size()-;
int tmp=-;
while(l<=r){
int mid=(l+r)>>;
if(vec[x][mid]<=y){
tmp=mid;
l=mid+;
}
else r=mid-;
}
return tmp;
}
int find2(int x,int y){
int l=;int r=vec[x].size()-;
int tmp=;
while(l<=r){
int mid=(l+r)>>;
if(vec[x][mid]>=y){
tmp=mid;
r=mid-;
}
else l=mid+;
}
return tmp;
}
int main(){
scanf("%d%d",&n,&m);
Block=sqrt(n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int num=unique(b+,b++n)-b-;
for(int i=;i<=n;i++){
a[i]=lower_bound(b+,b++num,a[i])-b;
block[i]=(i-)/Block+;
vec[a[i]].push_back(i);
if(!L[block[i]])L[block[i]]=i;
R[block[i]]=i;
}
for(int i=;i<=block[n];i++){
tot=;
for(int j=L[i];j<=n;j++){
cnt[a[j]]++;
if(tot<=cnt[a[j]]){
if(cnt[a[j]]>tot)ans=b[a[j]];
else ans=min(ans,b[a[j]]);
tot=cnt[a[j]];
}
f[i][block[j]]=tot;
ff[i][block[j]]=ans;
}
for(int j=L[i];j<=n;j++){
cnt[a[j]]=;
}
}
ans=;
for(int i=;i<=m;i++){
int l,r;
scanf("%d%d",&l,&r);
l=(l+ans-)%n+;r=(r+ans-)%n+;
if(l>r)swap(l,r);
if(block[l]+>=block[r]){
tot=;
ans=;
for(int i=l;i<=r;i++){
cnt[a[i]]++;
if(tot<=cnt[a[i]]){
if(cnt[a[i]]>tot)ans=b[a[i]];
else ans=min(ans,b[a[i]]);
tot=cnt[a[i]];
}
}
for(int i=l;i<=r;i++){
cnt[a[i]]=;
}
}
else{
top=;
tot=f[block[l]+][block[r]-];
ans=ff[block[l]+][block[r]-];
for(int i=l;i<=R[block[l]];i++){
cnt[a[i]]++;
if(cnt[a[i]]==)stack[++top]=a[i];
}
for(int i=L[block[r]];i<=r;i++){
cnt[a[i]]++;
if(cnt[a[i]]==)stack[++top]=a[i];
}
while(top){
int z=stack[top--];
int tmp=max(,find1(z,L[block[r]]-)-find2(z,R[block[l]]+)+);
if(tot<=cnt[z]+tmp){
if(cnt[z]+tmp>tot)ans=b[z];
else ans=min(ans,b[z]);
tot=cnt[z]+tmp;
}
cnt[z]=;
}
}
printf("%d\n",ans);
}
return ;
}

BZOJ 2724 [Violet 6]蒲公英(分块)的更多相关文章

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

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

  2. BZOJ 2724: [Violet 6]蒲公英 [分块 区间众数]

    传送门 题面太美不忍不放 分块分块 这种题的一个特点是只有查询,通常需要预处理:加入修改的话需要暴力重构预处理 预处理$f[i][j]$为第i块到第j块的众数,显然$f[i][j]=max{f[i][ ...

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

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

  4. [BZOJ 2724] [Violet 6] 蒲公英 【分块】

    题目链接:BZOJ - 2724 题目分析 这道题和 BZOJ-2821 作诗 那道题几乎是一样的,就是直接分块,每块大小 sqrt(n) ,然后将数字按照数值为第一关键字,位置为第二关键字排序,方便 ...

  5. BZOJ.2724.[Violet 6]蒲公英(静态分块)

    题目链接 区间众数 强制在线 考虑什么样的数会成为众数 如果一个区间S1的众数为x,那么S1与新区间S2的并的众数只会是x或S2中的数 所以我们可以分块先预处理f[i][j]表示第i到第j块的众数 对 ...

  6. 【刷题】BZOJ 2724 [Violet 6]蒲公英

    Description Input 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1 Output Sample Input ...

  7. 【BZOJ 2724】 2724: [Violet 6]蒲公英 (区间众数不带修改版本)

    2724: [Violet 6]蒲公英 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 1908  Solved: 678 Description In ...

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

    2724: [Violet 6]蒲公英 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 2900  Solved: 1031[Submit][Statu ...

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

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

随机推荐

  1. sicily 1342 开心的金明 (动规)

    刷一下简单的背包问题 以下为代码: //1342. 开心的金明 #include <iostream> using namespace std; #define MAX(a,b) a> ...

  2. 亿财道APP赚钱攻略,亿财道,一个看广告年入36万的APP

            亿财道(http://etway.net/),一款看广告(传单)赚钱的软件,这是一项革新的广告产品,代替了以往的纸质传单.在商家节约成本的同时,还给阅读者佣金,推广也有相应提成比例. ...

  3. Codeforces 987A. Infinity Gauntlet(手速题,map存一下输出即可)

    解法: 1.先将对应的字符串存入map. 2.然后将输入的串的second置为空. 3.输出6-n,输出map中的非空串. 代码: #include <bits/stdc++.h> usi ...

  4. MySQL学习(五)——使用JDBC完成用户表CRUD的操作

    通过案例我们发现“获得连接”和“释放资源”两次代码将在之后的增删改查所有功能中都存在,开发中遇到此种情况,将采用工具类的方法进行抽取,从而达到代码的重复利用. 1.使用properties配置文件 开 ...

  5. rman备份简介

    登陆rman: [oracle@oracle ~]$ rman target / connected to target database: FSDB (DBID=1179347208) 执行全备: ...

  6. mongodb报错:connection refused because too many open connections: 819

    问题: 发现mongodb无法连接,查看mongodb日志,出现大量的如下报错: [initandlisten] connection refused because too many open co ...

  7. Python ftplib 模块关于 ftp的下载

    import ftplib import os import socket import sys HOST='192.168.216.193' DIRN='c:\\ftp\FTP.123' FILE= ...

  8. tar 命令man说明

    TAR(1) User Commands TAR(1) NAME tar - manual page for tar 1.26 SYNOPSIS tar [OPTION...] [FILE]... D ...

  9. Pimple相关的源码

    已经有了非常好的Pimple的相关解析,建议先看下:Pimple - 一个简单的 PHP 依赖注入容器读 PHP - Pimple 源码笔记(上)读 PHP - Pimple 源码笔记(下) 这里通过 ...

  10. hadoop-10-创建yum资源库

    hadoop-10-创建yum资源库 1,在/etc/yum.repos.d/下面创建 ambari.repo  HDP.repo  HDP-UTILS.repo 三个文件: [root@server ...