【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的妹子序列了!但他们遇到了 ...
随机推荐
- UIAutomatorViewer 出现错误:Unable to connect to adb
最近升级了AndroidSDK,打开UIAutomatorViewer.bat,结果发现获取不了Android设备界面上的UI信息.经过一番努力,终于把这个问题解决了,详细过程如下: 1. Unabl ...
- Shell脚本命令图片
查看相关文档:shell脚本1 shell脚本2
- PAT L2-013 红色警报
https://pintia.cn/problem-sets/994805046380707840/problems/994805063963230208 战争中保持各个城市间的连通性非常重要.本题要 ...
- windows 内建环境变量
PS C:\Windows> ls env: Name Value ---- ----- _NT_SYMBOL_PATH srv*C:\Users\vv\Documents\symbols AL ...
- 微信小程序错误码参考大全
开发过程中,会遇到很多微信返回的状态码,鬼知道代表什么意思,现在好了,整理总结了一份状态码,方便大家. 转载:http://www.yiyongtong.com/archives/view-1856- ...
- Mapper动态代理方式
开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同Dao接口实现类方法. Mapper接 ...
- 从git中删除 .idea 目录
将.idea目录加入ignore清单: $ echo '.idea' >> .gitignore 从git中删除idea: $ git rm —cached -r .idea 3 将. ...
- 版本控制Git使用最佳实践
总结版本控制Git的使用,应明确有哪些具体的场景 应用场景 紧急上线(hotfix) 功能开发(feature) 测试(dev/release) 生产(master) 紧急上线 4.git c ...
- jdbc工具类2..0
一.创建外部文件 url=jdbc:mysql:///qy66 use=root password=root driver=com.mysql.jdbc.Driver 二.创建工具类 package ...
- Java之指定Junit测试方法的执行顺序举例
问题描述: 大家都知道使用JUnit进行测试的时候,方法的执行顺序不是按照编写的先后顺序执行的,那么如何控制Junit的执行顺序呢? 解决方法: 在测试类上加 @FixMethodOrder 注解即可 ...