用莫队比直接做快了很多。。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#include<algorithm>
#define maxn 100005
int a[maxn],b[maxn],c[maxn],num[maxn];//原始数组,排序后的数组,离散后的数组,离散后的数组中元素出现的次数
int s;//块长度
struct Query{
int l,r,id;
bool operator<(const Query &a)const{
if(l/s==a.l/s) return r<a.r;
return l/s<a.l/s;
}
}q[maxn];
int ans[maxn];
int main(){
int n,m;
while(scanf("%d%d",&n,&m)==){
s=(int)sqrt(1.0*n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b+n+);
for(int i=;i<=n;i++)
c[i]=lower_bound(b+,b++n,a[i])-b;
for(int i=;i<=m;i++)
scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
sort(q+,q+m+);
//处理每个询问
int l=,r=,tmp=;
for(int i=;i<=m;i++){
while(r<q[i].r){//右边界拓展
r++;
if(num[c[r]]==a[r]-) tmp++;
else if(num[c[r]]==a[r]) tmp--;
num[c[r]]++;
}
while(r>q[i].r){//右边界往回缩
if(num[c[r]]==a[r]) tmp--;
else if(num[c[r]]==a[r]+) tmp++;
num[c[r]]--;
r--;
}
while(l<q[i].l){//左边界往回缩
if(num[c[l]]==a[l]) tmp--;
else if(num[c[l]]==a[l]+) tmp++;
num[c[l]]--;
l++;
}
while(l>q[i].l){//左边界拓展
l--;
if(num[c[l]]==a[l]-) tmp++;
else if(num[c[l]]==a[l]) tmp--;
num[c[l]]++;
}
ans[q[i].id]=tmp;
}
for(int i=;i<=m;i++)
printf("%d\n",ans[i]);
}
return ;
}

cf220B莫队的更多相关文章

  1. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  2. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  3. 【填坑向】bzoj2038小Z的袜子 莫队

    学莫队必做题,,,但是懒得写.今天来填个坑 莫队水题 莫队实际上就是按一个玄学顺序来离线计算询问,保证复杂度只会多一个n1/2,感觉是玄学(离线算法都很玄学) 易错点:要开long long(卡我半天 ...

  4. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  5. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  6. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  7. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  8. 【BZOJ 3735】苹果树 树上莫队(树分块+离线莫队+鬼畜的压行)

    2016-05-09 UPD:学习了新的DFS序列分块,然后发现这个东西是战术核导弹?反正比下面的树分块不知道要快到哪里去了 #include<cmath> #include<cst ...

  9. 【BZOJ 2038】【2009 国家集训队】小Z的袜子(hose) 分块+莫队

    $SDOI2016Day-1$临时抱佛脚学习一下莫队算法$233$ 我预感到自己省选要爆0hhh #include<cmath> #include<cstdio> #inclu ...

随机推荐

  1. location的三种连接方式和区别

    location.href是一个属性,要这样使用:location.href='http://www.example.com'而location.assign('http://www.example. ...

  2. Python Machine Learning-Chapter4

    Chapter4 Building Good Training Sets – Data Preprocessing 4.1 Dealing with missing data 如何判断数据框内的数据是 ...

  3. mysql表备份的一种方式

    前提: 最近测试环境shop表经常出现表损坏不能正常查询,通过mysql的修复方法,暂时有效.故做该表的迁移操作. 思路是: 停止数据库的操作 1.备份老表.  2.创建一个结构一样的表. 3.将老表 ...

  4. UVALive - 7637 E - Balanced String(构造)

    原题链接 题意:给出一个打乱顺序的序列,问是否能构造出一个括号匹配的字符串.每个数字为此前读取到的左括号数减去右括号数. 分析:有左括号开始构造,不够的话就找右括号.注意特殊情况待处理.详情看代码 # ...

  5. awk词频统计功能

    [root@test88 ~]# vim word_freq.sh #!/bin/bash if [ $# -ne 1 ];then echo "Usage: $0 filename&quo ...

  6. C#实现office文档转换为PDF格式

    1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址   OfficeSave ...

  7. js sort方法根据数组中对象的某一个属性值进行排序

    sort方法接收一个函数作为参数,这里嵌套一层函数用来接收对象属性名,其他部分代码与正常使用sort方法相同. var arr = [ {name:'zopp',age:0}, {name:'gpp' ...

  8. 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

    在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化 ...

  9. MySQL内连接、外连接、交叉连接

    外连接: 左连接:left join 或 left outer join 以左边的表为基准,如果左表有数据,而右表没有数据,左表的数据正常显示,右表数据显示为空. 创建user表,用于记录用户 use ...

  10. ASP.NET MVC + EF 更新的几种方式(超赞)

    1.常用 db.Entry(实体).State = EntityState.Modified;db.SaveChanges(); 2.指定更新 db.Configuration.ValidateOnS ...