BZOJ.3809.Gty的二逼妹子序列(分块 莫队)
/*
25832 kb 26964 ms
莫队+树状数组:增加/删除/查询 都是O(logn)的,总时间复杂度O(m*sqrt(n)*logn),卡不过
莫队+分块:这样查询虽然变成了sqrt(n),但是修改是O(1)的
考虑对权值进行分块
细节...
*/
#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
const int N=1e5+5,M=1e6+5;
int n,m,A[N],size,Ans[M],belong[N],times[N],bloans[N];
struct Ques
{
int l,r,a,b,id;
bool operator <(const Ques &x)const
{
return belong[l]==belong[x.l] ? r<x.r : l<x.l;
}
}q[M];
inline int read()
{
int now=0,f=1;register char c=getchar();
for(;!isdigit(c);c=getchar())
if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=getchar());
return now*f;
}
int Query(int l,int r)
{
int res=0,t=min(r,belong[l]*size);
for(int i=l;i<=t;++i)
if(times[i]) ++res;
if(belong[l]!=belong[r])
for(int i=(belong[r]-1)*size+1;i<=r;++i)
if(times[i]) ++res;
for(int i=belong[l]+1;i<belong[r];++i)
res+=bloans[i];
return res;
}
inline void Add(int p)
{
if(!times[p]) ++bloans[belong[p]];
++times[p];
}
inline void Subd(int p)
{
--times[p];
if(!times[p]) --bloans[belong[p]];
}
int main()
{
n=read(),m=read();size=sqrt(n);
for(int i=1;i<=n;++i)
A[i]=read(), belong[i]=(i-1)/size+1;
for(int i=1;i<=m;++i)
q[i].l=read(), q[i].r=read(), q[i].a=read(), q[i].b=read(), q[i].id=i;
sort(q+1,q+1+m);
for(int i=1,l=1,r=0;i<=m;++i)
{
while(l<q[i].l) Subd(A[l++]);
while(l>q[i].l) Add(A[--l]);
while(r<q[i].r) Add(A[++r]);
while(r>q[i].r) Subd(A[r--]);
Ans[q[i].id]=Query(q[i].a,q[i].b);
}
for(int i=1;i<=m;++i)
printf("%d\n",Ans[i]);
return 0;
}
2019.11.14:
//2.04s 25.05MB 没以前跑得快...自闭了
#include <cmath>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
//#define MAXIN 500000
//#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e5+5,M=1e6+5;
int size,bel[N],val[N],bloans[N],times[N],Ans[M];
//char IN[MAXIN],*SS=IN,*TT=IN;
struct Queries
{
int l,r,a,b,id;
bool operator <(const Queries &x)const
{
return bel[l]==bel[x.l]?r<x.r:l<x.l;
}
}q[M];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
}
inline void Add(int x)
{
!times[x]&&(++bloans[bel[x]]), ++times[x];
}
inline void Subd(int x)
{
--times[x], !times[x]&&(--bloans[bel[x]]);
}
inline int Query(int l,int r)
{
int res=0;
for(int i=bel[l]+1; i<bel[r]; ++i) res+=bloans[i];
for(int i=l,t=std::min(bel[l]*size,r); i<=t; ++i) res+=(times[i]!=0);
if(bel[l]!=bel[r]) for(int i=(bel[r]-1)*size+1; i<=r; ++i) res+=(times[i]!=0);
return res;
}
int main()
{
const int n=read(),m=read(),size=sqrt(n); ::size=size;
for(int i=1; i<=n; ++i) val[i]=read(), bel[i]=(i-1)/size+1;
for(int i=1; i<=m; ++i) q[i]=(Queries){read(),read(),read(),read(),i};
std::sort(q+1,q+1+m);
for(int i=1,l=1,r=0; i<=m; ++i)
{
int ln=q[i].l,rn=q[i].r;
while(l>ln) Add(val[--l]);
while(r<rn) Add(val[++r]);
while(l<ln) Subd(val[l++]);
while(r>rn) Subd(val[r--]);
Ans[q[i].id]=Query(q[i].a,q[i].b);
}
for(int i=1; i<=m; printf("%d\n",Ans[i++]));
return 0;
}
BZOJ.3809.Gty的二逼妹子序列(分块 莫队)的更多相关文章
- BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...
- bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)
[题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...
- 【BZOJ 3809】 3809: Gty的二逼妹子序列 (莫队+分块)
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1728 Solved: 513 Description Autumn ...
- 【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1072 Solved: 292[Submit][Status][Di ...
- 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 ...
- [bzoj3809]Gty的二逼妹子序列_莫队_分块
Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
随机推荐
- k64 datasheet学习笔记3---Chip Configuration之Clock modules
1.前言 本文主要讲述chip configure之clock配置相关的内容,主要包含如下三个部分: MCG configuration:Multipurpose clock generator OS ...
- SHA1算法原理
一.SHA1与MD5差异 SHA1对任意长度明文的预处理和MD5的过程是一样的,即预处理完后的明文长度是512位的整数倍,但是有一点不同,那就是SHA1的原始报文长度不能超过2的64次方,然后SHA1 ...
- Linux内存管理--物理内存分配【转】
转自:http://blog.csdn.net/myarrow/article/details/8682819 1. First Fit分配器 First Fit分配器是最基本的内存分配器,它使用bi ...
- oracle forall
select * bulk collect into r_115 from TZTJ_CL0115 where nf = v_nf and yf = v_yf and ...
- Python-百度经纬度转高德经纬度
import math def bdToGaoDe(lon,lat): """ 百度坐标转高德坐标 :param lon: :param lat: :return: &q ...
- CentOS 6.5使用Corosync + pacemaker实现httpd服务的高可用
Corosync:它属于OpenAIS(开放式应用接口规范)中的一个项目corosync一版本中本身不具备投票功能,到了corosync 2.0之后引入了votequorum子系统也具备了投票功能了, ...
- LVS负载均衡器DR模型的实现
概述: 在大规模互联网应用中,负载均衡设备是必不可少的一个节点,源于互联网应用的高并发和大流量的冲击压力,我们通常会在服务端部署多个无状态的应用服务器和若干有状态的存储服务器(数据库.缓存等等) 一. ...
- Ex 6_5棋子放置问题_第八次作业
题目貌似有问题 (b) 子问题定义: 设maxValue[i][j]为棋盘的前i行中最后一行为i时第i行按照第j种放置方式放置时得到的最大覆盖值,comp[i][j]为第i种放置方式与第j种放置方式是 ...
- Lodash JavaScript 实用工具库
地址:https://www.lodashjs.com/ Lodash 是一个一致性.模块化.高性能的 JavaScript 实用工具库.
- 卓越的目标检测器Pelee
Densenet的改良—PeleeNET Pelee: A Real-Time Object Detection System on Mobile Devices 论文地址:https://arxiv ...