【Luogu4396】[AHOI2013]作业(莫队)
【Luogu4396】[AHOI2013]作业(莫队)
题面
题解
模板题
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX 300300
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
const int blk=550;
struct Query{int l,r,bl,a,b,id;}p[MAX];
bool operator<(Query a,Query b){if(a.bl!=b.bl)return a.bl<b.bl;return a.r<b.r;}
int n,m,a[MAX],num[MAX],bs[MAX],bnum[MAX],o[MAX],tot,bh[MAX];
int Bound(int x){return lower_bound(&o[1],&o[tot+1],x)-o;}
int ans1[MAX],ans2[MAX],Ans;
void Add(int x)
{
if(!num[a[x]]++)++bnum[bh[a[x]]];
bs[bh[a[x]]]+=1;
}
void Del(int x)
{
if(!--num[a[x]])--bnum[bh[a[x]]];
bs[bh[a[x]]]-=1;
}
void Calc(int a,int b,int id)
{
int s1=0,s2=0;
for(int &i=a;i<=b&&bh[i]==bh[i-1];++i)s1+=num[i],s2+=(num[i]>=1);
for(int &i=b;i>=a&&bh[i]==bh[i+1];--i)s1+=num[i],s2+=(num[i]>=1);
if(a<=b)for(int i=bh[a];i<=bh[b];++i)s1+=bs[i],s2+=bnum[i];
ans1[id]=s1;ans2[id]=s2;
}
int main()
{
n=read();m=read();
for(int i=1;i<=n;++i)a[i]=read(),o[++tot]=a[i];
for(int i=1;i<=m;++i)
{
int l=read(),r=read(),a=read(),b=read();
p[i]=(Query){l,r,(l-1)/blk+1,a,b,i};
o[++tot]=a;o[++tot]=b;
}
sort(&o[1],&o[tot+1]);tot=unique(&o[1],&o[tot+1])-o-1;
for(int i=1;i<=n;++i)a[i]=Bound(a[i]);
for(int i=1;i<=m;++i)p[i].a=Bound(p[i].a),p[i].b=Bound(p[i].b);
sort(&p[1],&p[m+1]);
for(int i=1;i<=tot;++i)bh[i]=(i-1)/blk+1;
int L=1,R=0;
for(int i=1;i<=m;++i)
{
while(L<p[i].l)Del(L++);
while(L>p[i].l)Add(--L);
while(R<p[i].r)Add(++R);
while(R>p[i].r)Del(R--);
Calc(p[i].a,p[i].b,p[i].id);
}
for(int i=1;i<=m;++i)printf("%d %d\n",ans1[i],ans2[i]);
return 0;
}
【Luogu4396】[AHOI2013]作业(莫队)的更多相关文章
- [AHOI2013]作业 (莫队+分块)
[AHOI2013]作业 (莫队+分块) 题面 给定了一个长度为n的数列和若干个询问,每个询问是关于数列的区间[l,r],首先你要统计该区间内大于等于a,小于等于b的数的个数,其次是所有大于等于a,小 ...
- Bzoj 3236: [Ahoi2013]作业 莫队,分块
3236: [Ahoi2013]作业 Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1113 Solved: 428[Submit][Status ...
- BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )
莫队..用两个树状数组计算.时间复杂度应该是O(N1.5logN). 估计我是写残了...跑得很慢... ----------------------------------------------- ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
- BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]
题意: 询问区间权值在$[a,b]$范围内种类数和个数 莫队 权值分块维护种类数和个数$O(1)-O(\sqrt{N})$ #include <iostream> #include < ...
- BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- BZOJ3236:[AHOI2013]作业(莫队,分块)
Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 ...
- bzoj 3236: 洛谷 P4396: [AHOI2013]作业 (莫队, 分块)
题目传送门:洛谷P4396. 题意简述: 给定一个长度为\(n\)的数列.有\(m\)次询问,每次询问区间\([l,r]\)中数值在\([a,b]\)之间的数的个数,和数值在\([a,b]\)之间的不 ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
随机推荐
- 01-学习vue前的准备工作
起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就直接 ...
- Ubuntu16系统中安装htpasswd
htpasswd是Apache附带的程序, htpasswd生成包含用户名和密码的文本文件, 每行内容格式为“用户名:密码”, 用于用户文件的基本身份认证. 当用户浏览某些网页的时候, 浏览器会提示输 ...
- js判断数组是否包含某个字符串变量的实例
最近碰到一个这样的现象,后台返回的数据中,数组里面有一些有变量值,有一些没有变量值. 举个例子,比如后台返回的例子是这样的: var arr=[ { "status":" ...
- AngularJS集合数据遍历显示
AngularJS集合数据遍历显示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- Bootstrap 字体图标(Glyphicons)
http://www.runoob.com/bootstrap/bootstrap-glyphicons.html 什么是字体图标? 字体图标是在 Web 项目中使用的图标字体.虽然,Glyphico ...
- servlet ServletContext
一.Servlet简介 1.什么是Servlet Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是 ...
- C#中is运算符
is运算符可以检查对象是否与特定的类型兼容.“兼容”表示对象或者该类型,或者派生自该类型.例如,要检查变量是否与object类型兼容,可以使用下面的代码: int i=10; if(i is ob ...
- 销售合同金额数据从Excel导入
一.业务需求 1.新增了销售合同金额的字段,但是老数据没有这个字段:所以销售合同金额从销售合同附件的各品种金额之和. 2.制作好excel字段模板,将此模板发送给销售业务部门来统计并完成excel表格 ...
- Zend Framework2从入门到精通
1. 下载安装zf2的web程序 步骤: 第一步,保证得到一个基本的zf2框架 直接从官网下载并解压即可:http://www.zendframework.org.cn/downloads/lates ...
- SpringBoot之文件下载
package org.springboot.controller; import org.springboot.constant.Constant; import org.springframewo ...