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]\ ...
随机推荐
- lnmp使用socket方式连接nginx优化php-fpm性能
lnmp使用socket方式连接nginx优化php-fpm性能 Nginx连接fastcgi的方式有2种:TCP和unix domain socket 什么是Unix domain socket?- ...
- ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061),mysql服务已启动
1 前言 在mysql服务已启动,用命令行进入或者heidisql工具都提示ERROR 2003 (HY000): Can't connect to MySQL server on 'localhos ...
- mac安装RabbitMQ
1 下载 地址 http://www.rabbitmq.com/install-standalone-mac.html 2 rabbitmq的安装目录: /Users/ysyc1/rabbitmq_s ...
- 篮球弹起问题(for循环)
- python-随机操作(random)
random模块作用是返回随机数,只要跟随机元素相关的,都可以使用它. Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等 ...
- myBatis各种依赖包
下载地址:myBatis各种依赖包
- MVC4 下DropDownList使用方法(转)
与MVC3相比,差别很大: 表现形式一: public ActionResult Main() { List<SelectListItem> items = new List<Sel ...
- Codeforces 605C Freelancer's Dreams 凸包 (看题解)
Freelancer's Dreams 我们把每个二元组看成是平面上的一个点, 那么两个点的线性组合是两点之间的连线, 即x * (a1, b1) + y * (a1, b1) && ...
- Linux虚拟内存的添加
引用自:http://blog.sina.com.cn/s/blog_9150610c0102weym.html 引用自: https://blog.csdn.net/libaoan1971/arti ...
- BZOJ3674 可持久化并查集加强版 可持久化 并查集
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3674 题意概括 n个集合 m个操作操作:1 a b 合并a,b所在集合2 k 回到第k次操作之后的 ...