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内有多少个数满足其值为其出现的次数 题解: ...
随机推荐
- 天梯赛 L3-002. 堆栈
思路:这里的线段树维护一个区间里面出现数的个数,对于Pop,push单点更新一下就好. #include<stdio.h> #include<iostream> #includ ...
- C# 弹出层移动
groupPrint.MouseDown += GroupBox1_MouseDown; #region 弹出层移动 [System.Runtime.InteropServices.Dl ...
- Spring Boot 揭秘与实战之RabbitMQ
Spring Boot 整合 RabbitMQ Spring Boot 整合 RabbitMQ 是非常容易,只需要两个步骤. 首先,在 pom.xml 中增加 RabbitMQ 依赖. <dep ...
- ubuntu18.04 下启动Android Studio报错KVM is required to run this AVD. /dev/kvm device: permission denied.
在ubuntu18.04下安装Android Studio,安装了模拟器后运行报错 KVM is required to run this AVD. /dev/kvm device: permissi ...
- c# 子类的声明
- Vue框架之vuex的使用
1.首先需要在你的项目目录下安装vuex 终端命令: 2.在全局组件中导入与声明vuex 3.创建store实例对象 let store = new Vuex.store({ state:{ }, m ...
- Win10安装MySQL5.7版本 解压缩版方法
1.下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 直接点击下载项 下载后: 2.可以把解压的内容随便放到一个目录,我的是如 ...
- Pytohn笔记(31)----第三方包
摘自: https://www.jianshu.com/p/bbc8672a2d09 一. from __future__ import **** [版本更新之后想在原来的版本使用新版本的一些功能] ...
- 在angular项目中使用web-component ----How to use Web Components with Angular
原文: https://medium.com/@jorgecasar/how-to-use-web-components-with-angular-41412f0bced8 ------------- ...
- 操作socket笔记
网络编程 1.tcp协议 #tcpserver #单纯一对一发 tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建tcp套接字 参 ...