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

//author Eterna
#define Hello the_cruel_world!
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<cmath>
#include<climits>
#include<deque>
#include<functional>
#include<complex>
#include<numeric>
#include<unordered_map>
#define Pi acos(-1.0)
#define ABS(x) ((x) >= 0 ? (x) : (-(x)))
#define pb(x) push_back(x)
#define lowbit(x) (x & -x)
#define FRIN freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\in.txt", "r", stdin)
#define FROUT freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\out.txt", "w", stdout)
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define outd(x) printf("%d\n", x)
#define outld(x) printf("%lld\n", x)
#define il inline
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int maxn = 4e4;
const int maxm = 2e2;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const double eps = 1e-;
inline int read_int() {
char c;
int ret = , sgn = ;
do { c = getchar(); } while ((c < '' || c > '') && c != '-');
if (c == '-') sgn = -; else ret = c - '';
while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
return sgn * ret;
}
inline ll read_ll() {
char c;
ll ret = , sgn = ;
do { c = getchar(); } while ((c < '' || c > '') && c != '-');
if (c == '-') sgn = -; else ret = c - '';
while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
return sgn * ret;
}
int res[maxm + ][maxm + ], sum[maxm + ][maxn + ], cnt[maxn + ];
int block, arr[maxn + ], ID[maxn + ];
int n, q, last;
int m, pos[maxn + ];
void pre_init() {
for (int i = ; i <= n; ++i)++sum[ID[i]][arr[i]];
for (int i = ; i <= m; ++i)for (int j = ; j <= ID[n]; ++j)sum[j][i] += sum[j - ][i];
for (int i = ; i <= ID[n]; ++i) {
memset(cnt, , sizeof(cnt));
for (int j = (i - ) * block + ; j <= n; ++j) {
int p = arr[j], k = res[i][ID[j - ]];
++cnt[p];
if (cnt[p] > cnt[k])res[i][ID[j]] = p;
else if (cnt[p] == cnt[k] && p < k)res[i][ID[j]] = p;
else res[i][ID[j]] = k;
}
}
memset(cnt, , sizeof(cnt));
}
int Query(int L, int R) {
int ans = res[ID[L] + ][ID[R] - ], tot = max(, sum[ID[R] - ][ans] - sum[ID[L]][ans]);
for (int i = L; i <= min(ID[L] * block, R); ++i) {
int p = arr[i];
++cnt[p];
int s = cnt[p] + max(, (sum[ID[R] - ][p] - sum[ID[L]][p]));
if (s > tot) ans = p, tot = s;
else if (s == tot && p < ans)ans = p;
}
if (ID[L] != ID[R]) {
for (int i = (ID[R] - )*block + ; i <= R; ++i) {
int p = arr[i];
++cnt[p];
int s = cnt[p] + max(, (sum[ID[R] - ][p] - sum[ID[L]][p]));
if (s > tot) ans = p, tot = s;
else if (s == tot && p < ans)ans = p;
}
}
for (int i = L; i <= min(ID[L] * block, R); ++i)--cnt[arr[i]];
if (ID[L] != ID[R])for (int i = (ID[R] - ) * block + ; i <= R; ++i)--cnt[arr[i]];
return ans;
}
int main()
{
n = read_int(), q = read_int();
block = ceil(sqrt(n));
for (int i = ; i <= n; ++i) pos[i] = arr[i] = read_int(), ID[i] = (i - ) / block + ;
sort(pos + , pos + + n);
m = unique(pos + , pos + + n) - pos - ;
for (int i = ; i <= n; ++i)arr[i] = lower_bound(pos + , pos + + m, arr[i]) - pos;
pre_init();
for (int i = ; i <= q; ++i) {
int l = read_int(), r = read_int();
l = (l + last - ) % n + , r = (r + last - ) % n + ;
if (l > r)swap(l, r);
last = pos[Query(l, r)];
outd(last);
}
return ;
}

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

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

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

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

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

  3. 洛谷 P4168 [Violet] 蒲公英

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

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

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

  5. [洛谷P4168][Violet]蒲公英

    题目大意:有$n(n\leqslant4\times10^4)$个数,$m(m\leqslant5\times10^4)$个询问,每次问区间$[l,r]$内的众数,若相同输出最小的,强制在线. 题解: ...

  6. luogu P4168 [Violet]蒲公英

    嘟嘟嘟 分块经典题竟然是一道黑题…… 分块求区间众数的大体思想是对于询问区间[L, R],预处理出这中间的整块的众数,然后统计两边零散的数在[L, R]中出现的次数,最后取出现次数最多且最小的数. 因 ...

  7. Luogu P4168 [Violet]蒲公英 分块

    这道题算是好好写了.写了三种方法. 有一个好像是$qwq$$N\sqrt(N)$的方法,,但是恳请大佬们帮我看看为什么这么慢$qwq$(后面的第三种) 注:$pos[i]$表示$i$属于第$pos[i ...

  8. 「分块系列」「洛谷P4168 [Violet]」蒲公英 解题报告

    蒲公英 Description 我们把所有的蒲公英看成一个长度为\(n\)的序列(\(a_1,a_2,...a_n\)),其中\(a_i\)为一个正整数,表示第i棵蒲公英的种类的编号. 每次询问一个区 ...

  9. 【洛谷 P4168】[Violet]蒲公英(分块)

    题目链接 题目大意:给定\(n\)个数和\(m\)个求区间众数的询问,强制在线 这题我\(debug\)了整整一个下午啊..-_- 从14:30~16:45终于\(debug\)出来了,\(debug ...

随机推荐

  1. DevExpress之TreeList控件用作导航使用说明

    最近项目用的是DEV界面框架, 由于各控件属性太多,以免遗忘.所以做个笔录.也方便有这方面需求的网友交流学习.下面开始讲解具体实现步骤. 一.先布局,设置相关属性. 1.首先到工具箱拖一个 TreeL ...

  2. 入门html第一次copy小米首页布局

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  3. linux链接及文件互相上传下载

    若排版紊乱可查看我的个人博客原文地址 基本操作 本篇博客主要介绍如何去链接远程的linux主机及如何实现本地与远程主机之间文件的上传下载操作,下面的linux系统是CentOS6.6 链接远程linu ...

  4. Linux下挂载iso文件和配置yum本地源

    Linux的版本: [root@pbn ~]# head -n 1 /etc/issueRed Hat Enterprise Linux Server release 6.1 (Santiago) 1 ...

  5. (转)ArcGIS Runtime for Android 使用异步GP服务绘制等值线

    关于基于Android上ArcGIS Server GP服务的调用,已经有前辈给出了很好的例子: http://blog.csdn.net/esrichinacd/article/details/92 ...

  6. 前端开发【第三篇: JavaScript基础】

    JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.如何编写 1.J ...

  7. 【转】Android-Input 键盘设备

    https://source.android.com/devices/input/keyboard-devices 键盘设备 Android 支持各种键盘设备,包括特殊功能小键盘(音量和电源控制),紧 ...

  8. python3 opencv3 实现基本的人脸检测、识别功能

    一言不和,先上码子(纯新手,莫嘲笑) # encoding: utf-8 #老杨的猫,环境:PYCHARM,python3.6,opencv3 import cv2,os import cv2.fac ...

  9. git怎么fork一个仓库并pull request

    一.使用git push <-----------就是这个玩意 1.设置用户信息 当安装完 Git 应该做的第一件事就是设置你的用户名称与邮件地址. 这样做很重要,因为每一个 Git 的提交都会 ...

  10. input.nextLine()的注意事项

    若在input.nextInt()和input.nextDouble()后使用input.nextLine(),要先加一个input.nextLine()进行换行