【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的妹子序列了!但他们遇到了 ...
随机推荐
- 【转】安装ambari的时候遇到的ambari和hadoop问题集
5.在安装的时候遇到的问题 5.1使用ambari-server start的时候出现ERROR: Exiting with exit code -1. 5.1.1REASON: Ambari Ser ...
- asp.net mvc5 action多个参数
需要完成http://site.com/user/add/1/2这样的url解析 使用action的参数直接获取数据的方式 Action声明如下 ) { ViewBag.clubID = id; ) ...
- 部署ingress及使用
一.下载yaml文件 wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.y ...
- 用python写一个预警机器人(支持微信和钉钉)
背景 线上的系统在运行中,发生故障时怎么及时的通过手机通知到相关人员?当然这是个很简单的需求,现有的方法有很多,例如: 如果我们用的云产品,那么一般都会有配套对应的监控预警功能,根据需要配置一下即可, ...
- python设计模式第七天【建造者模式】
1. 建造者模式UML图 2.应用场景 (1)专门创建具有符合属性的对象 3.代码实现 #!/usr/bin/env python #! _*_ coding: UTF-8 _*_ from abc ...
- 老男孩python学习自修第四天【字典的使用】
dict = {key1:value1, key2:value2} 定义字典 dict[key] = value 设置字典中指定健的值 dict.pop(key) 删除字典中指定健 dict.popi ...
- 错误模块名称: KERNELBASE.dll
部署win服务时,经常会出现类似下面的错误,错误模块名称: KERNELBASE.dll. 日志名称: Application 来源: Application ...
- SpringBoot之整合Mybatis范例
依赖包: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:/ ...
- vue 子组件修改父组件传来的props值,报错
vue不推荐直接在子组件中修改父组件传来的props的值,会报错 [Vue warn]: Avoid mutating a prop directly since the value will be ...
- 转 在PowerDesigner的PDM图形窗口中显示数据列的中文注释
Name是名称(字段描述),Code是字段名称,Comment是注释名称,ER图中显示的是Name.一般设计时,Name跟comment都设计成描述, 而设计时候常把comment写成中文,name保 ...