题解真的是太神奇了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. Centos7 mariadb (mysql)主从复制实现

    一.mysql基本命令 .启动mysql systemctl start mariadb .linux客户端连接自己 mysql -uroot -p -h 127.0.0.1 .远程链接mysql服务 ...

  2. Python 之网络编程之socket(2)黏包现象和socketserver并发

    一:黏包 ###tcp协议在发送数据时,会出现黏包现象.     (1)数据粘包是因为在客户端/服务器端都会有一个数据缓冲区,     缓冲区用来临时保存数据,为了保证能够完整的接收到数据,因此缓冲区 ...

  3. 新闻网大数据实时分析可视化系统项目——4、Zookeeper分布式集群部署

    ZooKeeper 是一个针对大型分布式系统的可靠协调系统:它提供的功能包括:配置维护.名字服务.分布式同步.组服务等: 它的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效.功能稳定的 ...

  4. JDBC原理及常见错误分析

    1.JDBC:Java DataBase Connectivity 可以为多种关系型数据库DBMS 提供统一的访问方式,用Java来操作数据库 2.JDBC API 主要功能: 三件事,具体是通过以下 ...

  5. String类与StringBuffer类

    String类与StringBuffer类   一.String类和StringBuffer类的区别 String类是不可变类,新建的对象为不可变对象(String类的内容和长度是固定的),一旦被创建 ...

  6. js求两个整数的百分比

    function GetPercent(num, total) {                          num = parseFloat(num);                   ...

  7. MyEclipse插件github安装使用

    MyEclipse插件github安装使用   网络上的介绍一堆堆的,但是自己尝试了下,发现问题很多,就动手做个教程. 大纲 1.git客户端安装 2.ssh配置 3.egit安装配置 4.参考资料 ...

  8. Jquery动画效果设置大全

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

  9. 【QSBOJ】字符串编辑

    题目链接:https://bbs.csdn.net/topics/390289884?page=1 AC代码: #include<bits/stdc++.h> using namespac ...

  10. 设备树DTS 学习:3-常用的DTS 函数

    Linux内核中目前DTS相关的函数都是以of_前缀开头的,它们的实现位于内核源码的drivers/of下面 void __iomem*of_iomap(struct device_node *nod ...