BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description
Input
Output
对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。
Sample Input
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4
Sample Output
0
0
2
1
1
1
0
1
2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
const int MAXN=;
const int MAXQ=;
const int size=; int N,M,S[MAXN],ans[MAXQ];
struct que{
int id,l,r,a,b;
friend bool operator < (que a,que b){
return a.l/size<b.l/size||a.l/size==b.l/size&&a.r<b.r;
}
}q[MAXQ];
struct BLOCK{
static const int maxn=;
static const int SIZE=;
static const int maxm=;
int c[maxn],kind[maxm],lef[maxm],rig[maxm],cnt,belong[maxn];
BLOCK(){ cnt=; }
void build(int n){
int p=;
while(p+SIZE<n+){
lef[cnt]=p,rig[cnt]=p+SIZE,p+=SIZE,cnt++;
for(int i=lef[cnt-];i<rig[cnt-];i++) belong[i]=cnt-;
}
lef[cnt]=p,rig[cnt]=n+;
for(int i=lef[cnt];i<rig[cnt];i++) belong[i]=cnt;
}
void update(int p,int v){
if(c[p]&&c[p]+v==) kind[belong[p]]--;
if(!c[p]&&c[p]+v==) kind[belong[p]]++;
c[p]+=v;
}
int query(int L,int R){
int re=,p=L;
if(belong[L]==belong[R]){
for(int i=L;i<=R;i++) if(c[i]) re++;
return re;
}
for(int i=L;i<rig[belong[L]];i++) if(c[i]) re++;
for(int i=lef[belong[R]];i<=R;i++) if(c[i]) re++;
for(int i=belong[L]+;i<belong[R];i++) re+=kind[i];
return re;
}
}block; void _scanf(int &x)
{
x=;
char c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='') x=x*+c-'',c=getchar();
}
int out_cnt,out[];
void _printf(int x)
{
out[++out_cnt]=x%,x/=;
while(x) out[++out_cnt]=x%,x/=;
while(out_cnt) putchar(''+out[out_cnt--]);
putchar('\n');
}
void data_in()
{
_scanf(N);_scanf(M);
for(int i=;i<=N;i++) _scanf(S[i]);
for(int i=;i<=M;i++){
_scanf(q[i].l);_scanf(q[i].r);
_scanf(q[i].a);_scanf(q[i].b);
q[i].id=i;
}
}
void movep(int &i,int j,int t)
{
while(i<j){
if(!t) block.update(S[i++],-);
else block.update(S[++i],);
}
while(i>j){
if(!t) block.update(S[--i],);
else block.update(S[i--],-);
}
}
void work()
{
sort(q+,q+M+);
block.build(N);
int l=,r=;
block.update(S[],);
for(int i=;i<=M;i++){
movep(r,q[i].r,);
movep(l,q[i].l,);
ans[q[i].id]=block.query(q[i].a,q[i].b);
}
for(int i=;i<=M;i++) _printf(ans[i]);
}
int main()
{
data_in();
work();
return ;
}
BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块的更多相关文章
- 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) ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
- BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...
- [BZOJ3809]Gty的二逼妹子序列[莫队+分块]
题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...
- bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)
[题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...
随机推荐
- JavaScript函数-高阶函数
JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数. function add(x,y,f) ...
- Linux/Mac scp命令笔记
scp命令用于Linux之间复制文件和目录. 参数说明: -1: 强制scp命令使用协议ssh1-2: 强制scp命令使用协议ssh2-4: 强制scp命令只使用IPv4寻址-6: 强制scp命令只使 ...
- Struts2前期(这框架目前正处于淘汰状态)
Struts2第一天 Struts2的学习路线 1. Struts2的入门:主要是学习Struts2的开发流程(Struts2的开发流程.常见的配置.Action类的编写) 2. Struts2的Se ...
- 修改zabbix字体格式
环境: centos7 zabbix3.2 1.获取喜欢的字体格式文件(喜欢别的字体也可以去网上下载) 通常都是ttf格式,可直接在windows下获取C:\Windows\Fonts 2.配置zab ...
- Eclipse关联tomcat
一,添加Tomcat Windows-->Preferences-->Server-->Runtime Enviroment添加一个tomcat,这里选择tomcat8.0 Next ...
- 【原创】如何治疗使用python中re模块group、groups与findall分组匹配后产生的“眩晕反应”
转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9244783.html 直接先上例子 >>> re.search('(book+ ...
- javaWeb项目加载HTML文件时报错 [No Find /index.html]
直接上主题: 在web.xml文件中添加如下信息: <display-name>Spring MVC Application</display-name> <servle ...
- Redis学习推荐
Redis快速入门 https://www.yiibai.com/redis/redis_quick_guide.html Redis用途和使用场景 https://blog.csdn.net/wei ...
- hive的load命令
Hive Load语句不会在加载数据的时候做任何转换工作,而是纯粹的把数据文件复制/移动到Hive表对应的地址. 语法 LOAD DATA [LOCAL] INPATH 'filepath' [OVE ...
- python中一些内置函数实例
lambda表达式 简单函数可用lambda表达式 1. def f1() return(123) r1=f1() print() 2. f2=lambda:123 r2=f2() print() 以 ...