【BZOJ3809】Gty的二逼妹子序列 莫队 分块
题目描述
给你一个长度为\(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的二逼妹子序列 莫队 分块的更多相关文章
- [BZOJ3809]Gty的二逼妹子序列[莫队+分块]
题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- 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) ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- [bzoj3809]Gty的二逼妹子序列_莫队_分块
Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
随机推荐
- 两次console.log打印值不同
var a=[1,2,3] console.log(a); a[2]=10; console.log(a); // 打印结果 // (3) [1, 2, 3] // (3) [1, 2, 10] // ...
- python第八章:多任务--小白博客
多线程threading 多线程特点: #线程的并发是利用cpu上下文的切换(是并发,不是并行)#多线程执行的顺序是无序的#多线程共享全局变量#线程是继承在进程里的,没有进程就没有线程#GIL全局解释 ...
- H5 61-浮动元素贴靠现象
61-浮动元素贴靠现象 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- Linux下php安装redis扩展(redis已经安装)
1. 下载需要的php操作redis的扩展包 (1).切换到 cd /usr/local/src (2). wget https://github.com/nicolasff/phpredis ...
- XT535
今天金山误删了一个文件,把手机系统整坏了,故刷了个机,刷机教程: http://bbs.dospy.com/thread-15027415-1-623-1.html 中间安装了个驱动精灵,否则手机开启 ...
- MySql concat与字符转义
mysql函数之四:concat() mysql 多个字段拼接 - duanxz - 博客园https://www.cnblogs.com/duanxz/p/5098875.html mysql 多个 ...
- Jenkins ChangeLog
Log changes in Jenkins - Stack Overflowhttps://stackoverflow.com/questions/13631145/log-changes-in-j ...
- socket FTP-1
基于socket实现文件的传输以及md5验证 server: import socket import os import hashlib server=socket.socket() server. ...
- CentOS7 网络NAT模式
问题:安装完毕ping命令不能用,然后改为桥接模式,ping可以用. 先了解桥接,NAT 的含义. 桥接:在bridged模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它可以访 ...
- Java集合和数组的区别
参考:Java集合和数组的区别 集合和容器都是Java中的容器. 区别 数组特点:大小固定,只能存储相同数据类型的数据 集合特点:大小可动态扩展,可以存储各种类型的数据 转换 数组转换为集合: A ...