Codeforces Round #136 (Div. 2) D. Little Elephant and Array 线段树 离线处理

brother的PT-9800PCN标签打印机二次开发 本来想通过esc/p指令打印标签,但是无奈能找到的资料太少,而且打电话给客服,也没有sample可以提供参考,后来客服说使用template功能的话,打印很方便。于是下载手册学习了一下, 先下载P-touch Editor软件,制作标签模板。 制作的模板就是普通的标签,不需要连接数据库之类的,模板里有3个object,分别是文本1,文本2,条形码3。这个很关键,后面的代码传送数据,就是按这... 阅读详情

D. Little Elephant and Array
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let's denote the number with index i as ai.

Additionally the Little Elephant has m queries to the array, each query is characterised by a pair of integers lj and rj (1 ≤ lj ≤ rj ≤ n). For each query lj, rj the Little Elephant has to count, how many numbers x exist, such that number x occurs exactly x times among numbers alj, alj + 1, ..., arj.

Help the Little Elephant to count the answers to all queries.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the size of array a and the number of queries to it. The next line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 109). Next m lines contain descriptions of queries, one per line. The j-th of these lines contains the description of the j-th query as two space-separated integers lj and rj (1 ≤ lj ≤ rj ≤ n).

Output

In m lines print m integers — the answers to the queries. The j-th line should contain the answer to the j-th query.

Sample test(s)
Input
7 2
3 1 2 2 3 3 7
1 7
3 4
Output
3
1

  N个数M个询问l,r,输出区间[l,r]内出现次数和值相等的有多少个。

  记录下询问,按r从小到大排序。

  i从1到N循环,每次处理出r=i的询问。循环过程中用vector  V[val]记录val出现的每个位置,cnt[val]记录当前val出现的次数。

  如果cnt[val]>=val,说明从V[val][cnt[val]-val]这里到当前的i这个区间肯定是满足的,在线段树上把V[val][cnt[val]-val]这里加1,并且V[val][cnt[val]-val-1]以及之前的位置到i这个区间是不满足的,当前V[val][cnt[val]-val-1]是已经加过1的,V[val][cnt[val]-val]也加了1,但是V[val][cnt[val]-val-1]到i是不满足的,所以把V[val][cnt[val]-val-1]这个位置减2,相当于在V[val][cnt[val]-val-1]这里多减一个1,抵消V[val][cnt[val]-val]加的1。又因为V[val][cnt[val]-val-2]已经减过2,现在已经有V[val][cnt[val]-val-1]来抵消了,所以V[val][cnt[val]-val-2]不需要减2了,要再加上1。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define pi (4*atan(1.0))
typedef long long LL;
using namespace std;

const int MAXN=100010;
const int MAXNODE=4*MAXN;

int N,M,a[MAXN],ans[MAXN],cnt[MAXN];
vector<int> V[MAXN];

struct Query{
    int l,r,id;
    bool operator < (const Query& rhs) const{
        return r<rhs.r;
    }
};
vector<Query> pos;

struct SegmentTree{
    int sum[MAXNODE];
    void clear(){
        memset(sum,0,sizeof(sum));
    }
    void update(int o,int L,int R,int pos,int v){
        if(L>=R){
            sum[o]+=v;
            return;
        }
        int mid=L+(R-L)/2;
        if(pos<=mid) update(o<<1,L,mid,pos,v);
        else update(o<<1|1,mid+1,R,pos,v);
        sum[o]=sum[o<<1]+sum[o<<1|1];
    }
    int query(int o,int L,int R,int ql,int qr){
        if(ql<=L&&qr>=R) return sum[o];
        int mid=L+(R-L)/2,ret=0;
        if(ql<=mid) ret+=query(o<<1,L,mid,ql,qr);
        if(qr>mid) ret+=query(o<<1|1,mid+1,R,ql,qr);
        return ret;
    }
}tree;

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&N,&M)!=EOF){
        for(int i=1;i<=N;i++) scanf("%d",&a[i]);
        pos.clear();
        for(int i=1;i<=N;i++) V[i].clear();
        int l,r;
        for(int i=0;i<M;i++){
            scanf("%d%d",&l,&r);
            pos.push_back((Query){l,r,i});
        }
        sort(pos.begin(),pos.end());
        tree.clear();
        memset(cnt,0,sizeof(cnt));
        int k=0;
        for(int i=1;i<=N;i++){
            int val=a[i];
            if(val<=N){
                cnt[val]++;
                V[val].push_back(i);
                if(cnt[val]>=val){
                    tree.update(1,1,N,V[val][cnt[val]-val],1);
                    if(cnt[val]>val) tree.update(1,1,N,V[val][cnt[val]-val-1],-2);
                    if(cnt[val]>val+1) tree.update(1,1,N,V[val][cnt[val]-val-2],1);
                }
            }
            while(pos[k].r==i&&k<M){
                ans[pos[k].id]=tree.query(1,1,N,pos[k].l,pos[k].r);
                k++;
            }
        }
        for(int i=0;i<M;i++) printf("%d\n",ans[i]);
    }
    return 0;
}


