zoukankan      html  css  js  c++  java
  • 题解 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 = '';
    					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 = ''){
    			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 != '')putchar(split);
    		}
    		inline void lf(){putchar('
    ');}
    		~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],'
    ');
    	return 0;
    }
    
  • 相关阅读:
    Python 安装Twisted 提示python version 2.7 required,which was not found in the registry
    Openfire Strophe开发中文乱码问题
    css div 垂直居中
    How to create custom methods for use in spring security expression language annotations
    How to check “hasRole” in Java Code with Spring Security?
    Android 显示/隐藏 应用图标
    Android 当媒体变更后,通知其他应用重新扫描
    文件上传那些事儿
    专题:点滴Javascript
    主流动画实现方式总结
  • 原文地址:https://www.cnblogs.com/colazcy/p/14084375.html
Copyright © 2011-2022 走看看