BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]
题意:
询问区间权值在$[a,b]$范围内种类数和个数
莫队
权值分块维护种类数和个数$O(1)-O(\sqrt{N})$
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pii pair<int, int>
#define MP make_pair
#define fir first
#define sec second
typedef long long ll;
const int N=1e5+, M=1e6+, BN=, BS=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
} int n, Q, a[N], l, r, x, y;
struct _blo{int l, r;} b[BN];
int block, m, pos[N];
struct Block{
inline void ini(){
block=sqrt( n );
m=(n-)/block+;
for(int i=; i<=n; i++) pos[i]=(i-)/block+;
for(int i=; i<=m; i++) b[i].l=(i-)*block+, b[i].r=i*block; b[m].r=n;
}
int kind[BS], c[N], sum[BS];
inline void add(int v) {
c[v]++; sum[pos[v]]++;
if(c[v]==) kind[pos[v]]++;
}
inline void del(int v) {
c[v]--; sum[pos[v]]--;
if(c[v]==) kind[pos[v]]--;
}
pii que(int l,int r) {
int pl=pos[l], pr=pos[r], ans1=, ans2=;
if(pl==pr) for(int i=l; i<=r; i++) ans1+=c[i], ans2+= c[i]>;
else{
for(int i=pl+; i<pr; i++) ans1+=sum[i], ans2+= kind[i];
for(int i=l; i<=b[pl].r; i++) ans1+=c[i], ans2+= c[i]>;
for(int i=b[pr].l; i<=r; i++) ans1+=c[i], ans2+= c[i]>;
}
return MP(ans1, ans2);
}
}B; struct meow{
int l, r, x, y, qid;
bool operator <(const meow &a) const{return pos[l]==pos[a.l] ? r<a.r : pos[l]<pos[a.l];}
}q[M];
pii ans[M];
void modui(){
int l=, r=;
for(int i=; i<=Q; i++){
while(r<q[i].r) r++, B.add(a[r]);
while(r>q[i].r) B.del(a[r]), r--;
while(l<q[i].l) B.del(a[l]), l++;
while(l>q[i].l) l--, B.add(a[l]);
ans[q[i].qid]= B.que(q[i].x, q[i].y);
}
} int main() {
freopen("in","r",stdin);
n=read(); Q=read();
for(int i=; i<=n; i++) a[i]=read();
for(int i=; i<=Q; i++) l=read(), r=read(), x=read(), y=read(), q[i]=(meow){l, r, x, y, i};
B.ini();
sort(q+, q++Q);
modui();
for(int i=; i<=Q; i++) printf("%d %d\n",ans[i].fir, ans[i].sec);
}
BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]的更多相关文章
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
- bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...
- bzoj 3809 Gty的二逼妹子序列——莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...
随机推荐
- HDU--2024
C语言合法标识符 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- CSS3动画属性和flex弹性布局各个属性
[CSS3动画的使用] 1.声明一个关键帧(动画): @keynames name{ from{} to{} } 每个阶段的写法: ①可以直接使用from-to的写法 ②可以设置0%-100%的写法, ...
- java实现死锁的demo
死锁 只有当t1线程占用o1且正好也需要o2,t2此时占用o2且正好也需要o1的时候才会出现死锁,(类似于2个人拿着两个筷子吃饭,都是需要对方的一根筷子才能吃) 以下代码t1线程占用o1,并且获取到o ...
- Spider_Man_3 の selenium
一:介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作, ...
- Tomcat学习笔记(二)—— 一个简单的Servlet容器
1.简介:Servlet编程是通过javax.Servlet和javax.servlet.http这两个包的类和接口实现的,其中javax.servlet.Servlet接口至关重要,所有的Servl ...
- POJ 3278 Catch That Cow(模板——BFS)
题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...
- light oj 1184 Marriage Media
题目: You run a marriage media. You take some profiles for men and women, and your task is to arrange ...
- Screen命令安装使用教程
在安装lnmp之前,我们一般先运行一下Screen程序,因为screen好像一个容器一样,把lnmp的安装过程保护了起来.以CentOS中安装lnmp为例,程序下载.编译都需要比较长的时间,如果中途遇 ...
- NGINX 配置404错误页面转向
什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...
- 从#65279字符看dede模板页面编码问题
今天一位朋友让帮忙给解决一个dede模板的问题,问题主要是:模板文件生成html文件之后会在body开头处加入一个可见的控制符,导致页面头部会出现一个空白行. 接到"& ...