题解真的是太神奇了2333

用离线和树状数组(为什么感觉和HH的项链是的,什么鬼),比较巧妙的是他把整个数列分成几段。用一个vector来记录每个数出现的位置。一共就是data[a[i]][sz]---data[a[i]][sz-a[i]],data[a[i]][sz-a[i]]----data[a[i]][sz-a[i]-1],0----data[a[i]][sz-a[i]-1],第一个区间的数是1,第二个区间是-1,第三个是0,这样区间加减的话就直接出来了是不是成立一共有a[i]个a[i]而且,还可以往后递推,太神奇了2333

 #include<bits/stdc++.h>
#define lowbit(x) x&(-x)
#define LL long long
#define N 200005
#define M 1000005
#define mod 1000000007LL
#define inf 0x7ffffffff
using namespace std;
inline int ra()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>''){if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
int c[N],ans[N],a[N],n,m;
struct node{
int l,r,id;
bool operator < (const node t) {return r<t.r;}
}q[N];
void add(int i, int v)
{
while (i<=n)
{
c[i]+=v;
i+=lowbit(i);
}
}
int ask(int x)
{
int sum=;
while (x>)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
vector<int> data[N];
int main()
{
int sz; n=ra();m=ra();
for (int i=; i<=n; i++) a[i]=ra();
for (int i=; i<=m; i++)
{
q[i].l=ra(),q[i].r=ra();
q[i].id=i;
}
sort(q+,q+m+);
for (int i=,k=; i<=n; i++)
{
if (a[i]<=n)
{
data[a[i]].push_back(i);
sz=data[a[i]].size();
if (sz>=a[i])
{
add(data[a[i]][sz-a[i]],);
if (sz>a[i]) add(data[a[i]][sz-a[i]-],-);
if (sz>a[i]+) add(data[a[i]][sz-a[i]-],);
}
}
while (q[k].r==i && k<=m)
{
ans[q[k].id]=ask(q[k].r)-ask(q[k].l-);
k++;
}
for (int j=; j<=n; j++)
printf("%d ",ask(j));
cout<<endl;
}
for (int i=; i<=m; i++)
printf("%d\n",ans[i]);
return ;
}

cf221 D. Little Elephant and Array的更多相关文章

  1. Codeforces 221 D. Little Elephant and Array

    D. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

  2. AC日记——Little Elephant and Array codeforces 221d

    221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...

  3. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  4. Codeforces Round #136 (Div. 1) B. Little Elephant and Array

    B. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

  5. Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)

    vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...

  6. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  7. 『题解』Codeforces220B Little Elephant and Array

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description The Little Elephant loves playing with ...

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

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

  9. Codeforces 220B - Little Elephant and Array 离线树状数组

    This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will s ...

随机推荐

  1. 9.2.3 hadoop reduce端连接-分区分组聚合

    1.1.1         reduce端连接-分区分组聚合 reduce端连接则是利用了reduce的分区功能将stationid相同的分到同一个分区,在利用reduce的分组聚合功能,将同一个st ...

  2. Servlet对用户输入的数据进行读取

    逻辑代码: package com.zyb.test; import java.io.IOException; import java.util.Enumeration; import javax.s ...

  3. 学习Javascript的8张思维导图

    分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...

  4. 十四 Spring的AOP的基于AspectJ的注解开发

    Spring的AOP的基于AspectJ的注解开发 创建项目,引入jar包 编写目标类.切面类 配置目标类.切面类 在注解文件里开启AOP的开发 <?xml version="1.0& ...

  5. PHP的错误知识

    一.绪 PHP程序的错误发生一般归属于: 语法错误(会阻止脚本的执行) 运行时错误(不会阻止脚本的执行,但会组织脚本做希望它做的任何事情) 逻辑错误(不会阻止脚本执行,也不会显示错误消息) 二.开启错 ...

  6. 「Luogu P3820 小D的地下温泉」

    这道题的考点比较多. 前置芝士 BFS(DFS),这两种算法在这道题中并没有什么特别突出的地方,基本就是自己看心情写(本文以DFS为准,所以我心情是好是坏呢?) 连通块,可以将每一个温泉看作一个连通块 ...

  7. 解决Eclipse Debug 断点调试的source not found问题

    写完代码进行调试的时候,经常会用到断点调试,一步步检测问题,但有时候eclipse有时候无法进入断点,这样就失去了断点的意义,原因是debug无法找到该项目的源代码,解决方法如下 1,打开debug ...

  8. 「NOIP2009」靶形数独

    传送门 Luogu 解题思路 这题其实挺简单的. 首先要熟悉数独,我们应该要优先搜索限制条件多的行,也就是可能方案少的行,显然这样可以剪枝,然后再发挥一下dfs的基本功就可以了. 细节注意事项 爆搜题 ...

  9. Mac如何升级自带的vim

    brew install vim --with-lua --with-override-system-vi brew install macvim --with-lua --with-override ...

  10. Java 推荐读物与源代码阅读

    Java 推荐读物与源代码阅读                                                     江苏无锡  缪小东 1. Java语言基础     谈到Java ...