3809: Gty的二逼妹子序列

链接

分析:

  和这道AHOI2013 作业差不多。权值是1~n的,所以对权值进行分块。$O(1)$修改,$O(\sqrt n)$查询。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int bel[N], sum[N], a[N], Ans[N * ], cnt[N], B;
struct Que{
int l, r, a, b, id;
bool operator < (const Que &A) const {
return bel[l] == bel[A.l] ? r < A.r : bel[l] < bel[A.l];
}
}Q[N * ]; inline void add(int x) {
cnt[x] ++;
if (cnt[x] == ) sum[bel[x]] ++;
}
inline void del(int x) {
cnt[x] --;
if (cnt[x] == ) sum[bel[x]] --;
}
inline int query(int l,int r) {
int res = ;
for (int i = l, lim = min(r, bel[l] * B); i <= lim; ++i) res += (cnt[i] > );
if (bel[l] != bel[r])
for (int i = (bel[r] - ) * B + ; i <= r; ++i) res += (cnt[i] > );
for (int i = bel[l] + ; i <= bel[r] - ; ++i) res += sum[i];
return res;
}
int main() {
int n = read(), m = read(); B = sqrt(n);
for (int i = ; i <= n; ++i) a[i] = read();
for (int i = ; i <= m; ++i)
Q[i].l = read(), Q[i].r = read(), Q[i].a = read(), Q[i].b = read(), Q[i].id = i;
for (int i = ; i <= n; ++i) bel[i] = (i - ) / B + ;
sort(Q + , Q + m + );
int L = , R = ;
for (int i = ; i <= m; ++i) {
while (L > Q[i].l) add(a[--L]);
while (R < Q[i].r) add(a[++R]);
while (L < Q[i].l) del(a[L++]);
while (R > Q[i].r) del(a[R--]);
Ans[Q[i].id] = query(Q[i].a, Q[i].b);
}
for (int i = ; i <= m; ++i) printf("%d\n",Ans[i]);
return ;
}

3809: Gty的二逼妹子序列的更多相关文章

  1. BZOJ 3809: Gty的二逼妹子序列

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1387  Solved: 400[Submit][Status][Di ...

  2. Bzoj 3809: Gty的二逼妹子序列 莫队,分块

    3809: Gty的二逼妹子序列 Time Limit: 35 Sec  Memory Limit: 28 MBSubmit: 868  Solved: 234[Submit][Status][Dis ...

  3. 【BZOJ 3809】 3809: Gty的二逼妹子序列 (莫队+分块)

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1728  Solved: 513 Description Autumn ...

  4. BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块

    Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...

  5. BZOJ 3809 Gty的二逼妹子序列(莫队+分块)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...

  6. 【BZOJ】3809: Gty的二逼妹子序列

    http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题意:n个元素(1<=n<=100000)每个元素有一权值<=n.q个询问, ...

  7. bzoj 3809 Gty的二逼妹子序列——莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...

  8. bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...

  9. bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)

    [题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...

随机推荐

  1. JavaScript的事件对象中的特殊属性和方法(鼠标,键盘)

    鼠标操作导致的事件对象中的特殊属性和方法 鼠标事件是 Web 上面最常用的一类事件,毕竟鼠标还是最主要的定位设备.那么通过事件对象可以获取到鼠标按钮信息和屏幕坐标获取等 鼠标按钮 只有在主鼠标按钮被单 ...

  2. android中反射技术使用实例

    在计算机科学领域.反射是指一类应用,它们能够自描写叙述和自控制.也就是说,这类应用通过採用某种机制来实现对自己行为的描写叙述(self-representation)和监測(examination), ...

  3. python 打包文件

    tarfile import tarfile tar = tarfile.open("sk_camera_6018.tar","w") tar.add(full ...

  4. 当网页失去焦点时改变网页的title值

    <script> document.addEventListener('visibilitychange', function() { var isHidden = document.hi ...

  5. 2019.3.6 Github学习 &Git学习

    Github学习 1.使用Github 1.1 目的 借助github托管项目代码 1.2 基本概念 仓库(Repository) 仓库的意思是:即你的项目,你想在Github上开源一个项目,那就必须 ...

  6. LayIM.AspNetCore Middleware 开发日记(四)主角登场(LayIM介绍)

    前言 在前几篇中已经初步介绍了开发AspNetCore中间件的一些基础知识,不过都没有很深入的去研究,后续还是需要去看看源码.本篇呢,终于有点开头的味道了,就是要介绍LayIM了,其实标题写的是主角, ...

  7. AOP各种的实现

    1 AOP各种的实现 AOP就是面向切面编程,我们可以从几个层面来实现AOP. 在编译器修改源代码,在运行期字节码加载前修改字节码或字节码加载后动态创建代理类的字节码,以下是各种实现机制的比较. 类别 ...

  8. nginx下配置多个web服务

    参考 nginx配置详解  nginx反向代理与负载均衡详解 一.nginx简介: Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能 ...

  9. docker安装后启动不了 解决方法

    第一种情况: 可能是你的selinux没有disabled,导致了守护docker的deamon进程启动不了 查看系统日志如果发现下列信息: May 7 12:34:14 localhost dock ...

  10. SPOJ 4487. Can you answer these queries VI splay

    题目链接:点击打开链接 题意比較明显,不赘述. 删除时能够把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且仅仅有i这一个 点 #include<stdio.h> #in ...