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) ...
随机推荐
- hdu_2669 Romantic(扩展欧几里得)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669 Romantic Time Limit: 2000/1000 MS (Java/Others) ...
- 在windows下详解:大端对齐和小端对齐
计算机的内存最小单位是什么?是BYTE,是字节.一个大于BYTE的数据类型在内存中存放的时候要有先后顺序. 高内存地址放整数的高位,低内存地址放整数的低位,这种方式叫倒着放,术语叫小端对齐.电脑X86 ...
- 互联网App应用程序测试流程及测试总结
互联网App应用程序测试流程及测试总结 1. APP测试基本流程 1.1流程图 仍然为测试环境 Pass 1.2测试周期 测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日 ...
- python 元组学习
元组用tuple表示,用,分割开,和列表类似,但是没有排序修改等高级操作.简单地说就是终态的...... >>> tuple1 = (1,2,3)>>> type( ...
- 【JDBC】Java 连接 MySQL 基本过程以及封装数据库工具类
一. 常用的JDBC API 1. DriverManager类 : 数据库管理类,用于管理一组JDBC驱动程序的基本服务.应用程序和数据库之间可以通过此类建立连接.常用的静态方法如下 static ...
- CSS深入理解学习笔记之margin
1.margin与容器尺寸 元素尺寸:①可视尺寸 clientWidth(标准):②占据尺寸 margin与可视尺寸:①适用于没有设定width/height的普通block元素:②只适用于水平方向尺 ...
- 使用非java代码编程
使用非JAVA代码 JAVA语言及其标准API(应用程序编程接口)应付应用程序的编写已绰绰有余.但在某些情况下,还是必须使用非JAVA编码.例如,我们有时要访问操作系统的专用特性,与特殊的硬件 ...
- XML,HTML,XHTML
对于上面3种技术,我们经常使用到,这里具体的做一个总结,来对比一下这3个东西. 什么是XML? XML即Extentsible Markup Language(可扩展标记语言),是用来定义其它语言的一 ...
- Struts2.3.34+Hibernate 4.x+Spring4.x 整合二部曲之下部曲
1 导入jar包 本文最后会给出项目的地址,各位无须看急. 2 配置web.xml文件 <?xml version="1.0" encoding="UTF-8&qu ...
- awk 字符串处理函数
awk提供了许多强大的字符串函数,见下表:awk内置字符串函数gsub(r,s) 在整个$0中用s替代rgsub(r,s,t) 在整个t中用s替代rindex(s,t) 返回s中字符 ...