题解真的是太神奇了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. sigprocmask

    sigprocmask 检测和更改进程的信号屏蔽字 初始化信号屏蔽字的函数 sigprocempty--设置空的信号屏蔽字 sigprocfillset----设置全集的信号屏蔽字

  2. token和session的区别

    session和token都是用来保持会话,功能相同 一.session机制,原理 session是服务端存储的一个对象,主要用来存储所有访问过该服务端的客户端的用户信息(也可以存储其他信息),从而实 ...

  3. 夯实Java基础(十六)——枚举类的使用

    1.枚举类简介 枚举是仅容许特定数据类型值的有限集合.例如我们平时生活中星期一到星期日这七天就是一个特定的有限集合,一年四季的春夏秋冬也同样是的,它们都是枚举.枚举和我们数学中的集合非常相似,如果我们 ...

  4. Keras入门——(6)长短期记忆网络LSTM(三)

    参考: https://blog.csdn.net/u012735708/article/details/82769711 https://zybuluo.com/hanbingtao/note/58 ...

  5. RTL级低功耗设计

    重点:门控时钟和操作数隔离 1.并行与流水的选择 并行与流水都是用面积换速度,但是有时可以降低功耗 并行处理常用于数字信号处理部分,采用并行处理,也已降低系统工作频率从而降低功耗 用两个乘法器取代原设 ...

  6. Android程序的入口点是什么,不是Main()吗

    很多初入Android开发的网页可能不知道Android程序的入口点是什么,不是main()吗,当然我相信回复onCreate的在字面上不算错,但是你们想的是Activity中的onCreate 方法 ...

  7. (转)C#的 GC工作原理基础

    作为一位C++出身的C#程序员,我最初对垃圾收集(GC)抱有怀疑态度,怀疑它是否能够稳定高效的运作:而到了现在,我自己不得不说我已经逐渐习惯并依赖GC与我的程序“共同奔跑”了,对“delete”这个习 ...

  8. 在React中随机生成图形验证码

    各个方法 在输入框中定义一个位置存放图形 完整代码 方便复制粘贴 import React, { Component } from 'react'; import styles from './lef ...

  9. Jquery所有Dom操作汇总

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 本地连接 HDFS 报错 Exception in thread "main" org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security.AccessControlException: Permission denied: user=JM.H,access=WRITE, inode="":r

    此时 到hdfs机器下修改hdfs-site.xml即可 添加如下配置 <property> <name>dfs.permissions</name> <va ...