luogu4168 [Violet]蒲公英
#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]蒲公英的更多相关文章
- 题解【luogu4168 [Violet]蒲公英】
		Description 给出一个长度为 \(n\) 序列 \(a\) ,\(m\) 次询问,每次询问区间 \([l,r]\) 里的众数(出现次数最多的数).若有多个,输出最小的. \(a_i \leq ... 
- 洛谷 P4168 [Violet]蒲公英 解题报告
		P4168 [Violet]蒲公英 题目背景 亲爱的哥哥: 你在那个城市里面过得好吗? 我在家里面最近很开心呢.昨天晚上奶奶给我讲了那个叫「绝望」的大坏蛋的故事的说!它把人们的房子和田地搞坏,还有好多 ... 
- 【luogu1468】[Violet]蒲公英--求区间众数
		题目背景 亲爱的哥哥: 你在那个城市里面过得好吗? 我在家里面最近很开心呢.昨天晚上奶奶给我讲了那个叫「绝望」的大坏蛋的故事的说!它把人们的房子和田地搞坏,还有好多小朋友也被它杀掉了.我觉得把那么可怕 ... 
- [Violet]蒲公英   分块
		发现写算法专题老是写不动,,,, 所以就先把我在luogu上的题解搬过来吧! 题目大意:查询区间众数,无修改,强制在线 乍一看是一道恐怖的题,仔细一看发现并没有那么难: 大致思路是这样的,首先我们要充 ... 
- P4168 [Violet]蒲公英  区间众数
		$ \color{#0066ff}{ 题目描述 }$ 在乡下的小路旁种着许多蒲公英,而我们的问题正是与这些蒲公英有关. 为了简化起见,我们把所有的蒲公英看成一个长度为n的序列 \((a_1,a_2.. ... 
- 洛谷 P4168 [Violet] 蒲公英
		历尽千辛万苦终于AC了这道题目... 我们考虑1个区间\([l,r]\), 被其完整包含的块的区间为\([L,R]\) 那么众数的来源? 1.\([l,L)\)或\((R,r]\)中出现的数字 2.\ ... 
- P4168 [Violet]蒲公英
		神仙分块题?其实还是很简单的,res[i][j]表示第i块到第j块的众数,然后再用sum[i][j]表示前i块中j这个种类出现的次数,然后分块瞎搞就行了,感觉我写的十分简洁,好评( //author ... 
- BZOJ2724 [Violet]蒲公英(分块)
		区间众数.分块,预处理任意两块间所有数的众数,和每块中所有数的出现次数的前缀和.查询时对不是整块的部分暴力,显然只有这里出现的数可能更新答案.于是可以优美地做到O(n√n). #include< ... 
- p4168 [Violet]蒲公英(分块)
		区间众数的重题 和数列分块入门9双倍经验还是挺好的 然后开O2水过 好像有不带log的写法啊 之后在补就是咕咕咕 // luogu-judger-enable-o2 #include <cstd ... 
随机推荐
- [HNOI2018]毒瘤
			Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和 ... 
- 区间dp实战练习
			题解报告:poj 2955 Brackets(括号匹配) Description We give the following inductive definition of a “regular br ... 
- 转 python 将一个文件中内容添加到另一个文件指定位置
			http://blog.csdn.net/huguangshanse00/article/details/14624601 
- qconbeijing2015
			http://2015.qconbeijing.com/schedule 大会日程 2015年4月23日,星期四 地点 2号厅 203AB 201AB 9:15 开场致辞 专题 主题演讲 互联网金融背 ... 
- jmeter压测--从文本中读取参数
			由于之前从数据库获取查询结果作为请求的入参(使用场景:测试一个接口并发处理数据的能力,并且每次请求传入的参数都要不同.),会一定程度上造成对数据库的压测,在没有完全搞清楚多线程之间参数的传递之前,我们 ... 
- Hibernate5 与 Spring Boot2 最佳性能实践
			参考 Hibernate5 与 Spring Boot2 最佳性能实践(1) Hibernate5 与 Spring Boot2 最佳性能实践(2) Best Performance Practice ... 
- LeetCode 69 题
			1.题目要求 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出 ... 
- python绘图工具包 matplotlib 中文乱码问题
			环境: python2.7 windows 8.1 解决: 改配置文件,把字体改为支持中文的字体. 找到python安装目录下的 \Lib\site-packages\matplotlib\mpl-d ... 
- 解决Ueditor在bootstarp 模态框中全屏问题
			基本的一些配置就不说了.先说一下要注意的问题:首先是zIndex的设置.记住最好都显示设置模态框和ueditor的zIndex.理清他们的层叠关系. 特别是用到ueditor里面的图片上传功能的更要设 ... 
- windows系统下查看或删除自己电脑的共享文件以及文件夹
			(1)查看所有共享 net share (2)删除指定共享 例如:删除C盘共享 net share C$ /delete net share 共享名 /delete (/del) 
