浅谈莫队: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的二逼妹子序列的更多相关文章

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

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

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

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

  3. BZOJ3809: Gty的二逼妹子序列

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

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

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

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

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

  6. 2019.01.08 bzoj3809: Gty的二逼妹子序列(莫队+权值分块)

    传送门 题意:多组询问,问区间[l,r]中权值在[a,b]间的数的种类数. 看了一眼大家应该都知道要莫队了吧. 然后很容易想到用树状数组优化修改和查询做到O(mnlogamax)O(m\sqrt nl ...

  7. 【莫队算法】【权值分块】bzoj3809 Gty的二逼妹子序列

    如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; int ...

  8. 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块

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

  9. 【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1072  Solved: 292[Submit][Status][Di ...

  10. BZOJ 3809: Gty的二逼妹子序列

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1387  Solved: 400[Submit][Status][Di ...

随机推荐

  1. openpyxl之excel操作

    一.读取excel中内容 1.导入模块 : from openpyxl import load_workbook 2.打开excel : workbook = load_workbook(" ...

  2. 跨平台移动开发_PhoneGap API 事件类型

    PhoneGap API Events backbuttondevicereadymenubuttonpauseresumeonlineofflinebatterycriticalbatterylow ...

  3. 常用display属性

    table 1. 宽高由内容撑开 2.独占一行 3.可设置宽高 4.可设置margin.padding inline-table 与display: table大体一致,区别在于不独占一行,为行内元素 ...

  4. oracle修改密码和设置密码有效期

    一.修改密码1)修改密码 sql>alter user user01 identified by password; 2)修改密码并unlock sql>alter user user01 ...

  5. 医院内外网之间通过网闸交互,通过端口转发加nginx代理实现内网访问外网

    首先介绍下主要需求,很简单,就是要在医院his系统内嵌公司的平台,实现内网直接访问外网 这是院方给我提供的网闸相关配置,105是医院内网的服务器,120是外网的服务器,中间通过网闸配置的几个端口实现互 ...

  6. python练习_三级菜单

    python练习_三级菜单 需求: 做一个地区查询三级菜单,输入一级能够打印下一级 在第三级个第二级输入e可以返回上一级 在任意一级输入q则退出程序 以下代码实现的功能与思路: 功能: (1)通过In ...

  7. react列表中,当key改变后发生的事情

    Main.jsx export default class Main extends PureComponent { constructor(props) { super(props); this.s ...

  8. idel 中 生成 jar包 和项目中自己需要的包

    一.首先在自己的项目中创建一个类类中创建一个构造方法构造方法中传入一个字符串参数(这个字符串参数是为了传入路径) 在方法体内通过file类创建文件夹(换而言之就是项目中的包) 二 .就是对这个项目中的 ...

  9. Java-集合类源码List篇(二)

    前言 上篇中,我们分析了ArrayList的常用方法及其实现机制.ArrayList是基于内存空间连续的数组来实现的,List中其实还提供了一种基于链表结构的LinkedList来实现集合.同时多线程 ...

  10. js的事件处理与闭包:

    var i = 0; for(i=0;i<5;i++){ (function(i){ setTimeout(function(){alert(i)},3000); })(i) } // 上面打印 ...