Uni-App 使用极光推送完整指南 Uni-App集成极光推送的完整指南:从注册极光账号到代码实现。包含前期准备(获取AppKey、注册厂商账号)、Uni-App配置(插件导入、初始化代码)、高级功能(角标设置、Java后台接口)及常见问题排查。详细说明了推送监听、registrationID获取、消息接收处理等核心功能实现步骤,并提供两种推送接口示例(按平台推送和统一推送)。适合需要在Uni-App项目中实现推送功能的开发者参考。 阅读详情

相关推荐

OpenClaw 快速安装与本地应用实战 —— 每一步都有截图加详细说明,本地AI助手,让打工人也有下属

《OpenClaw 2026开源智能体框架10分钟快速部署指南》摘要:本文提供OpenClaw最新版全平台部署方案,重点解决三大核心问题:1)环境配置优化,通过一键脚本实现3-8分钟快速安装(含国内镜像加速方案);2)初始化配置详解,包含大模型绑定(智普AI/本地模型)与5个高频技能安装;3)实战演示文件自动整理任务。同时总结6类常见故障解决方案,涵盖权限、端口占用等典型问题,确保用户从安装到首个自动化任务落地全程无障碍。特别强调Windows需管理员权限、禁用中文路径等关键注意事项,实现"安装即

weixin_43538316的博客 816

Little Elephant and Array(CF-220B)

Problem Description The Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let's denote the number with index i as ai. Additionally t...

Alex_McAvoy的博客 468

【RL in VLA】VLA中的强化学习

本文提出了一种名为 Reinforcement Learning Distilled Generalist(RLDG) 的方法,通过强化学习生成高质量训练数据来微调机器人基础模型。通过在多个精确操作任务上的实验,我们证明了 RLDG 在成功率和泛化能力上显著优于使用人类演示的传统微调方法。RLDG 不仅减少了对人类演示数据的依赖,还展示了在复杂多阶段任务中的灵活性和有效性。

三木今天学习了嘛の博客 2291

莫队算法-离线+分块-两个模板题

莫队算法特点:离线,时间复杂度n1.5 对数组进行分块,分块的大小是n0.5,数组被分成了n0.5块。 将所有查询按第一关键字为左边界的分块索引,第二关键字为右边界进行排序。详见代码cmp函数。 排序完成以后,离线回答每个询问。 在模板基础上,一般是修改add和del函数,指针移动时,区间维护内容稍有不同,详见下面的两个题目。 题目一:SPOJ DQUERY - D-query Given a sequence of n numbers a1, a2, …, an and a number of d-q

Nothing_but_Fight的博客 365

手把手教你用Simulink搭建级联H桥储能变流器仿真模型(附SOC均衡分析)

本文详细介绍了如何使用Simulink搭建级联H桥储能变流器仿真模型,并附有SOC均衡分析。从仿真环境准备、单相H桥单元建模到多模块级联方案和SOC均衡策略实现,逐步指导读者完成复杂系统的构建与优化。通过关键仿真结果分析和工程实践问题排查,帮助读者掌握级联H桥储能变流器的核心技术与应用方法。

weixin_42524226的博客 453

Nacos2.X 源码分析:为订阅方推送、服务健康检查、集群数据同步、grpc客户端服务端初始化

而Nacos2.X 从http请求改为了grpc请求的方式了,在健康检查这方面就是基于长连接来实现的。客户端发送心跳是在构建grpc连接时会有一个线程每隔5s发送一次,NacosClient端发送心跳的代码在。我们能大致猜测:当一个服务的实例进行了更新,那么是不是应该推送给所有订阅了该服务的客户端?当我订阅了某个服务,是不是应该将该服务信息推送给我?在服务实例检查检查流程中,如果出现了要注销的服务实例就会发布。在服务发现时,进行订阅服务的最后会发布一个。服务注册与服务订阅流程中发布的事件最终会被。

qq_44027353的博客 1603

Little Elephant and Array

B. Little Elephant and Array time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard output The Little Elephant loves playing with arrays. He has array a, co...

weixin_43438700的博客 255

FISCO BCOS(十二)——— 区块链关键概念

1、什么是区块链 区块链(blockchain)是在比特币之后提出的一个概念,在中本聪关于比特币的论文中没有直接引入blockchain的概念,而是以chain of block来描述一种数据结构,比特币采用区块链技术,但是区块链并不等同于比特币。 Chain of block是指由多个区块通过哈希(hash)串联成一条链式结构的数据组织方式。区块链则是采用多项技术交叉组合,维护管理这个chain of block数据结构,形成一个不可篡改的分布式账本的综合技术领域。 区块链已经应用于数字货币、金融、司法、

林中有神君 9344

codeforces 221D Little Elephant and Array

题目的意思是说,给定一个区间,其长度为m,然后进行n次查询,每次查询给定区间[l,r],查出区间中出现次数等于其值大小的数的个数。开始做的时候,不是超空间就是超时间,然后借鉴了一下别人的思路。    首先使用一个a数组记录m个元素,期间用b数组记录每个元素出现的次数,然后在用l数组和r数组记录给定的查询区间,因为已经记录每个元素的出现次数,所以那些出现次数小于其值的数根本就不用考虑了,然后就是从

