luogu4168 [Violet]蒲公英
2024-08-30 14:06:26
#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;
}
最新文章
- node fs lstat 如何区别文件和文件夹
- linux命令格式及基础命令(一)
- CentOS 7下源码安装MySQL 5.6
- 【zepto学习笔记02】零碎点
- Decimal To Fraction 小数转换成分数
- Code(组合数学)
- :Hibernate逍遥游记-第16管理session和实现对话
- mysql计算连续天数,mysql连续登录天数,连续天数统计
- Print! Print! Print!
- arcpy.mapping常用四大件-MapsurroundElement
- JAVA常用集合源码解析系列-ArrayList源码解析(基于JDK8)
- iOS 可高度自定义的底部弹框
- 使用docker redis-cluster集群搭建
- java script入门之知识
- .NET跨平台实践:再谈用C#开发Linux守护进程 — 完整篇
- PowerBuilder编程新思维5:包装(界面美化与WebUI+React)
- JS参差不齐的数组
- 【转】Ubuntu12.04 LTS下环境变量设置
- 使用base64转码的方式上传图片
- Spring 工作流程简单介绍
热门文章
- git导出代码
- hibernate Day2
- HttpURLConnection 发送PUT请求 json请求体 与服务端接收
- 基于python的request库,模拟登录csdn博客
- C. Arcade dp二维费用背包 + 滚动数组 玄学
- Ionic开发-如何在ion-content形成上下结构 上面固定下层可滚动
- Docker容器相关技术
- 移除sql数据所有链接用户
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
- JavaScript 的垃圾回收与内存泄露