sjtu1364 countcountcount
Description
我有一个元素个数为\(n\)的整数数组\(a\)和\(Q\)个问题,每个问题有\(x,y\)两个参数,我要数有多少个整数\(K\)满足\(K\)在\(a[x]…a[y]\)中出现了恰好\(K\)次。
题面就这么简单了。
Input
第一行两个整数\(n,Q\),表示数组\(a\)的元素个数和询问数;
接下来一行n给整数,描述数组a;
接下来Q行,每行两个数xi,yi(1<=xi<=yi<=n),表示询问的左右边界;
Output
输出Q行,每行一个整数表示满足询问的K的个数。
Sample Input
7 2
3 1 2 2 3 3 7
1 7
3 4
Sample Output
3
1
Hint
对于30%的数据,\(1 \le n,Q \le 1000\);
对于100%的数据,\(1 \le n,Q \le 100000,1 \le a[i] \le 10^9\);
sb莫队题目。。。1个元素支持\(O(1)\)加入。
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define maxn (100010)
int N,tot,Q,A[maxn],num[maxn],ans[maxn],lim,size;
inline int gi()
{
char ch; int f = 1,ret = 0;
do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
if (ch == '-') f = -1,ch = getchar();
do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
return ret*f;
}
struct node
{
int l,r,id;
inline void read(int i) { l = gi(),r = gi(); id = i; lim = max(lim,r); }
}query[maxn];
inline bool cmp1(const node &a,const node &b) { return a.l < b.l; }
inline bool cmp2(const node &a,const node &b) { return a.r < b.r; }
inline void work()
{
int res = 0,L = query[1].l,R = query[1].r;
for (int i = L;i <= R;++i)
if (A[i] <= N)
{
if (num[A[i]] == A[i]) --res;
if (++num[A[i]] == A[i]) ++res;
}
for (int i = 1;i <= Q;++i)
{
while (R < query[i].r)
{
++R;
if (A[R] <= N)
{
if (num[A[R]] == A[R]) --res;
if (++num[A[R]] == A[R]) ++res;
}
}
while (R > query[i].r)
{
if (A[R] <= N)
{
if (num[A[R]] == A[R]) --res;
if (--num[A[R]] == A[R]) ++res;
}
--R;
}
while (L > query[i].l)
{
--L;
if (A[L] <= N)
{
if (num[A[L]] == A[L]) --res;
if (++num[A[L]] == A[L]) ++res;
}
}
while (L < query[i].l)
{
if (A[L] <= N)
{
if (num[A[L]] == A[L]) --res;
if (--num[A[L]] == A[L]) ++res;
}
++L;
}
ans[query[i].id] = res;
}
}
int main()
{
freopen("1364.in","r",stdin);
freopen("1364.out","w",stdout);
N = gi(); Q = gi();
for (int i = 1;i <= N;++i) A[i] = gi();
for (int i = 1;i <= Q;++i) query[i].read(i);
size = ceil(sqrt(lim)+0.5);
sort(query+1,query+Q+1,cmp1);
for (int i = 1,j;i <= Q;i = j+1)
{
for (j = i;j < Q&&query[j+1].l - query[i].l <= size;++j);
sort(query+i,query+j+1,cmp2);
}
work();
for (int i = 1;i <= Q;++i) printf("%d\n",ans[i]);
fclose(stdin); fclose(stdout);
return 0;
}
sjtu1364 countcountcount的更多相关文章
随机推荐
- [转]ORACLE 异常错误处理
本文转自:http://www.cnblogs.com/soundcode/archive/2012/01/10/2318385.html 本篇主要内容如下: 5.1 异常处理概念 5.1.1 预定义 ...
- [记录]java.math.biginteger cannot be cast to java.lang.long
可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...
- 【转】Android自动化测试之MonkeyRunner录制和回放脚本(四)
测试脚本录制: 方案一: 我们先看看以下monkeyrecoder.py脚本: #Usage: monkeyrunner recorder.py #recorder.py http://mirror ...
- Delphi - 在ListView中添加一个进度条
// 相关定义 Type TListData = Record FileName: String; Percent: Integer; End; PListData = ^TListData; // ...
- 忘记root口令的处理方法
最近在拜读<Unix & Linux 大学教程>这本书,这本书写得不错.关于Unix和Linux比较介绍比较深入根源,内容也比较详细,很值得去拜读. 在书中的附录中有一 ...
- 页面动态加载js文件
function loadJS(url, onload) { var domscript = document.createElement('script'); domscript.src = url ...
- android中的4种点击事件
四种点击事件 (1)采用内部类的方式去实现OnClickListener (2)匿名内部类 (3)当前类imp OnClickListener (4)onclick ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- PHP学习笔记——上传文件到服务端的文件夹下
环境 开发包:appserv-win32-2.5.10 服务器:Apache2.2 数据库:phpMyAdmin 语言:php5,java 平台:windows 10 需求 编写一个PHP脚本页面,可 ...
- 【nodemailer】之 work with mustache
之前对nodemailer做了简要的研究,基本上是搞定了发邮件的问题.但很多情况下邮件的内容不是固定的,然后又需要有一个合适的样式,这就需要使用模板了.nodemailer有nodemailer-ma ...