[BZOJ3920]Yuuna的礼物
题目大意:
给你一个长度为$n(n\le40000)$的数列$\{a_i\}(1\le a_i\le n)$,给出$m(m\le40000)$次询问,每次给出$l,r,k_1,k_2$询问区间$[l,r]$中出现次数第$k_1$小的数中第$k_2$小的数是多少?
思路:
运用莫队算法离线处理所有询问,分块维护数列中每个数的出现次数。考虑如何维护出现次数的出现次数以及出现次数相同的数。同样采用分块,先预处理出对于某一种出现次数,所有可能的数,再将其离散化,对于离散化后的数分块维护。由于题目空间限制只有24M,因此需要手写内存池或者使用vector,可以证明离散化以后的数组空间复杂度是$O(n)$的。这样时间复杂度$O(m\sqrt n)$,空间复杂度是$O(n+m)$。
#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,M=;
int n,a[N],b[N],block,ans[M],cnt[N];
struct Query {
int l,r,k1,k2,id;
bool operator < (const Query &another) const {
return l/block<another.l/block||(l/block==another.l/block&&r<another.r);
}
};
Query q[M];
std::vector<int> v[N],rank[N],cnt1,sum1,cnt2[N],sum2[N];
inline void modify(const int &x,const int &p,const int &d) {
cnt2[x][p]+=d;
sum2[x][p/block]+=d;
if(d==&&!cnt1[x]++) sum1[x/block]++;
if(d==-&&!--cnt1[x]) sum1[x/block]--;
}
inline void modify(const int &x,const int &d) {
if(cnt[x]) modify(cnt[x],rank[x][cnt[x]],-);
if(cnt[x]+=d) modify(cnt[x],rank[x][cnt[x]],);
}
inline int solve(const std::vector<int> &c,const std::vector<int> &s,const int &k) {
register int x=,cnt=;
while((cnt+s[x])<k) cnt+=s[x++];
for(register int i=x*block;i<(x+)*block;i++) {
if((cnt+=!!c[i])>=k) return i;
}
}
inline int query(const int &k1,const int &k2) {
const int x=solve(cnt1,sum1,k1);
return v[x][solve(cnt2[x],sum2[x],k2)];
}
int main() {
block=sqrt(n=getint());
for(register int i=;i<=n;i++) {
cnt[a[i]=b[i]=getint()]++;
}
std::sort(&b[],&b[n]+);
for(register int i=;i<=n;i++) {
if(b[i]==b[i-]) continue;
rank[b[i]].resize(cnt[b[i]]+);
for(register int j=;j<=cnt[b[i]];j++) {
rank[b[i]][j]=v[j].size();
v[j].push_back(b[i]);
}
cnt[b[i]]=;
}
for(register int i=;i<=n;i++) {
cnt2[i].resize(v[i].size()+);
sum2[i].resize(cnt2[i].size()/block+);
}
const int m=getint();
for(register int i=;i<m;i++) {
const int l=getint(),r=getint(),k1=getint(),k2=getint();
q[i]=(Query){l,r,k1,k2,i};
}
cnt1.resize(n+);
sum1.resize(n/block+);
std::sort(&q[],&q[m]);
for(register int i=,l=,r=;i<m;i++) {
while(r<q[i].r) modify(a[++r],);
while(l>q[i].l) modify(a[--l],);
while(r>q[i].r) modify(a[r--],-);
while(l<q[i].l) modify(a[l++],-);
ans[q[i].id]=query(q[i].k1,q[i].k2);
}
for(register int i=;i<m;i++) {
printf("%d\n",ans[i]);
}
return ;
}
[BZOJ3920]Yuuna的礼物的更多相关文章
- bzoj3920: Yuuna的礼物(莫队+分块套分块)
思路挺简单的,但是总感觉好难写...码力还是差劲,最后写出来也挺丑的 这题显然是个莫队题,考虑怎么转移和询问... 根据莫队修改多查询少的特点,一般用修改快查询慢的分块来维护.查第$k_1$小的出现次 ...
- 【莫队算法】【权值分块】bzoj3920 Yuuna的礼物
[算法一] 暴力. 可以通过第0.1号测试点. 预计得分:20分. [算法二] 经典问题:区间众数,数据范围也不是很大,因此我们可以: ①分块,离散化,预处理出: <1>前i块中x出现的次 ...
- bzoj 3920: Yuuna的礼物
Description 转眼就要到Karin的生日了!Yuuna她们想为她准备生日礼物!现在有许多礼物被排列成了一个一维序列,每个礼物都有一个价值.Yuuna对这个序列十分感兴趣.因此,你需要多次回答 ...
- BZOJ.3920.Yuuna的礼物(莫队 分块套分块 分段离散化)
题目链接 详细题解:https://www.cnblogs.com/autsky-jadek/p/4376091.html 代码参考自:https://www.cnblogs.com/Sakits/p ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- jQuery动画-圣诞节礼物
▓▓▓▓▓▓ 大致介绍 下午看到了一个送圣诞礼物的小动画,正好要快到圣诞节了,就动手模仿并改进了一些小问题 原地址:花式轮播----圣诞礼物传送 思路:动画中一共有五个礼物,他们平均分布在屏幕中,设置 ...
- bzoj 3055礼物运送 floyed + 状压DP
bzoj 3055: 礼物运送 floyed first 设f[i][S]表示取到了S集合中的所有点(不一定是经过的所有点),最后停在了i的最优值. 初始就f[i][{i}] = dis[1][i] ...
- Typecast 免费了!献给设计师们的礼物
TypeCast 让你可以从 Fonts.com.TypeKit.FontDeck 和 Google 这些字体供应和商选择字体,而且能非常方便的比较这些字体使用效果.如果你想获得用户对这些字体效果的反 ...
- 【HDU 5835】Danganronpa(分配礼物)
10种礼物,每种有ai个,每个小朋友分两个礼物,其中普通礼物要求相邻两人的不能一样,求最多分给几个小朋友. sum/2是最多的情况.什么时候发不了那么多,就是当max很大,无论怎么发,都发不完max. ...
随机推荐
- Python全栈工程师(集合、函数)
ParisGabriel 感谢 大家的支持 你们的阅读评价就是我最好的动力 我会坚持把排版内容以及偶尔的错误做的越来越好 每天坚持 一天一篇 点个订阅吧 灰常感谢 ...
- 1004 Counting Leaves (30 分)(树的遍历)
给出一棵树,问每一层各有多少叶子节点 dfs遍历树 #include<bits/stdc++.h> using namespace std; vector<]; int n,m; i ...
- Hadoop平台K-Means聚类算法分布式实现+MapReduce通俗讲解
Hadoop平台K-Means聚类算法分布式实现+MapReduce通俗讲解 在Hadoop分布式环境下实现K-Means聚类算法的伪代码如下: 输入:参数0--存储样本数据的文本文件inpu ...
- 用archlinux作为日常开发机的感受
机器配置 CPU: Intel Core i5-6200U RAM: 8G Resolution: 1920x1080 我在arch下常用的软件 图形桌面环境 i3wm wifi无线管理 Networ ...
- Learn the shell
learn the shell what is the shell? when we speak of the command line,we are really to the shell.Actu ...
- 第1张 Maven简介 学习笔记
什么是构建? 编译.运行单元测试.生成文档.打包和部署 Maven的应用: 构建工具 依赖管理工具 通过坐标系统定位到每一个构建(artifact) 项目信息管理工具 Maven对于项目目录结构.测试 ...
- 新浪微博 page应用 自适应高度设定 终于找到解决方法
我做的是PAGE应用,无法自适应高度.找了好久解决方法. 用js 设置父窗口 iframe 也不好用,有的浏览器不兼容. 官方上说发是这样的: 应用动态高度自适应 Iframe高度:开发者可以使Ifr ...
- [bzoj5287] [HNOI2018]毒瘤
题目描述 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和.毒瘤考虑了n ...
- [bzoj4945][Noi2017]游戏
题目大意:有$n$个位置,有三种数,每个位置只可以填一种数,$d(d\leqslant8)$个位置有三种选择,其他位置只有两种选择.有一些限制,表示第$i$个位置选了某种数,那么第$j$个位置就只能选 ...
- [bzoj4361] isn [树状数组+dp+容斥原理]
题面 传送门 思路 首先,本题目的核心元素是非降子序列,而显然这个题目中的子序列只和序列的长度.位置,以及互相之间的包含关系,这些东西相关 所以我们可以依据这些先"猜"(实际上是估 ...