浮躁的人,终究一事无成! 1598

PCL 基于强度的双边滤波【2027最新版】

基于强度信息的双边滤波代码实现、滤波前后点云未出现变化的原因以及获取带有强度pcd点云的方法。本文由CSDN点云侠原创,博客长期更新,本文最近一次更新时间为:2026年12月3日。

点云侠的博客 29万+

【Bluetooth蓝牙开发】十一、一文秒懂 | 超详细的Bluez交叉编译

1、开篇词 2、蓝牙开发入门 3、蓝牙协议栈总览 4、蓝牙协议栈——物理层 5、蓝牙协议栈——链路层 6、蓝牙协议栈——传输层 7、蓝牙协议栈——L2CAP协议 8、蓝牙协议栈——ATT协议 9、蓝牙协议栈——GATT协议 10、蓝牙通信流程分析 11、蓝牙协议栈——Bluez交叉编译 12、蓝牙调试工具汇总

Donge's Blog 3994

codeforces - B. Little Elephant and Array

B. Little Elephant and Array time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard output The Little Elephant loves playing with arrays. He has array a, co...

青烟绕指柔的博客 504

使用Android Studio调试系统应用之TvSettings(一):移植

对于做ROM的同仁、以及想要深入学习Android同学,必定会去调试Android的系统APK,而 *aosp* 源码中的系统APK源码,无法直接在Android Studio中进行调试,本文将持续更新细节,手把手教你,使用Android Studio调试安卓的系统应用。在每个阶段,提供知识点的梳理、总结,为您答疑解惑!

上周的博客 2059

Codeforces Round #136 (Div. 2) D. Little Elephant and Array

题意:给你n个数m个查询,问在每一个查询区间[l,r]范围内有多少个值为valu的数出现次数也为valu。 有两种用线段树的方法。两种方法同样都是离线处理查询,保存所有的查询,将查询按照终点从小到大排序。用cnt[valu]记录valu的出现次数,用pos[valu]记录每个valu出现的位置。 1.线段树更新区间,查询点 从小到大遍历这n个数,同时更新cnt[]和pos[]。当valu的出

3113

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

『题解』Codeforces220B Little Elephant and Array

更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description The Little Elephant loves playing with arrays. He has array \(a\), consisting of \(n\) positive integers, indexed from \(1\) to \(n\...

weixin_30460489的博客 155

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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 standard input output standard output The Little Elephant loves play

qqspeed 1017

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Little Elephant and Array - CodeForces 220 B 树状数组

Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves playing

提比的强行AC 966

CodeForces 221D Little Elephant and Array(前缀和|莫队算法|树状数组)

这道题的题意是给你一串数字,再给你一对序号,满足1出现一次,2出现两次,3出现三次以此类推,找出某对序号中有多少数字满足以上规律。没做出来,题意看着简单,想着朴素解法就是可以的,但是网上大量的都是莫队算法,线段树,区间统计等等…不用也行,用了也不见得简单。 D. Little Elephant and Array time limit per test4 seconds memory limit ...

Nerdant 493

CF #136Div2 D. Little Elephant and Array

D. Little Elephant and Array time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard output The Little Elephant loves playing with arrays. He has

SeeYouAlways 1344

【分解因子】The Little Elephant loves numbers.

The Little Elephant loves numbers. He has a positive integer x. The Little Elephant wants to find the number of positive integers d, such that d is the divisor of x, and x and d have at least one comm...

于衡的博客 392

Codeforces Round #136 (Div. 1) B. Little Elephant and Array

time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves playing with arrays. He has array...

weixin_30912051的博客 140

CodeForces - 221D:Little Elephant and Array(莫队算法)

Description The Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let’s denote the number with index i as ai. Additionally the Little E...

No Pain, No Gain . 290

CodeForces - 221D Little Elephant and Array(区间前缀和)

Description: The Little Elephant loves playing with arrays. He has arraya, consisting ofnpositive integers, indexed from 1 ton. Let's denote the number with indexiasai. Additionally the Little...

邵光亮的博客 759

uva11996 - Jewel Magic 伸展树

Problem J Jewel Magic I am a magician. I have a string of emeralds and pearls. I may insert new jewels in the string, or remove old ones. I may even reverse a consecutive part of the string. At

7529

堆 优先队列

首先看完全二叉树的定义: 若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。   如果一个完全二叉树的某个结点值都不大于或小于他的父结点,那就叫做堆。根结点是所有结点里最大的叫大根堆,最小的叫小根堆。   比如:  9-1 a是大根堆,9-2 a是小根堆,剩下的都不是堆(因为不是完全二叉树)

2236

uva10410 - Tree Reconstruction BFS和DFS重建树

You have just finished a compiler design homework question whereyou had to find the parse tree of an expression. Unfortunately youleft your assignment in the library, but luckily your friend picked it

1223
上一篇: poj2991 Crane 向量旋转+线段树
下一篇: hdu2970 Suffix reconstruction 后缀数组反过来构造串
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址