cf221 D. Little Elephant and Array
题解真的是太神奇了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的更多相关文章
- 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 ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- CodeForces 221D Little Elephant and Array
Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...
- 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 ...
- 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)) 在 ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- 『题解』Codeforces220B Little Elephant and Array
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description The Little Elephant loves playing with ...
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
- 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 ...
随机推荐
- [mjpeg @ ...] unable to decode APP fields: Invalid data found when processing input
通过FFmpeg打开自己笔记本摄像头(HP Wide Vision HD Camera)操作时遇到如下错误: [mjpeg @ 0000029be7cbd000] unable to decode A ...
- HTML5画的简单时钟
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 设计模式课程 设计模式精讲 9-2 原型模式coding
1 课堂演练 1.1 super.toString 作用 1.2 为什么要使用克隆方法呢 2 代码解析 2.1 代码解析1(使用原型模式之前) 2.2 代码解析2(使用原型模式默认方式(浅克隆)) 2 ...
- Pytorch 初次使用nn包
计算图和autograd是十分强大的工具,可以定义复杂的操作并自动求导:然而对于大规模的网络,autograd太过于底层. 在构建神经网络时,我们经常考虑将计算安排成层,其中一些具有可学习的参数,它们 ...
- 标准模板库中的链表(list)
//C++数据结构与算法(第4版) Adam Drozdek 著 徐丹 吴伟敏<<清华大学出版社>> 头文件:include<list> list() 创建一个 ...
- hadoop 第一个 mapreduce 程序(对MapReduce的几种固定代码的理解)
1.2MapReduce 和 HDFS 是如何工作的 MapReduce 其实是两部分,先是 Map 过程,然后是 Reduce 过程.从词频计算来说,假设某个文件块里的一行文字是”Thisis a ...
- Jquery实现下拉tab切换
//需求:分别点击html,css,js元素时展示对应的列表内容,其他列表内容要自动收起来 //代码如下 <!DOCTYPE html> <html lang="en&qu ...
- 1_01_MSSQL课程_基础入门2
1.数据库的迁移方案 ->分离 附加 ->权限问题: ->启用Administrator账号 ->把数据库文件放到默认的数据库文件存放目录. ->数据库文件兼容级别,设置 ...
- Windows编程常用api
转载网络 黑客常用WIN API函数整理 一.进程 创建进程: CreateProcess (,,,,,,,&si,&pi); WinExec("notepad", ...
- api文档方法参数
in型参数,带信息进去用: out型参数,方法执行结束,带着信息出来 如: CreateProcessW( _In_opt_ LPCWSTR lpApplicationName, _I ...