BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块
BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块
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
HINT
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
char nc() {
static char buf[100000],*p1,*p2;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd() {
register int x=0;
register char s=nc();
while(s<'0'||s>'9')s=nc();
while(s>='0'&&s<='9')x=(x<<3)+(x<<1)+s-'0',s=nc();
return x;
}
#define N 100001
int n,m,c[N],pos[N],L[1050],R[1050],size,block,ansblo[1050],h[N],ans[N*10];
struct A {
int l,r,a,b,id;
}q[N*10];
bool cmp(const A &x,const A &y) {
if(pos[x.l]!=pos[y.l]) return x.l<y.l;
return x.r<y.r;
}
int query(int l,int r) {
int p=pos[l],q=pos[r],ans=0,i;
if(p==q) {
for(i=l;i<=r;i++) {
if(h[i]) ans++;
}
return ans;
}
for(i=p+1;i<q;i++) {
ans+=ansblo[i];
}
for(i=l;i<=R[p];i++) {
if(h[i]) ans++;
}
for(i=L[q];i<=r;i++) {
if(h[i]) ans++;
}
return ans;
}
void del(int x) {
h[x]--;
if(h[x]==0) ansblo[pos[x]]--;
}
void add(int x) {
h[x]++;
if(h[x]==1) ansblo[pos[x]]++;
}
void solve() {
int l=1,r=0,i;
for(i=1;i<=m;i++) {
while(l<q[i].l) del(c[l]),l++;
while(r>q[i].r) del(c[r]),r--;
while(l>q[i].l) l--,add(c[l]);
while(r<q[i].r) r++,add(c[r]);
ans[q[i].id]=query(q[i].a,q[i].b);
}
}
int main() {
n=rd(); m=rd();
int i,j,size=sqrt(n);
block=n/size;
for(i=1;i<=block;i++) {
L[i]=R[i-1]+1; R[i]=size*i;
for(j=L[i];j<=R[i];j++) {
c[j]=rd(); pos[j]=i;
}
}
if(R[block]!=n) {
block++; L[block]=R[block-1]+1; R[block]=n;
for(i=L[block];i<=n;i++) {
c[i]=rd(); pos[i]=block;
}
}
for(i=1;i<=m;i++) {
q[i].l=rd(); q[i].r=rd(); q[i].a=rd(); q[i].b=rd();
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
solve();
for(i=1;i<=m;i++) {
printf("%d\n",ans[i]);
}
}
代码(3263):
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
char nc() {
static char buf[100000],*p1,*p2;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd() {
register int x=0;
register char s=nc();
while(s<'0'||s>'9')s=nc();
while(s>='0'&&s<='9')x=(x<<3)+(x<<1)+s-'0',s=nc();
return x;
}
#define N 100001
int n,m,c[N],pos[N],L[1050],R[1050],size,block,ansblo[1050],h[N],ans[N*10],ans1[N*10],ansblo1[1050];
struct A {
int l,r,a,b,id;
}q[N*10];
bool cmp(const A &x,const A &y) {
if(pos[x.l]!=pos[y.l]) return x.l<y.l;
return x.r<y.r;
}
int query(int l,int r) {
int p=pos[l],q=pos[r],ans=0,i;
if(p==q) {
for(i=l;i<=r;i++) {
if(h[i]) ans++;
}
return ans;
}
for(i=p+1;i<q;i++) {
ans+=ansblo[i];
}
for(i=l;i<=R[p];i++) {
if(h[i]) ans++;
}
for(i=L[q];i<=r;i++) {
if(h[i]) ans++;
}
return ans;
}
int query1(int l,int r) {
int p=pos[l],q=pos[r],i,ans=0;
if(p==q) {
for(i=l;i<=r;i++) {
if(h[i]) ans+=h[i];
}
return ans;
}
for(i=p+1;i<q;i++) ans+=ansblo1[i];
for(i=l;i<=R[p];i++) if(h[i]) ans+=h[i];
for(i=L[q];i<=r;i++) if(h[i]) ans+=h[i];
return ans;
}
void del(int x) {
h[x]--;
if(h[x]==0) ansblo[pos[x]]--;
ansblo1[pos[x]]--;
}
void add(int x) {
h[x]++;
if(h[x]==1) ansblo[pos[x]]++;
ansblo1[pos[x]]++;
}
void solve() {
int l=1,r=0,i;
for(i=1;i<=m;i++) {
while(l<q[i].l) del(c[l]),l++;
while(r>q[i].r) del(c[r]),r--;
while(l>q[i].l) l--,add(c[l]);
while(r<q[i].r) r++,add(c[r]);
ans[q[i].id]=query(q[i].a,q[i].b);
ans1[q[i].id]=query1(q[i].a,q[i].b);
}
}
int main() {
n=rd(); m=rd();
int i,j,size=sqrt(n);
block=n/size;
for(i=1;i<=block;i++) {
L[i]=R[i-1]+1; R[i]=size*i;
for(j=L[i];j<=R[i];j++) {
c[j]=rd(); pos[j]=i;
}
}
if(R[block]!=n) {
block++; L[block]=R[block-1]+1; R[block]=n;
for(i=L[block];i<=n;i++) {
c[i]=rd(); pos[i]=block;
}
}
for(i=1;i<=m;i++) {
q[i].l=rd(); q[i].r=rd(); q[i].a=rd(); q[i].b=rd();
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
solve();
for(i=1;i<=m;i++) {
printf("%d %d\n",ans1[i],ans[i]);
}
}
BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块的更多相关文章
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]
题意: 询问区间权值在$[a,b]$范围内种类数和个数 莫队 权值分块维护种类数和个数$O(1)-O(\sqrt{N})$ #include <iostream> #include < ...
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
- 【洛谷4396/BZOJ3236】[AHOI2013]作业(莫队+分块/树状数组/线段树)
题目: 洛谷4396 BZOJ3236(权限) 这题似乎BZOJ上数据强一些? 分析: 这题真的是--一言难尽 发现题面里没说权值的范围,怕出锅就写了离散化.后来经过面向数据编程(以及膜神犇代码)知道 ...
- [BZOJ3236]:[Ahoi2013]作业(莫队+分块)
题目传送门 题目描述 此时已是凌晨两点,刚刚做了$Codeforces$的小$A$掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可与做完的数学作业,接下来是分别都是一个小时可 ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 链接 分析: 和这道AHOI2013 作业差不多.权值是1~n的,所以对权值进行分块.$O(1)$修改,$O(\sqrt n)$查询. 代码: #include< ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
随机推荐
- oracle 修改 字段名称
暂时应该没有对应的方法,所以我用自己想好的方法去修改 /*修改原字段名name为name_tmp,是将想改名称的字段改为没用/临时的字段*/ Alter table 表名 rename column ...
- java设计模式--单列模式
java设计模式--单列模式 单列模式定义:确保一个类只有一个实例,并提供一个全局访问点. 下面是几种实现单列模式的Demo,每个Demo都有自己的优缺点: Demo1: /** * 单列模式需要满足 ...
- css中的行高line-height
“行高”顾名思意指一行文字的高度.具体来说是指两行文字间基线之间的距离.基线实在英文字母中用到的一个概念,我们刚学英语的时使用的那个英语本子每行有四条线,其中底部第二条线就是基线,是a,c,z,x等字 ...
- mongodb的安装使用,window和centos环境
官网:https://www.mongodb.org/downloads 版本:最终稳定版 (mongodb-win32-x86_64-2008plus-ssl-3.2.6-signed.msi 绿色 ...
- Qt5.7 实现Https 认证全过程解析(亲自动手版)
#### NetworkRequestManager.h #include <QSsl>#include <QSslKey>#include <QSslSocket> ...
- MySQL技术内幕 InnoDB存储引擎(笔记)
1. InnoDB 体系架构 其中,后台程序主要负责刷新内存池中的数据,保证缓冲池中的内存缓存的是最近的数据. 此外将已经修改的数据刷新到磁盘文件,同时保证在数据库发生异常的时候Innodb能恢复正常 ...
- Python 函数和相关用法笔记
python中%r和%s的区别 总结:%r打印时能够重现它所代表的对象 __str__和__repr__的用法
- Robot framework(RF) 用户关键字
3.6 用户关键字 在Robot Framework 中关键字的创建分两种:系统关键字和用户关键字. 系统关键字是需要通过脚本开发相应的类和方法,从而实现某一逻辑功能. 用户关键字是根据业务的需求利 ...
- Python_正则表达式样例
''' 正则表达式是字符串处理的有力工具和技术,正则表达式使用预定义的特定模式去匹配一类具有共同特征的字符串, 主要用于字符串处理,可以快速.准确地完成复杂的查找.替换等处理要求. 正则表达式由元字符 ...
- 进阶-JMS 知识梳理
JMS 一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Pro ...