【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的妹子序列了!但他们遇到了 ...
随机推荐
- stark组件数据库管理软件的总结
1.stark - 总结 用到的知识点: 1.单例模式 2.继承 3.反射 4.面向对象 5.modelform 1.注册表 单例模式 site = StarkSite() 2.生成url url ...
- selenium处理alert弹出框
import time from selenium import webdriver driver =webdriver.Chrome(r"D:\工具包\chromedriver.exe&q ...
- Oracle条件判断if...elsif
- 莫烦scikit-learn学习自修第五天【训练模型的属性】
1.代码实战 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ from sklearn import datasets from sklearn.linear ...
- 老男孩python学习自修第十二天【常用模块之生成随机数】
常用函数 import random random.random() 生成0到1之间的小数 random.randint(begin, end) 生成[begin, end]之间的整数 random. ...
- 手写事务管理器 也是spring实现事务管理的原理
- codeforces467C
George and Job CodeForces - 467C The new ITone 6 has been released recently and George got really ke ...
- CF980E
题面 Panel 国将举办名为数字游戏的年度表演.每个省派出一名选手. 国家有 n 个编号从 1 到 n 的省,每个省刚好有一条路径将其与其他省相连.第 i 个省出来的代表有 2^i 名粉丝. 今年, ...
- 鼠标事件-MouseEvent
当鼠标进行某种操作时,就会生成一个event对象,该对象记录着鼠标触发事件时的所有属性. 可以通过如下方法在google控制台打印出 MouseEvent 对象. function mouseDown ...
- Ubuntu16.04安装使用wps
Ubuntu16.04安装使用wps 1.wps官网下载并安装wps 此处以Debian安装包为例,官网下载路径 http://www.wps.cn/product/wpslinux/# 直接安装: ...