题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809

容易想到树状数组维护值域。但修改和查询都是 log 太慢。

考虑有 nsqrt(n) 个修改、m个查询,所以给查询 sqrt(n) ,给修改 O(1) 。对值域分块即可。(msqrt(n)也能过?)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1e5+,M=1e6+,K=;
int n,m,a[N],base,bh[N],cnt[N],sm[K],ans[M];
struct Ques{
int l,r,a,b,bh;
}q[M];
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
bool cmp(Ques u,Ques v){return bh[u.l]==bh[v.l]?u.r<v.r:bh[u.l]<bh[v.l];}
void add(int x)
{
if(!cnt[x])sm[bh[x]]++; cnt[x]++;
}
void del(int x)
{
cnt[x]--; if(!cnt[x])sm[bh[x]]--;
}
int query(int l,int r)
{
int ret=;
if(bh[r]-bh[l]<=)
{
for(int i=l;i<=r;i++)if(cnt[i])ret++;
return ret;
}
for(int i=bh[l]+;i<bh[r];i++) ret+=sm[i];
int L=base*bh[l],R=base*(bh[r]-);
for(int i=l;i<=L;i++)if(cnt[i])ret++;
for(int i=R+;i<=r;i++)if(cnt[i])ret++;
return ret;
}
int main()
{
n=rdn(); m=rdn(); base=sqrt(n);
for(int i=;i<=n;i++) a[i]=rdn(),bh[i]=(i-)/base+;
for(int i=;i<=m;i++)
q[i].l=rdn(),q[i].r=rdn(),q[i].a=rdn(),q[i].b=rdn(),q[i].bh=i;
sort(q+,q+m+,cmp);
int L=,R=;
for(int i=;i<=m;i++)
{
int l=q[i].l,r=q[i].r;
while(L>l)add(a[--L]);
while(R<r)add(a[++R]);
while(L<l)del(a[L++]);
while(R>r)del(a[R--]);
ans[q[i].bh]=query(q[i].a,q[i].b);
}
for(int i=;i<=m;i++)printf("%d\n",ans[i]);
return ;
}

bzoj 3809 Gty的二逼妹子序列——莫队+分块的更多相关文章

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

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

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

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

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

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

  4. [BZOJ3809]Gty的二逼妹子序列[莫队+分块]

    题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...

  5. 【BZOJ3809】Gty的二逼妹子序列 莫队 分块

    题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...

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

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

  7. [AHOI2013]作业 & Gty的二逼妹子序列 莫队

    ---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...

  8. [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...

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

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

随机推荐

  1. 向odoo贡献中文翻译

    建议通过 osc-git向odoo贡献中文翻译     osc-git 是指'开源中国'的git平台. 网址是 http://git.oschina.net/     注册osc-git 账号省略. ...

  2. Balanced Binary Tree——数是否是平衡,即任意节点左右字数高度差不超过1

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  3. ollydbg快速定位方便调试

    在ollydbg调试的时候,会看到大量的汇编代码(远多于源代码),代码中有大量的函数嵌套调用,调试起来周期很长,难度比较大. 所以我们希望能快速定位到代码,以下是快速定位的四种方法: 1.Goto命令 ...

  4. python(17)- 迭代器和生成器及应用

    什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2.可迭代对象 ...

  5. python(19)- 列表生成式和生成器表达式练习Ⅰ

    列表表达式 程序一: 常规写法: egg_list=[] for i in range(100): egg_list.append('egg%s' %i) print(egg_list) 列表表达式写 ...

  6. spl_autoload_register的使用

    class Loader{ static function loadClass($class) { $class = $class.'.php'; if(file_exists($class)) { ...

  7. 杭电 HDU 1279 验证角谷猜想

    验证角谷猜想 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  8. 1.新手上路:Windows下,配置Qt环境

    个人体会: 我最初只是想看看C++除了"黑窗口"之外,怎么才能做一些"更好看的东西".之后在网上看到有人推荐Qt,就看了一下官网(https://www.qt. ...

  9. vue directive demo

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 数据库MySQL经典面试题之SQL语句

    数据库MySQL经典面试题之SQL语句 1.需要数据库表1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学 ...