题解 CF220B 【Little Elephant and Array】
作者:互联网
Solution CF220B Little Elephant and Array
题目大意:给定一个序列,多次询问在 \([l,r]\) 内,有多少个数 \(x\),其出现次数也为 \(x\)。
暴力,奇妙的复杂度
分析:
首先可能成为答案的数字个数是 \(O(\sqrt{n})\) 的,然后大于 \(n\) 的数也是不可能成为答案的。因此我们直接暴力统计。
开个数组统计 \(x \leq n\) 的 \(x\) 的出现次数,记为 \(cnt[x]\),只有 \(x \leq cnt[x]\) 的 \(x\) 才可能为答案,离线一下就可以了。
#include <cstdio>
#include <cctype>
#pragma GCC optmize(2)
using namespace std;
typedef long long ll;
constexpr int maxn = 1e5 + 100,inf = 0x7fffffff;
struct IO{//-std=c++11,with cstdio and cctype
private:
static constexpr int ibufsiz = 1 << 20;
char ibuf[ibufsiz + 1],*inow = ibuf,*ied = ibuf;
static constexpr int obufsiz = 1 << 20;
char obuf[obufsiz + 1],*onow = obuf;
const char *oed = obuf + obufsiz;
public:
inline char getchar(){
#ifndef ONLINE_JUDGE
return ::getchar();
#else
if(inow == ied){
ied = ibuf + sizeof(char) * fread(ibuf,sizeof(char),ibufsiz,stdin);
*ied = '\0';
inow = ibuf;
}
return *inow++;
#endif
}
template<typename T>
inline void read(T &x){
static bool flg;flg = 0;
x = 0;char c = getchar();
while(!isdigit(c))flg = c == '-' ? 1 : flg,c = getchar();
while(isdigit(c))x = x * 10 + c - '0',c = getchar();
if(flg)x = -x;
}
template <typename T,typename ...Y>
inline void read(T &x,Y&... X){read(x);read(X...);}
inline int readi(){static int res;read(res);return res;}
inline long long readll(){static long long res;read(res);return res;}
inline void flush(){
fwrite(obuf,sizeof(char),onow - obuf,stdout);
fflush(stdout);
onow = obuf;
}
inline void putchar(char c){
#ifndef ONLINE_JUDGE
::putchar(c);
#else
*onow++ = c;
if(onow == oed){
fwrite(obuf,sizeof(char),obufsiz,stdout);
onow = obuf;
}
#endif
}
template <typename T>
inline void write(T x,char split = '\0'){
static unsigned char buf[64];
if(x < 0)putchar('-'),x = -x;
int p = 0;
do{
buf[++p] = x % 10;
x /= 10;
}while(x);
for(int i = p;i >= 1;i--)putchar(buf[i] + '0');
if(split != '\0')putchar(split);
}
inline void lf(){putchar('\n');}
~IO(){
fwrite(obuf,sizeof(char),onow - obuf,stdout);
}
}io;
template <typename A,typename B>
inline void chkmin(A &x,const B &y){if(y < x)x = y;}
template <typename A,typename B>
inline void chkmax(A &x,const B &y){if(y > x)x = y;}
int n,m,val[maxn],cnt[maxn],sum[maxn],ans[maxn],l[maxn],r[maxn];
int main(){
#ifndef ONLINE_JUDGE
freopen("fafa.in","r",stdin);
#endif
io.read(n,m);
for(int i = 1;i <= n;i++){
io.read(val[i]);
if(val[i] <= n)cnt[val[i]]++;
}
for(int i = 1;i <= m;i++)io.read(l[i],r[i]);
for(int i = 1;i <= n;i++)
if(i <= cnt[i]){
for(int k = 1;k <= n;k++)sum[k] = sum[k - 1] + (i == val[k]);
for(int k = 1;k <= m;k++)
if(sum[r[k]] - sum[l[k] - 1] == i)ans[k]++;
}
for(int i = 1;i <= m;i++)io.write(ans[i],'\n');
return 0;
}
标签:Little,int,题解,void,read,CF220B,maxn,inline,obuf 来源: https://www.cnblogs.com/colazcy/p/14084375.html