二次联通门 : 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的更多相关文章

  1. 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 ...

  2. CodeForces 220B(B. Little Elephant and Array)

    http://codeforces.com/contest/220/problem/B 题意:给出一个数组,给出m组询问,问区间中出现a[i] 次的有多少个. sl: 很显然的离线问题了. 大视野菜花 ...

  3. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  4. AC日记——Little Elephant and Array codeforces 221d

    221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...

  5. 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 ...

  6. 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)) 在 ...

  7. Little Elephant and Array CodeForces - 220B (莫队)

    The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...

  8. 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 ...

  9. codeforces 220B . Little Elephant and Array 莫队+离散化

    传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...

随机推荐

  1. Codeforces Round #588 (Div. 1) 简要题解

    1. 1229A Marcin and Training Camp 大意: 给定$n$个对$(a_i,b_i)$, 要求选出一个集合, 使得不存在一个元素好于集合中其他所有元素. 若$a_i$的二进制 ...

  2. Codeforces Round #563 Div. 2

    A:显然排序即可. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 100000 ...

  3. springboot中将日志信息存放在catalina.base中

    <?xml version="1.0" encoding="UTF-8"?> <configuration debug="true& ...

  4. DropDownList下拉控件

    <asp:DropDownList    ID="DropDownList1" runat="server" Width="177px" ...

  5. Linux Wireless Supported Devices

    Linux Wireless Supported Devices https://ark.intel.com/content/www/us/en/ark/products/series/59484/i ...

  6. 0.b概述

    一.计算机与算法 计算 = 信息处理 计算模型 = 计算机 = 信息处理工具 算法:特定计算模型下,解决特定问题的指令序列  要素:输入 输出 正确性 确定性 可行性 有穷性 好算法:正确 健壮 可读 ...

  7. pdm文件打开方式

    转自:https://blog.csdn.net/qq_36855191/article/details/79299216 pdm打开网站:http://www.dmanywhere.cn/

  8. SDcms1.8代码审计

    由于工作原因,分析了很多的cms也都写过文章,不过觉得好像没什么骚操作都是网上的基本操作,所以也就没发表在网站上,都保存在本地.最近突然发现自己博客中实战的东西太少了,决定将以前写的一些文章搬过来,由 ...

  9. pip3升级问题

    输入命令sudo pip3 install --upgrade pip 升级完成之后执行pip命令会报错,错误信息如下: File "/usr/bin/pip3", line 9, ...

  10. 【leetcode】544. Output Contest Matches

    原题 During the NBA playoffs, we always arrange the rather strong team to play with the rather weak te ...