Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array
/*
Codeforces 221d D. Little Elephant and Array 题意 : 询问一段区间中出现次数等于自身的数的个数 正解是dp 莫队水过, 作为我莫队的入门题
myj的思路 66 把所有需查询的区间排序 当前查询区间的答案为上一个区间的答案通过多次的区间移动得出 */
#include <algorithm>
#include <cstdio>
#include <cmath> #define Max 100005 void read (int &now)
{
now = ;
register char word = getchar ();
bool temp = false;
while (word < '' || word > '')
{
if (word == '-')
temp = true;
word = getchar ();
}
while (word >= '' && word <= '')
{
now = now * + word - '';
word = getchar ();
}
if (temp)
now = -now;
} int N, M; int belong[Max];
struct Data
{
int l, r; int ID; bool operator < (const Data &now) const
{
if (belong[now.l] == belong[this->l])
return now.r > this->r;
return belong[now.l] > belong[this->l];
}
}; int number[Max]; int K_Size;
int count[Max];
int rank_[Max]; Data query[Max];
int Answer[Max], Result;
int pos[Max]; incount void Updata (int now, int key)
{
if (count[number[now]] == pos[now])
Result--;
count[number[now]] += key;
if (count[number[now]] == pos[now])
Result++;
} int main (int argc, char *argv[])
{
read (N);
read (M);
K_Size = sqrt (N);
for (int i = ; i <= N; i++)
{
read (number[i]);
rank_[i] = number[i];
pos[i] = number[i];
belong[i] = (i + ) / K_Size;
}
std :: sort (rank_ + , rank_ + + N);
int Size = std :: unique (rank_ + , rank_ + + N) - rank_ - ;
for (int i = ; i <= N; i++)
number[i] = std :: lower_bound (rank_ + , rank_ + + Size, number[i]) - rank_;
for (int i = ; i <= M; i++)
{
read (query[i].l);
read (query[i].r);
query[i].ID = i;
}
std :: sort (query + , query + + M);
int l = , r = ;
for (int cur = , now_1, now_2; cur <= M; cur++)
{
now_1 = query[cur].l;
now_2 = query[cur].r;
if (l < now_1)
for (int i = l; i < now_1; i++)
Updata (i, -);
else
for (int i = l - ; i >= now_1; i--)
Updata (i, );
if (r < now_2)
for (int i = r + ; i <= now_2; i++)
Updata (i, );
else
for (int i = r; i > now_2; i--)
Updata (i, -);
l = now_1;
r = now_2;
Answer[query[cur].ID] = Result;
}
for (int i = ; i <= M; i++)
printf ("%d\n", Answer[i]);
return ;
}
Codeforces 221d 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 ... 
- CodeForces 220B(B. Little Elephant and Array)
		http://codeforces.com/contest/220/problem/B 题意:给出一个数组,给出m组询问,问区间中出现a[i] 次的有多少个. sl: 很显然的离线问题了. 大视野菜花 ... 
- CodeForces 221D Little Elephant and Array
		Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ... 
- AC日记——Little Elephant and Array codeforces 221d
		221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ... 
- 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)) 在 ... 
- 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 ... 
- codeforces 220B . Little Elephant and Array 莫队+离散化
		传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ... 
随机推荐
- babel tsc webpack
			我要用啥?js的话:babel编译+webpack模块打包ts的话:tsc编译成js+babel编译+webpack模块打包浏览器情况:如果您的浏览器支持es6所有语法那么就可以只用webpack来处 ... 
- python练习:面向对象1
			面向对象习题: 一:定义一个学生类.有下面的类属性: 1 姓名 2 年龄 3 成绩(语文,数学,英语)[每课成绩的类型为整数] 类方法: 1 获取学生的姓名:get_name() 返回类型:str 2 ... 
- ubuntu下安装软件时报错解决:Unmet dependencies. Try 'apt-get -f install' with no packages
			在错误后面运行以下代码,补全依赖项: sudo apt-get -f install 
- 你听过稀疏数组(sparseArray)吗?
			稀疏数组(sparseArray) 基本介绍 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 1.记录数组一共有几行几列,有多少个不同的值 ... 
- 删除静态页面的html
			function dellist(obj) { $(obj).parent().parent().remove(); } 
- Intellij的Terminal框里输入npm无效
			Intellij的Terminal框里输入npm无效,解决办法: 1.安装node.js的时候选择全部安装: 2.在intellij的file->settings->Tools->T ... 
- 服务发现之consul理论整理_结合Docker+nginx+Tomcat简单部署案例
			目录 一.理论概述 服务发现的概念简述 consul简述 二.部署docker+consul+Nginx案例 环境 部署 三.测试 四.总结 一.理论概述 服务发现的概念简述 在以前使用的是,N台机器 ... 
- 有关Error during sbt execution: No Scala version specified or detected的解决方案--SBT
			sbt 全称为 Simple Build Tool,是 Scala 项目中的标准构建工具,类似于 Java 下的 Maven/Groovy 中的 Gradle. 项目的构建 项目依赖自动化管理 提供统 ... 
- linux网络编程之system v共享内存
			接着上次的共享内存继续学习,这次主要是学习system v共享内存的使用,下面继续: 跟消息队列一样,共享内存也是有自己的数据结构的,system v共享内存也是随内核持续的,也就是说当最后一个访问内 ... 
- Session&Cookie&localStorage浅谈
			Session&Cookie&localStorage 领导让我开发一个有两张信息表单需要提交页面的网站,我作为一名开发人员,这个需求太简单了,和领导说直接存session sessi ... 
