BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]
题意:
询问区间权值在$[a,b]$范围内种类数和个数
莫队
权值分块维护种类数和个数$O(1)-O(\sqrt{N})$
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pii pair<int, int>
#define MP make_pair
#define fir first
#define sec second
typedef long long ll;
const int N=1e5+, M=1e6+, BN=, BS=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
} int n, Q, a[N], l, r, x, y;
struct _blo{int l, r;} b[BN];
int block, m, pos[N];
struct Block{
inline void ini(){
block=sqrt( n );
m=(n-)/block+;
for(int i=; i<=n; i++) pos[i]=(i-)/block+;
for(int i=; i<=m; i++) b[i].l=(i-)*block+, b[i].r=i*block; b[m].r=n;
}
int kind[BS], c[N], sum[BS];
inline void add(int v) {
c[v]++; sum[pos[v]]++;
if(c[v]==) kind[pos[v]]++;
}
inline void del(int v) {
c[v]--; sum[pos[v]]--;
if(c[v]==) kind[pos[v]]--;
}
pii que(int l,int r) {
int pl=pos[l], pr=pos[r], ans1=, ans2=;
if(pl==pr) for(int i=l; i<=r; i++) ans1+=c[i], ans2+= c[i]>;
else{
for(int i=pl+; i<pr; i++) ans1+=sum[i], ans2+= kind[i];
for(int i=l; i<=b[pl].r; i++) ans1+=c[i], ans2+= c[i]>;
for(int i=b[pr].l; i<=r; i++) ans1+=c[i], ans2+= c[i]>;
}
return MP(ans1, ans2);
}
}B; struct meow{
int l, r, x, y, qid;
bool operator <(const meow &a) const{return pos[l]==pos[a.l] ? r<a.r : pos[l]<pos[a.l];}
}q[M];
pii ans[M];
void modui(){
int l=, r=;
for(int i=; i<=Q; i++){
while(r<q[i].r) r++, B.add(a[r]);
while(r>q[i].r) B.del(a[r]), r--;
while(l<q[i].l) B.del(a[l]), l++;
while(l>q[i].l) l--, B.add(a[l]);
ans[q[i].qid]= B.que(q[i].x, q[i].y);
}
} int main() {
freopen("in","r",stdin);
n=read(); Q=read();
for(int i=; i<=n; i++) a[i]=read();
for(int i=; i<=Q; i++) l=read(), r=read(), x=read(), y=read(), q[i]=(meow){l, r, x, y, i};
B.ini();
sort(q+, q++Q);
modui();
for(int i=; i<=Q; i++) printf("%d %d\n",ans[i].fir, ans[i].sec);
}
BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]的更多相关文章
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
- bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...
- bzoj 3809 Gty的二逼妹子序列——莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...
随机推荐
- python内建函数isinstance基础用法
语法:isinstance(object,type) 作用:来判断一个对象是否是一个已知的类型. 其第一个参数(object)为对象,第二个参数(type)为类型名(int...)或类型名的一个 ...
- android组件化方案、二维码扫码、Kotlin新闻客户端、动画特效等源码
Android精选源码 CalendarView日历选择器 android下拉刷新动画效果代码 一个非常方便的fragment页面框架 android组件化方案源码 Zxing实现二维码条形码的扫描和 ...
- POI读写大数据量excel,解决超过几万行而导致内存溢出的问题
1. Excel2003与Excel2007 两个版本的最大行数和列数不同,2003版最大行数是65536行,最大列数是256列,2007版及以后的版本最大行数是1048576行,最大列数是16384 ...
- UEP-时间
时间戳转化为Date(or String) SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ...
- 【转自知乎】:localhost、127.0.0.1 和 本机IP 三者的区别?
作者:知乎用户链接:https://www.zhihu.com/question/23940717/answer/26230963来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- dede后台出现 保存目录数据时失败,请检查你的输入资料是否存在问题
dede 5.7无法增加顶级/二级栏目,保存目录数据时失败,请检查你的输入资料是否存在问题!执行了SQL还是不行 解决档案:用正常可以添加栏目的,将E:\wamp\www\dededln\back(d ...
- ip001
----------- <?phpheader('Content-type:text/html;charset=utf8');// <script type="text/java ...
- 我的java学习之路--Reflect专题
学习网址:http://www.imooc.com/video/3725 1.Class类的使用 class类 在面向对象的世界里,万事万物皆对象 java语言中,静态的成员.普通数据类型类不是对象. ...
- [转]CentOS Apache 性能调试!
查看Apache的并发请求数及其TCP连接状态: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 返回结果示例 ...
- JAVA中创建线程的三种方法及比较
JAVA中创建线程的方式有三种,各有优缺点,具体如下: 一.继承Thread类来创建线程 1.创建一个任务类,继承Thread线程类,因为Thread类已经实现了Runnable接口,然后重写run( ...