AC日记——Little Elephant and Array codeforces 221d
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的更多相关文章
- AC日记——Little Elephant and Shifts codeforces 221e
E - Little Elephant and Shifts 思路: 一次函数线段树(疯狂debug): b不断循环左移,判断每次最小的|i-j|,a[i]=b[j]: 仔细观察发现,每个bi移动时, ...
- AC日记——Little Elephant and Numbers codeforces 221b
221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...
- AC日记——Little Elephant and Function codeforces 221a
A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...
- AC日记——Little Elephant and Problem codeforces 221c
221C 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
- Little Elephant and Array CodeForces - 220B(莫队)
给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdi ...
- AC日记——Sagheer and Nubian Market codeforces 812c
C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...
- AC日记——Red and Blue Balls codeforces 399b
399B - Red and Blue Balls 思路: 惊讶的发现,所有的蓝球的消除都是独立的: 对于在栈中深度为i的蓝球消除需要2^i次操作: 代码: #include <cstdio&g ...
- AC日记——Andryusha and Colored Balloons codeforces 780c
C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...
随机推荐
- javaScript对SEO的影响
在两大搜索引擎阵营中,大量实践证明百度对JAVASCRIP的处理很不理想而GOOGLE的处理要好一些. 网页中出现大量的JavaScript会给搜索引擎爬行增加难度.其主要影响有以下几点: 1.干 ...
- Python基础——安装运行
Python是如何运行的? 像绝大多数编程语言一样,要在计算机上能够运行python程序,至少需要安装一个最小的Python包:一个Python解释器和支持的库. 安装Python 安装包下载:htt ...
- 关于main与wmain函数
最近写一个控制台程序,并且希望该控制台程序运行时不显示控制台窗口,于是在程序include语句下面加入如下代码 #pragma comment (linker,"/subsystem:\&q ...
- js 全局变量和局部变量
Javascript在执行前会对整个脚本文件的声明部分做完整分析(包括局部变量),但是不能对变量定义做提前解析 1.作用域和作用域链 2.变量声明提前
- 基于TensorFlow的循环神经网络(RNN)
RNN适用场景 循环神经网络(Recurrent Neural Network)适合处理和预测时序数据 RNN的特点 RNN的隐藏层之间的节点是有连接的,他的输入是输入层的输出向量.extend(上一 ...
- c++知识点总结--静态与动态类型
对象的静态类型(static type),就是它在程序中被声明时所采用的类型 对象的动态类型(dynamic type)是指“目前所指对象的类型” vitual 函数是动态绑定而来,调用一个vir ...
- 原始套接字--简易ping程序
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> ...
- 最小生成树 $Kruskal$ 算法
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; int h[maxn], v[maxn], ...
- PHP7 深入理解
读 PHP7的内核剖析 php7-internal 记录 3 PHP的相关组成 3.1 SAPI php常见的四种运行模式 SAPI:Server Application Programming In ...
- 移动平台自动化测试:appium(二)
环境搭建.本环境基于win7_x64搭建 安装环境需要用到的工具清单: android sdk:https://developer.android.com/studio/index.html Appi ...