Little Elephant and Array

Time Limit: 4000ms
Memory Limit: 262144KB

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

Input
7 2
3 1 2 2 3 3 7
1 7
3 4
Output
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 ;
}

最新文章

  1. solr schema.xml文档节点配置
  2. Android中asset和raw的区别
  3. 【转】解决IIS7该问.svc文件的错误问题
  4. ACdream1063——平衡树
  5. NSDate 时间比较...等
  6. 解决关于ArcGIS10.2服务手动启动的问题
  7. LeetCode:Palindrome Partitioning,Palindrome Partitioning II
  8. 数位dp-POJ-3252-Round Numbers
  9. silverlight嵌套html不能输入中文问题
  10. node.js在windows下的学习笔记(5)---用NODE.JS创建服务器和客户端
  11. 谈论HashMap,HashSet,HashTableeasy被我们忽视
  12. hibernate添加spring 事务管理注意问题记录
  13. uoj123 【NOI2013】小Q的修炼
  14. 通过 Ansible 安装 Docker
  15. LeetCode(32):最长有效括号
  16. ios 消除 字符串 首尾空格
  17. Mysql 索引 n-gram分词引擎使用
  18. 35. oracle中instr在平台上的转换用法
  19. sklearn_模型遍历
  20. Linux,ubuntu14.04.5下安装软件

热门文章

  1. QT5每日一学(二)编写QT多窗口程序
  2. Latex排版工具的使用(一) 分类: Latex 2014-06-14 22:52 448人阅读 评论(0) 收藏
  3. JS 对输入判断变化屏蔽中文输入法连续输入时触发的事件
  4. 基于CentOS6.5下Suricata(一款高性能的网络IDS、IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐)
  5. DEV—【GridControl 按钮列无法触发点击事件解决方案】
  6. 自动判断手机版和pc版
  7. Python3 动手自己写谷歌翻译
  8. Java集合框架源码(二)——hashSet
  9. 合并百度影音的离线数据 with python 2.1 bdv格式的更新
  10. nutwk的maven中央仓库及配置