CodeForces 221D Little Elephant and Array
Little Elephant and Array
This problem will be judged on CodeForces. Original ID: 221D
64-bit integer IO format: %I64d Java class name: (Any)
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 Input
7 2
3 1 2 2 3 3 7
1 7
3 4
3
1
Source
#include <bits/stdc++.h>
#define A first
#define B second
using namespace std;
typedef pair<int,int> pii;
const int maxn = ;
int tree[maxn<<];
inline void pushdown(int v){
if(tree[v]){
tree[v<<] += tree[v];
tree[v<<|] += tree[v];
tree[v] = ;
}
}
void update(int L,int R,int lt,int rt,int val,int v){
if(lt <= L && rt >= R){
tree[v] += val;
return;
}
pushdown(v);
int mid = (L + R)>>;
if(lt <= mid) update(L,mid,lt,rt,val,v<<);
if(rt > mid) update(mid + ,R,lt,rt,val,v<<|);
}
int query(int L,int R,int pos,int v){
if(L == R) return tree[v];
pushdown(v);
int mid = (L + R)>>;
if(pos <= mid) return query(L,mid,pos,v<<);
if(pos > mid) return query(mid + ,R,pos,v<<|);
}
int ans[maxn],a[maxn],cnt[maxn];
vector<int>pos[maxn];
struct QU{
int x,y,id;
bool operator<(const QU &rhs)const{
return y < rhs.y;
}
}q[maxn];
pii pre[maxn];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(tree,,sizeof tree);
memset(cnt,,sizeof cnt);
for(int i = ; i < n; ++i)
scanf("%d",a + i);
for(int i = ; i < m; ++i){
scanf("%d%d",&q[i].x,&q[i].y);
q[i].id = i;
}
for(int i = ; i < maxn; ++i) pos[i].clear();
sort(q,q + m);
for(int i = , j = ; i < n; ++i){
if(a[i] <= n){
pos[a[i]].push_back(i + );
cnt[a[i]]++;
if(cnt[a[i]] == a[i]){
pre[a[i]] = pii(,pos[a[i]][]);
update(,n,pre[a[i]].A,pre[a[i]].B,,);
}else if(cnt[a[i]] > a[i]){
update(,n,pre[a[i]].A,pre[a[i]].B,-,);
pre[a[i]] = pii(pre[a[i]].B + ,pos[a[i]][cnt[a[i]] - a[i]]);
update(,n,pre[a[i]].A,pre[a[i]].B,,);
}
}
while(j < m && q[j].y == i + ){
ans[q[j].id] = query(,n,q[j].x,);
++j;
}
}
for(int i = ; i < m; ++i)
printf("%d\n",max(,ans[i]));
}
return ;
}
改点求段
#include <bits/stdc++.h>
#define A first
#define B second
using namespace std;
typedef pair<int,int> pii;
const int maxn = ;
int tree[maxn<<];
void update(int L,int R,int pos,int val,int v){
if(L == R){
tree[v] += val;
return;
}
int mid = (L + R)>>;
if(pos <= mid) update(L,mid,pos,val,v<<);
if(pos > mid) update(mid + ,R,pos,val,v<<|);
tree[v] = tree[v<<] + tree[v<<|];
}
int query(int L,int R,int lt,int rt,int v){
if(lt <= L && rt >= R) return tree[v];
int mid = (L + R)>>,ret = ;
if(lt <= mid) ret = query(L,mid,lt,rt,v<<);
if(rt > mid) ret += query(mid + ,R,lt,rt,v<<|);
return ret;
}
int a[maxn],cnt[maxn],ans[maxn];
vector<int>pos[maxn];
struct QU{
int x,y,id;
bool operator<(const QU &rhs)const{
return y < rhs.y;
}
}q[maxn];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(tree,,sizeof tree);
memset(cnt,,sizeof cnt);
for(int i = ; i < maxn; ++i) pos[i].clear();
for(int i = ; i < n; ++i)
scanf("%d",a + i);
for(int i = ; i < m; ++i){
scanf("%d%d",&q[i].x,&q[i].y);
q[i].id = i;
}
sort(q,q + m);
for(int i = ,j = ; i < n; ++i){
if(a[i] <= n){
pos[a[i]].push_back(i + );
++cnt[a[i]];
if(cnt[a[i]] >= a[i]) update(,n,pos[a[i]][cnt[a[i]] - a[i]],,);
if(cnt[a[i]] >= a[i] + ) update(,n,pos[a[i]][cnt[a[i]] - a[i] - ],-,);
if(cnt[a[i]] > a[i] + ) update(,n,pos[a[i]][cnt[a[i]] - a[i] - ],,);
}
while(j < m && q[j].y == i + ){
ans[q[j].id] = query(,n,q[j].x,q[j].y,);
++j;
}
}
for(int i = ; i < m; ++i)
printf("%d\n",ans[i]);
}
return ;
}
CodeForces 221D Little Elephant and Array的更多相关文章
- 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内有多少个数满足其值为其出现的次数 题解: ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- Codeforces - 220B Little Elephant and Array(莫队模板题)
题意: m次查询.每次查询范围[L,R]中出现次数等于该数字的数字个数. 题解: 由于分块,在每次询问中,同一块时l至多移动根号n,从一块到另一块也是最多2倍根号n.对于r,每个块中因为同一块是按y排 ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- 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)) 在 ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* 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 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 ...
随机推荐
- 洛谷 P2824 [HEOI2016/TJOI2016]排序 (线段树合并)
(另外:题解中有一种思路很高妙而且看上去可以适用一些其他情况的离线方法) 线段树合并&复杂度的简单说明:https://blog.csdn.net/zawedx/article/details ...
- SolrCloud索引富文本数据
solrconfig配置文件: schema配置文件: 执行目录: /opt/solr-5.5.4/server/scripts/cloud-scripts -- 下载配置文件 ./zkcli.sh ...
- 转】Neo4j集群安装实践
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/2/ 感谢! Posted: Oct 29, 2013 Ta ...
- .NET框架概述
.NET战略目标: 任何时候(when),任何地方(where),使用任何工具(what)都能通过.NET的服务获得网络上的任何信息. .NET优势: 1.提供了一个面向对象的编程环境,完全支持面向对 ...
- 自学 iOS - 三十天三十个 Swift 项目 第三天
做了这个小demo 之后 感觉OC 和swift 还是有很大的差别的 自己还是要去多看些swift的语法 用的不是很熟练 1.这个demo 的资源文件 我都是用原工程的 2.同样的自定义cell 的 ...
- Elasticsearch--集群管理_时光机&监控
目录 Elasticsearch时光机 创建快照存储库 清理:删除旧的快照 监控集群状态和健康度 集群健康度API 索引统计API 状态API 节点信息API 节点统计API 集群状态API 挂起任务 ...
- 机器学习-Logistic function(Sigmoid function)
下面给出H函数 由这个函数生成的曲线称为Sigmoid曲线 先不从数学上说为什么这个模型中二元分类上比线性模型好,单纯从图形上看就可以得到直观的结论 首先Y值域在[0,1],其次图形中中间陡峭而两 ...
- SOA测试之浏览器插件
1. Chrome HTTP Rest Client 插件: 1.1 Postman: https://chrome.google.com/webstore/detail/postman-rest-c ...
- java web 学习笔记 - servlet01
---恢复内容开始--- 1.Servlet介绍 Servlet 是用java语言编写的服务器端小程序,属于一个CGI程序,但与传统的CGI不同的是,它是多线程实现的,并且可以多平台移植. 用户自定义 ...
- 函数式编程:上线文、包裹、容器-我们可以将一个值用Context(上下文)包裹起来
Functor,即函子,是 Haskell 中普遍存在的.最基本的类型类.你可以用以下两种方式来理解 Functor: 它代表某种容器,该容器能够将某一函数应用到其每一个元素上. 它代表某种“可计算上 ...