#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
int n, m, bel[40005], a[40005], blc, f[1005][1005], cnt[40005], idx, ans;
int val[40005], uu, vv;
map<int,int> mp;
vector<int> vec[40005];
int mycnt(int uu, int vv, int ww){
return upper_bound(vec[ww].begin(), vec[ww].end(), vv)-lower_bound(vec[ww].begin(), vec[ww].end(), uu);
}
int query(int uu, int vv){
int tmpmax=f[bel[uu]+1][bel[vv]-1], tmpcnt=mycnt(uu, vv, tmpmax);
if(bel[uu]==bel[vv]){
for(int i=uu; i<=vv; i++){
int t=mycnt(uu, vv, a[i]);
if(t>tmpcnt || (t==tmpcnt && a[i]<tmpmax)){
tmpmax = a[i];
tmpcnt = t;
}
}
}
else{
for(int i=uu; i<=bel[uu]*blc; i++){
int t=mycnt(uu, vv, a[i]);
if(t>tmpcnt || (t==tmpcnt && a[i]<tmpmax)){
tmpmax = a[i];
tmpcnt = t;
}
}
for(int i=(bel[vv]-1)*blc+1; i<=vv; i++){
int t=mycnt(uu, vv, a[i]);
if(t>tmpcnt || (t==tmpcnt && a[i]<tmpmax)){
tmpmax = a[i];
tmpcnt = t;
}
}
}
return tmpmax;
}
int main(){
cin>>n>>m;
blc = sqrt(n*log(2)/log(n));
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
val[++idx] = a[i];
bel[i] = (i - 1) / blc + 1;
}
sort(val+1, val+1+n);
idx = unique(val+1, val+1+idx) - val - 1;
for(int i=1; i<=n; i++){
a[i] = lower_bound(val+1, val+1+idx, a[i]) - val;
vec[a[i]].push_back(i);
}
for(int i=1; i<=n; i=bel[i]*blc+1){
memset(cnt, 0, sizeof(cnt));
int tmpmax=0, tmpcnt=0;
for(int j=i; j<=n; j++){
cnt[a[j]]++;
if(cnt[a[j]]>tmpcnt || (cnt[a[j]]==tmpcnt && a[j]<tmpmax)){
tmpmax = a[j];
tmpcnt = cnt[a[j]];
}
f[bel[i]][bel[j]] = tmpmax;
}
}
while(m--){
scanf("%d %d", &uu, &vv);
uu = (uu + ans - 1) % n + 1;
vv = (vv + ans - 1) % n + 1;
if(uu>vv) swap(uu, vv);
ans = val[query(uu, vv)];
printf("%d\n", ans);
}
return 0;
}

luogu4168 [Violet]蒲公英的更多相关文章

  1. 题解【luogu4168 [Violet]蒲公英】

    Description 给出一个长度为 \(n\) 序列 \(a\) ,\(m\) 次询问,每次询问区间 \([l,r]\) 里的众数(出现次数最多的数).若有多个,输出最小的. \(a_i \leq ...

  2. 洛谷 P4168 [Violet]蒲公英 解题报告

    P4168 [Violet]蒲公英 题目背景 亲爱的哥哥: 你在那个城市里面过得好吗? 我在家里面最近很开心呢.昨天晚上奶奶给我讲了那个叫「绝望」的大坏蛋的故事的说!它把人们的房子和田地搞坏,还有好多 ...

  3. 【luogu1468】[Violet]蒲公英--求区间众数

    题目背景 亲爱的哥哥: 你在那个城市里面过得好吗? 我在家里面最近很开心呢.昨天晚上奶奶给我讲了那个叫「绝望」的大坏蛋的故事的说!它把人们的房子和田地搞坏,还有好多小朋友也被它杀掉了.我觉得把那么可怕 ...

  4. [Violet]蒲公英 分块

    发现写算法专题老是写不动,,,, 所以就先把我在luogu上的题解搬过来吧! 题目大意:查询区间众数,无修改,强制在线 乍一看是一道恐怖的题,仔细一看发现并没有那么难: 大致思路是这样的,首先我们要充 ...

  5. P4168 [Violet]蒲公英 区间众数

    $ \color{#0066ff}{ 题目描述 }$ 在乡下的小路旁种着许多蒲公英,而我们的问题正是与这些蒲公英有关. 为了简化起见,我们把所有的蒲公英看成一个长度为n的序列 \((a_1,a_2.. ...

  6. 洛谷 P4168 [Violet] 蒲公英

    历尽千辛万苦终于AC了这道题目... 我们考虑1个区间\([l,r]\), 被其完整包含的块的区间为\([L,R]\) 那么众数的来源? 1.\([l,L)\)或\((R,r]\)中出现的数字 2.\ ...

  7. P4168 [Violet]蒲公英

    神仙分块题?其实还是很简单的,res[i][j]表示第i块到第j块的众数,然后再用sum[i][j]表示前i块中j这个种类出现的次数,然后分块瞎搞就行了,感觉我写的十分简洁,好评( //author ...

  8. BZOJ2724 [Violet]蒲公英(分块)

    区间众数.分块,预处理任意两块间所有数的众数,和每块中所有数的出现次数的前缀和.查询时对不是整块的部分暴力,显然只有这里出现的数可能更新答案.于是可以优美地做到O(n√n). #include< ...

  9. p4168 [Violet]蒲公英(分块)

    区间众数的重题 和数列分块入门9双倍经验还是挺好的 然后开O2水过 好像有不带log的写法啊 之后在补就是咕咕咕 // luogu-judger-enable-o2 #include <cstd ...

随机推荐

  1. Python函数和面向对象题目

  2. Wolfycz的娱乐赛题解

    现在不会放题解的!比赛完了我会把题解放上来的 祝大家玩的愉快~ 等会,cnblogs不会显示更新时间?我禁赛我自己 UPD:2018.12.15 欢迎大家爆踩标程- painting 我们考虑转化题意 ...

  3. 关于html/css的路径问题

    非原创,转自:http://blog.sina.com.cn/s/blog_6c21f6480101cb33.html [问题描述]: 比如你有Web项目solo,假如目录结构如下: 在cy.css中 ...

  4. spark Listener和metrics实现分析

    在spark内部,rpc可以用来实现不同组件(Driver, executor,client)之间的远程交互.而在同一组件内,spark还有事件监听机制,如spark中各种指标的采集主要就是通过事件监 ...

  5. Redis java操作客服端——jedis

    1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.happygo-content-Service工程中. 1.1. 连 ...

  6. markdown快捷键(转)

    markdown快捷键 GLYPH NAME COMMAND Command, Cmd, Clover, (formerly) Apple ALT Option, Opt, (Windows) Alt ...

  7. jQuery选择器之属性筛选选择器

    在这么多属性选择器中[attr="value"]和[attr*="value"]是最实用的 [attr="value"]能帮我们定位不同类型 ...

  8. canvas基础绘制-绚丽倒计时

    效果图: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  9. [Tunny]Git常用命令与入门

    [黄映焜/Tunny,20140709] Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而实现文档的版本控制..g ...

  10. IOStableviewsectionSet

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {     if (t ...