221D - Little Elephant and Array

思路:

  莫队;

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005 struct QueryType {
int l,r,id;
};
struct QueryType qu[maxn]; int n,m,ai[maxn],bi[maxn],ans[maxn],ci[maxn],num[maxn];
int size,now,bel[maxn],blo; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} bool cmp(QueryType aa,QueryType bb)
{
if(bel[aa.l]==bel[bb.l])
{
if(bel[aa.r]==bel[bb.r])
{
if(aa.l==bb.l) return aa.r<bb.r;
else return aa.l<bb.l;
}
else return bel[aa.r]<bel[bb.r];
}
else return bel[aa.l]<bel[bb.l];
} inline void updata(int x,int dis)
{
if(num[ai[x]]==ci[x]) now--;
num[ai[x]]+=dis;
if(num[ai[x]]==ci[x]) now++;
} int main()
{
in(n),in(m);
for(int i=;i<=n;i++) in(ai[i]),bi[i]=ai[i],ci[i]=ai[i];
sort(bi+,bi+n+),size=unique(bi+,bi+n+)-bi-;
for(int i=;i<=n;i++) ai[i]=lower_bound(bi+,bi+size+,ai[i])-bi;
blo=sqrt(n);for(int i=;i<=n;i++) bel[i]=(i+)/blo;
for(int i=;i<=m;i++) in(qu[i].l),in(qu[i].r),qu[i].id=i;
sort(qu+,qu+m+,cmp);int l=,r=;
for(int no=;no<=m;no++)
{
int ll=qu[no].l,rr=qu[no].r;
if(l<ll) for(int i=l;i<ll;i++) updata(i,-);
else for(int i=l-;i>=ll;i--) updata(i,);
if(r<rr) for(int i=r+;i<=rr;i++) updata(i,);
else for(int i=r;i>rr;i--) updata(i,-);
l=ll,r=rr,ans[qu[no].id]=now;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
return ;
}

AC日记——Little Elephant and Array codeforces 221d的更多相关文章

  1. AC日记——Little Elephant and Shifts codeforces 221e

    E - Little Elephant and Shifts 思路: 一次函数线段树(疯狂debug): b不断循环左移,判断每次最小的|i-j|,a[i]=b[j]: 仔细观察发现,每个bi移动时, ...

  2. AC日记——Little Elephant and Numbers codeforces 221b

    221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...

  3. AC日记——Little Elephant and Function codeforces 221a

    A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...

  4. AC日记——Little Elephant and Problem codeforces 221c

    221C 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  5. Little Elephant and Array CodeForces - 220B (莫队)

    The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...

  6. Little Elephant and Array CodeForces - 220B(莫队)

    给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdi ...

  7. AC日记——Sagheer and Nubian Market codeforces 812c

    C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...

  8. AC日记——Red and Blue Balls codeforces 399b

    399B - Red and Blue Balls 思路: 惊讶的发现,所有的蓝球的消除都是独立的: 对于在栈中深度为i的蓝球消除需要2^i次操作: 代码: #include <cstdio&g ...

  9. AC日记——Andryusha and Colored Balloons codeforces 780c

    C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. VS Extension+NVelocity系列(二)——让VS支持 NVelocity的智能提示(上)

    一.基础概念 应该庆幸的是,VS的插件是靠着MEF实现而不是MAF,这让你所做的工作减轻了许多.如果在这之前,您已经了解了MEF的原理,我想对于VS插件的编写,您应该是很容易就能理解的.看看几个VS2 ...

  2. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目5

    2014-03-20 03:23 题目:给定一个字符串,输出其全排列. 解法:可以调用STL提供的next_permutation(),也可以自己写一个.对于这种看起来简单的题目,应该在能优化的地方, ...

  3. Windows10使用pip安装python包时报错-UnicodeDecodeError: 'ascii' codec c

    本人是Windows10,用的方法2解决的 原文链接http://blog.csdn.net/all_over_servlet/article/details/45112221 先交待下开发环境: 操 ...

  4. JMeter学习笔记(五) 文件上传接口测试

    此次测试的是上传图片接口,我把测试情况整理了一下,其他的上传文件接口都类似. 1.我通过jmeter的录制功能获取到了接口地址以及相关参数,如果有接口文档就会方便很多,此步骤就不多做说明了 2.因为上 ...

  5. Linux - Shell - 常用方法 - 备忘录

    $? 上一个指令的返回值 =成功,=失败 dmesg 检测系统开机启动信息 $() 对命令的替换,同`` ${} 对变量的替换,同$var $(()) 对内部内容进行整数运算 i= grep AAA ...

  6. eclipse里导入maven项目有红叉的解决办法

    导入maven的项目上有红叉,说明eclipse里maven的插件该更新了 1.help里选择install new software 2.点击add,输入name:MavenArchiver, lo ...

  7. [转载]GCC 编译使用动态链接库和静态链接库--及先后顺序----及环境变量设置总结

    来自http://blog.csdn.net/benpaobagzb/article/details/51364005 GCC 编译使用动态链接库和静态链接库 1 库的分类 根据链接时期的不同,库又有 ...

  8. java语言基础第二讲 课程作业02 总结

    一.编程的好习惯 1.注释习惯.单行注释://,  多行注释:/*   */ . 2.程序中添加适当的空白:4个空格. 3.文件名必须与公有类名一致,即public class 类名. 4.java中 ...

  9. jquery serialize() 方法

    ajax异步提交的时候,会使用该方法. 方法:jQuery ajax - serialize() 方法

  10. poj1936 假期计划第一水

    All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29651   Accepted: 12312 Desc ...