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 ...
随机推荐
- JavaScript-页面打印正方形,各种三角形与菱形
一. 正方形 a) 在第一个for循环中控制,一共输出几行.依靠的是,每次输出一行,就会在后面输出一个换行符<br>; b) 在第二个for循环中控制每行输出几个五角星.这样的 ...
- Linux、Windows 下分割、合并rar文件
1.分割rar 1.1 linux下分割压缩rar 安装rar和unrar和序 $sudo aptitude install rar unrar 示例,分割压缩temp文件,每个包为1MB $rar ...
- java 反向工具类
1.ReflectUtil.java package com.example.scansell; import android.util.Log; import java.lang.reflect.C ...
- SpringMVC和MyBatis的整合
这里我们需要用到一个关键的jar包——Spring-MyBatis,它会帮你将MyBatis代码无缝地整合到Spring中.具体可以参考http://www.mybatis.org/spring/zh ...
- 164 Maximum Gap 最大间距
给定一个无序的数组,找出数组在排序后相邻的元素之间最大的差值.尽量尝试在线性时间和空间复杂度情况下解决此问题.若数组元素个数少于2,则返回0.假定所有的元素都是非负整数且范围在32位有符号整数范围内. ...
- jQuery将json字符串显示在页面上
js代码: function syntaxHighlight(json) { if (typeof json != 'string') { json = JSON.stringify(json, un ...
- 微信小程序 图片加载失败处理方案
小程序端展示网络资源图片可能会失败,下面介绍一种自己的处理方法 1. js文件中判断图片 url 是否存在,存在则正常显示,不存在则替换url为本地默认图片 2. 当图片 url 存在,但是加载失败时 ...
- cyclic swapping algorithm
原文见:https://leetcode.com/problems/couples-holding-hands/discuss/113362/JavaC%2B%2B-O(N)-solution-usi ...
- Dreamoon and MRT
Dreamoon and MRT 题目链接: http://codeforces.com/group/gRkn7bDfsN/contest/212299/problem/B 只需要考虑相对位置,设a0 ...
- Vsphere中ESXi主机ssh开启的三种方法
ESXi 5.5是直接安装在物理主机上的一个虚拟机系统,本质上是一个Linux系统. 平时可以通过VMware Client端或者VMware vCenter进行管理,但对于一些特殊的VMware命令 ...