AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array
思路:
莫队;
代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005 struct QueryType {
int l,r,id;
};
struct QueryType qu[maxn]; int n,m,ai[maxn],bi[maxn],ans[maxn],ci[maxn],num[maxn];
int size,now,bel[maxn],blo; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} bool cmp(QueryType aa,QueryType bb)
{
if(bel[aa.l]==bel[bb.l])
{
if(bel[aa.r]==bel[bb.r])
{
if(aa.l==bb.l) return aa.r<bb.r;
else return aa.l<bb.l;
}
else return bel[aa.r]<bel[bb.r];
}
else return bel[aa.l]<bel[bb.l];
} inline void updata(int x,int dis)
{
if(num[ai[x]]==ci[x]) now--;
num[ai[x]]+=dis;
if(num[ai[x]]==ci[x]) now++;
} int main()
{
in(n),in(m);
for(int i=;i<=n;i++) in(ai[i]),bi[i]=ai[i],ci[i]=ai[i];
sort(bi+,bi+n+),size=unique(bi+,bi+n+)-bi-;
for(int i=;i<=n;i++) ai[i]=lower_bound(bi+,bi+size+,ai[i])-bi;
blo=sqrt(n);for(int i=;i<=n;i++) bel[i]=(i+)/blo;
for(int i=;i<=m;i++) in(qu[i].l),in(qu[i].r),qu[i].id=i;
sort(qu+,qu+m+,cmp);int l=,r=;
for(int no=;no<=m;no++)
{
int ll=qu[no].l,rr=qu[no].r;
if(l<ll) for(int i=l;i<ll;i++) updata(i,-);
else for(int i=l-;i>=ll;i--) updata(i,);
if(r<rr) for(int i=r+;i<=rr;i++) updata(i,);
else for(int i=r;i>rr;i--) updata(i,-);
l=ll,r=rr,ans[qu[no].id]=now;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
return ;
}
AC日记——Little Elephant and Array codeforces 221d的更多相关文章
- AC日记——Little Elephant and Shifts codeforces 221e
E - Little Elephant and Shifts 思路: 一次函数线段树(疯狂debug): b不断循环左移,判断每次最小的|i-j|,a[i]=b[j]: 仔细观察发现,每个bi移动时, ...
- AC日记——Little Elephant and Numbers codeforces 221b
221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...
- AC日记——Little Elephant and Function codeforces 221a
A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...
- AC日记——Little Elephant and Problem codeforces 221c
221C 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
- Little Elephant and Array CodeForces - 220B(莫队)
给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdi ...
- AC日记——Sagheer and Nubian Market codeforces 812c
C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...
- AC日记——Red and Blue Balls codeforces 399b
399B - Red and Blue Balls 思路: 惊讶的发现,所有的蓝球的消除都是独立的: 对于在栈中深度为i的蓝球消除需要2^i次操作: 代码: #include <cstdio&g ...
- AC日记——Andryusha and Colored Balloons codeforces 780c
C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...
随机推荐
- Docker容器 - 容器时间跟宿主机时间同步
在Docker容器创建好之后,可能会发现容器时间跟宿主机时间不一致,这就需要同步它们的时间,让容器时间跟宿主机时间保持一致. 转载自:https://www.cnblogs.com/kevingrac ...
- python学习笔记一:数据类型
一.Python文件类型 1.源代码 hello.py: 1 #!/usr/bin/python 2 print "hello world" 2.字节代码:python源文件经编译 ...
- leetcode 【 Intersection of Two Linked Lists 】python 实现
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- python 学习分享-socket编程
socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思. 通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟 ...
- linux socket c/s上传文件
这是上传文件的一个示例,可以参照自行修改成下载或者其它功能. 在上传时,需要先将文件名传到服务器端,这是采用一个结构体,包含文件名及文件名长度(可以用于校验),防止文件名乱码. client #inc ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- shell中的>&1和 >&2是什么意思?
当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出. 在 shell 程 ...
- 软考——(1)J2SE
我们先从Java说起,简单的说,Java是一种面向对象的程序设计语言,可跨平台使用. 与之前学习的程序设计语言相比,最值得一提的就是Java的两种核心机制:Java虚拟机和垃圾回收机制. 1)虚拟机 ...
- 【转】TCP通信的三次握手和四次撒手的详细流程(顿悟)
TCP(Transmission Control Protocol) 传输控制协议 三次握手 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位 ...
- Tomcat学习笔记(十二)
Host和Engine容器 Context容器的父容器通常是Host容器. Engine容器表示Catalina的整个servlet引擎.如果使用Engine容器,那么它总是处于容器层级的最顶层.默认 ...