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内有多少个数满足其值为其出现的次数 题解: ...
随机推荐
- adb连接安卓设备的2种方式
一.usb连接 安卓设备打开开发者模式,启用usb调试 CMD窗口输入adb devices,此时可以看到自己的设备 PS:无法看到自己设备时,查看手机USB调试是否打开:PC端是否安装手机驱动. 二 ...
- C#进阶系列——WebApi 传参详解
本篇打算通过get.post.put.delete四种请求方式分别谈谈基础类型(包括int/string/datetime等).实体.数组等类型的参数如何传递. 回到顶部 一.get请求 对于取数据, ...
- springboot中将日志信息存放在catalina.base中
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="true& ...
- Java Comparable与Comparator区别
1,两种接口的展示 下面的程序是两个类各自实现了Comparable接口.Comparator接口 package com.cnblogs.mufasa.Solution; import java.u ...
- C# EF 加密连接数据库连接字符串
不多说,直接上代码 public partial class Model1 : DbContext { private static string connStr = ""; pu ...
- JS实现倒计时效果,并退出系统
背景:由于单点登录后,一直在本系统操作,可是门户体统的会话失效时间有30分钟,所以30分钟后,需要重新登录系统才可以进行操作. 方法:想过在本系统中的每个操作都先跟门户系统进行交互,渠道refresh ...
- Python opencv提取视频中的图片
作者:R语言和Python学堂链接:https://www.jianshu.com/p/e3c04d4fb5f3 这个函数就是本文要介绍的video2frames()函数,功能就是从视频中提取图片,名 ...
- linux驱动开发随手记【1】
1.一个patch只完成一项任务 2.用vim编辑多个文件,想同时关闭所有文件时候,可以用 :qall 命令 .vim用户手册08 3.mmc dev 0.mmc为U-boot命令,这句话的含义是设置 ...
- windows上安装python虚拟环境
一.windows上安装python虚拟环境 1.安装pip install virtualenvvirtualenv --version 2.新建一个python虚拟环境virtual_env_01 ...
- Apache老版本下载地址
有时候我们想做测试或者使用旧版本软件时,往往发现官方网站已经下架,以Apache为例: Apache旧版本下载地址:http://archive.apache.org/dist/httpd/ .... ...