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]\ ...
随机推荐
- 梯度优化算法总结以及solver及train.prototxt中相关参数解释
参考链接:http://sebastianruder.com/optimizing-gradient-descent/ 如果熟悉英文的话,强烈推荐阅读原文,毕竟翻译过程中因为个人理解有限,可能会有谬误 ...
- Linux MMC framework2:基本组件之core
1.前言 本文主要core组件的主要流程,在介绍的过程中,将详细说明和core相关的流程,涉及到其它组件的详细流程再在相关文章中说明. 2.主要数据结构和API TODO 3. 主要流程 3.1 mm ...
- V4L2学习记录【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637600.html V4L2学习记录 这个还没有分析完,先在这放着,防止电脑坏掉丢了,以后再完善 V4L ...
- 转载:Nginx是什么(1.1)《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19609.html 人们在了解新事物时,往往习惯通过类比来帮助自己理解事物的概貌.那么,我们在学习Nginx时也采用同样的方式,先来看 ...
- Python-ccs动画及阴影
动画及阴影 0. 什么时候该用什么布局 <!-- 定位布局: 以下两种布局不易解决的问题, 盒子需要脱离文档流处理 --> <!-- 浮动布局: 一般有block特性的盒子,水平排列 ...
- OCM_第七天课程:Section3 —》数据库可用性
注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...
- 【linux】16进制格式查看命令hexdump
test.txt内容 asdfsg ewtwfsdf1Hello World! hexdump -Cv test.txt 输出 |asdfsg ewtwfsdf1| 6c 6c 6f 6f 6c ...
- cf276E 两棵线段树分别维护dfs序和bfs序,好题回头再做
搞了一晚上,错了,以后回头再来看 /* 对于每次更新,先处理其儿子方向,再处理其父亲方向 处理父亲方向时无法达到根,那么直接更新 如果能达到根,那么到兄弟链中去更新,使用bfs序 最后,查询结点v的结 ...
- 对象本田CRV
<script> window.onload = function (ev) { var vcar = new Car("本田", "CRV", 4 ...
- Oracle学习笔记--第3章 使用sql*plus工具
使用sql*plus工具 1.sqlplus连接方式示例 sqlplus system/123[as sysdba]; 2.查看表结构命令;desc[ribe] e.g. desc scot ...