题目描述

  给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值。

  \(n\leq 100000,m\leq 1000000\)

题外话

  Q:这道题和BZOJ3809有什么区别呢?

  A:卡空间。

题解

  考虑莫队。

  每次转移时如果用树状数组很明显会TLE。所以要分块。

  每\(\sqrt n\)个数分一块。这样转移是\(O(1)\)的,查询是\(O(\sqrt n)\)的。

  时间复杂度:\(O(n\sqrt m+m\sqrt n)\)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void sort(int &a,int &b)
{
if(a>b)
swap(a,b);
}
void open(const char *s)
{
#ifdef DEBUG
char str[100];
sprintf(str,"%s.in",s);
freopen(str,"r",stdin);
sprintf(str,"%s.out",s);
freopen(str,"w",stdout);
#endif
}
int sz1,sz2;
//sz1=sqrt(n)
//sz2=n/sqrt(m)
int b1(int x)
{
return (x+sz1-1)/sz1;
}
int b2(int x)
{
return (x+sz2-1)/sz2;
}
namespace orzzjt
{
int a[100010];
int b[100010];
void change(int x,int v)
{
a[x]+=v;
b[b1(x)]+=v;
}
int query(int x,int y)
{
int s=0;
int i;
int bx=b1(x);
int by=b1(y);
if(bx==by)
{
for(i=x;i<=y;i++)
s+=a[i];
}
else
{
for(i=x;i<=bx*sz1;i++)
s+=a[i];
for(i=(by-1)*sz1+1;i<=y;i++)
s+=a[i];
for(i=bx+1;i<=by-1;i++)
s+=b[i];
}
return s;
}
}
struct ques
{
int l,r,a,b,id;
};
ques b[1000010];
int cmp(ques a,ques b)
{
if(b2(a.l)!=b2(b.l))
return b2(a.l)<b2(b.l);
return a.r<b.r;
}
int a[100010];
int ans[1000010];
int c[100010];
void add(int x)
{
c[x]++;
if(c[x]==1)
orzzjt::change(x,1);
}
void del(int x)
{
c[x]--;
if(!c[x])
orzzjt::change(x,-1);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
sz1=sqrt(n);
sz2=n/sqrt(m);
sz2=max(sz2,1);
int i;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=m;i++)
{
scanf("%d%d%d%d",&b[i].l,&b[i].r,&b[i].a,&b[i].b);
b[i].id=i;
}
sort(b+1,b+m+1,cmp);
int l=1,r=0;
for(i=1;i<=m;i++)
{
while(r<b[i].r)
add(a[++r]);
while(l>b[i].l)
add(a[--l]);
while(r>b[i].r)
del(a[r--]);
while(l<b[i].l)
del(a[l++]);
ans[b[i].id]=orzzjt::query(b[i].a,b[i].b);
}
for(i=1;i<=m;i++)
printf("%d\n",ans[i]);
return 0;
}

【BZOJ3809】Gty的二逼妹子序列 莫队 分块的更多相关文章

  1. [BZOJ3809]Gty的二逼妹子序列[莫队+分块]

    题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...

  2. Bzoj 3809: Gty的二逼妹子序列 莫队,分块

    3809: Gty的二逼妹子序列 Time Limit: 35 Sec  Memory Limit: 28 MBSubmit: 868  Solved: 234[Submit][Status][Dis ...

  3. bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...

  4. bzoj 3809 Gty的二逼妹子序列——莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...

  5. [AHOI2013]作业 & Gty的二逼妹子序列 莫队

    ---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...

  6. BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块

    Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...

  7. [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业

    [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj   bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...

  8. [bzoj3809]Gty的二逼妹子序列_莫队_分块

    Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...

  9. bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...

随机推荐

  1. koa入门

    创建koa2工程 首先初始化项目 npm init -y 项目名称 安装koa $ npm i koa 我们创建一个目录hello-koa并作为工程目录用VS Code打开.然后,我们创建app.js ...

  2. UnderWater+SDN论文之三

    Software-Defined Underwater Acoustic Modems: Historical Review and the NILUS Approach Source: IEEE J ...

  3. Could not open connection

    意思是不能打开JDBC连接,如果代码没写错的话就是服务没打开,开一下服务就行了,oracle两个必开的服务:OracleServiceORCL和OracleOraDb11g_home2TNSListe ...

  4. React Native之FlatList的介绍与使用实例

    React Native之FlatList的介绍与使用实例 功能简介 FlatList高性能的简单列表组件,支持下面这些常用的功能: 完全跨平台. 支持水平布局模式. 行组件显示或隐藏时可配置回调事件 ...

  5. Redis 安装学习

    Linux下下载安装redis https://redis.io/download tar -zvxf redisxxx cd redisxxxx make  ---进行安装 vim ~.bash_p ...

  6. Hbase存储模式

    以key.value的结构存储数据;  (table,rowkey,family,colum,timestamp)构成数据的key,value存储数据

  7. python爬虫之git的使用(origin说明)

    1.首先我们回忆两个命令 #git remote add origin 远程仓库链接 #git push -u origin master 我们一起看看这个命令,git是git的一级命令,push就是 ...

  8. Python:matplotlib绘制散点图

    与线型图类似的是,散点图也是一个个点集构成的.但不同之处在于,散点图的各点之间不会按照前后关系以线条连接起来. 用plt.plot画散点图     奇怪,代码和前面的例子差不多,为什么这里显示的却是散 ...

  9. idea创建maven项目的一点关键

    maven中的一些概念: POM:项目对象模型(Project Object Model),是项目的一些关键元信息的集合.主要包含项目管理信息.具体的项目描述.开发小组的构 成.源代码库(如CVS)和 ...

  10. 九、.net core用orm继承DbContext(数据库上下文)方式操作数据库

    一.创建一个DataContext普通类继承DbContext  安装程序集:Pomelo.EntityFrameworkCore.MySql   二.配置连接字符串(MySql/SqlServer都 ...