BZOJ3809:Gty的二逼妹子序列
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=3809
用树状数组维护区间内元素个数,直接在树状数组上统计即可。
然后就\(TLE\)了…………
在桶上使用分块科技统计元素就能\(A\)了。
时间复杂度:\(O(n\sqrt{n})\)
空间复杂度:\(O(n)\)
代码如下:
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define low(i) ((i)&(-(i)))
const int maxn=1e5+5,maxm=1e6+5;
int n,m,block;
int num[maxn],bel[maxn],L[320],R[320];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct query {
int l,r,a,b,id,ans;
bool operator<(const query &a)const {
if(bel[l]!=bel[a.l])return bel[l]<bel[a.l];
if(bel[l]&1)return r<a.r;return r>a.r;
}
}q[maxm];
struct sqrt_technology {
int sum[320],cnt[maxn];
void add(int pos,int v) {
if(cnt[pos]==0&&v==1)sum[bel[pos]]++;
if(cnt[pos]==1&&v==-1)sum[bel[pos]]--;
cnt[pos]+=v;
}
int query(int l,int r) {
int res=0;
if(bel[l]==bel[r]) {
for(int i=l;i<=r;i++)
res+=(cnt[i]!=0);
}
else {
for(int i=l;i<=R[bel[l]];i++)
res+=(cnt[i]!=0);
for(int i=L[bel[r]];i<=r;i++)
res+=(cnt[i]!=0);
for(int i=bel[l]+1;i<bel[r];i++)
res+=sum[i];
}
return res;
}
}T;
bool cmp(query a,query b) {return a.id<b.id;}
int main() {
n=read(),m=read(),block=sqrt(n);
for(int i=1;i<=n;i++) {
num[i]=read(),bel[i]=(i-1)/block+1;
if(bel[i]!=bel[i-1])R[bel[i-1]]=i-1,L[bel[i]]=i;
}R[bel[n]]=n;
for(int i=1;i<=m;i++) {
q[i].id=i;
q[i].l=read(),q[i].r=read();
q[i].a=read(),q[i].b=read();
}
sort(q+1,q+m+1);
int nowl=1,nowr=0;
for(int i=1;i<=m;i++) {
while(nowl<q[i].l)T.add(num[nowl++],-1);
while(nowl>q[i].l)T.add(num[--nowl],1);
while(nowr<q[i].r)T.add(num[++nowr],1);
while(nowr>q[i].r)T.add(num[nowr--],-1);
q[i].ans=T.query(q[i].a,q[i].b);
}
sort(q+1,q+m+1,cmp);
for(int i=1;i<=m;i++)
printf("%d\n",q[i].ans);
return 0;
}
BZOJ3809:Gty的二逼妹子序列的更多相关文章
- [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的二逼妹子序列
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方 ...
- [BZOJ3809]Gty的二逼妹子序列[莫队+分块]
题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
- 2019.01.08 bzoj3809: Gty的二逼妹子序列(莫队+权值分块)
传送门 题意:多组询问,问区间[l,r]中权值在[a,b]间的数的种类数. 看了一眼大家应该都知道要莫队了吧. 然后很容易想到用树状数组优化修改和查询做到O(mnlogamax)O(m\sqrt nl ...
- 【莫队算法】【权值分块】bzoj3809 Gty的二逼妹子序列
如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; int ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
- 【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 ...
随机推荐
- 写python中的装饰器
python中的装饰器主要用于在已有函数实现功能前附加需要输出的信息,下面将用实例展示我如何写装饰器. 首先分别尝试写装饰器装饰一个无参函数和一个有参函数(被装饰函数仅输出,无返回值情况下) def ...
- 使用git从本地上传至git码云远程仓库
从 http://git-scm.com/download 下载window版的客户端.下载好,一步一步安装即可. 使用前的基本设置 git config --global user.name & ...
- Windos Server 2008 NFS 服务安装使用
系统环境:Windos 2008 R2 x64位 安装服务:NFS 文件服务 我的电脑-->右击管理-->功能-->添加功能 选择网络文件系统服务工具 安装服务 添加角色 下一步 选 ...
- Python字符串格式转换
转换类型 转换类型 说明 d, i 带符号十进制 b 无符号二进制 o 无符号八进制 u 无符号十进制 x 无符号十六进制(小写) X 无符号十六进制(大写) e 科学计数法表示的浮点数(小写) E ...
- 优美的英文诗歌Beautiful English Poetry
<When you are old>——<当你老了> --- William Butler Yeats ——威廉·巴特勒·叶芝When you are old and grey ...
- wampserver安装在服务器中,但是mysql不能远程登录的解决方案
利用mysql workbench或者Navicat连接服务器的mysql时,有时候会出现拒绝访问, 因为在mysql中没有赋予其他用户权限,只能本地登录,所以要进行设置. 设置如下: 打开mysql ...
- shell 读取文件的每一行内容并输出
(1)#!/bin/bash while read linedo echo $linedone < file (2)#!/bin/bash cat file | while read l ...
- 并发Socket程序设计
1. 非阻塞并发模型 直接将socket设置为非阻塞, 轮询处理连接和接收. 缺点: 极大消耗CPU资源,不适合实际应用. 2. 信号驱动模型 当Socket文件描述符准备就绪后 内核会给进程发送一个 ...
- Python ssh 远程执行shell命令
工具 python paramiko 远程执行命令 import paramiko ssh = paramiko.SSHClient() key = paramiko.AutoAddPolicy() ...
- json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...