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之间,所以我们可以建一个基 ...
随机推荐
- 关于改变placeholder的颜色
input::-webkit-input-placeholder{ color:red; } input::-moz-placeholder{ /* Mozilla Firefox 19+ */ co ...
- JVM垃圾回收补充知识点
1. 分代 虚拟机中的共划分为三个代: 年轻代(Young Gen):eden和survivor-8:1:1 年老代(Old Gen):存储大对象,由survivor晋升 永久代(perm Gen): ...
- Docker官方文档翻译1
转载请标明出处: https://blog.csdn.net/forezp/article/details/80098675 本文出自方志朋的博客 本系列教程翻译于docker文档,文档地址:http ...
- TIDB2 —— 三篇文章了解 TiDB 技术内幕 - 说存储
原文地址:https://pingcap.com/blog-cn/tidb-internal-1/ 引言 数据库.操作系统和编译器并称为三大系统,可以说是整个计算机软件的基石.其中数据库更靠近应用层, ...
- onload函数不执行
原因: <jsp:include page="">通过该标签引入jsp时会导致<body>失效 案例: <jsp:include page=" ...
- c++ 数字和字符串的相互转换
需要包含头文件<sstream> 字符串转化为int #include<stdio.h> #include<string.h> #include<iostre ...
- python核心编程2 第十章 练习
10-6.改进的open().为内建的open()函数创建一个封装.使得成功打开文件后,返回文件句柄:若打开失败则返回给调用者None, 而不是生成一个异常.这样你打开文件就不需要额外的异常处理语句. ...
- 转:前端安全之XSS攻击
前端安全 原文链接:https://www.freebuf.com/articles/web/185654.html 随着互联网的高速发展,信息安全问题已经成为企业最为关注的焦点之一,而前端又是引发企 ...
- rails小技巧之分组查询统计并去重
分组查询并统计 SpecialGroup.group(:special_type).count select special_type,count(*) from special_groups gro ...
- unity独立游戏开发日记2018/09/27
今天优化了下昨天的代码,并且添加了树木和其他资源的生成.还修复了接近石头后,挖掘图标不出现的bug.目前可以在unity中稳定60-70fps. 详看文章:https://www.cnblogs.co ...