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 ;
}

CodeForces 221D Little Elephant and Array的更多相关文章

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

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

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

  3. CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)

    题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...

  4. Codeforces - 220B Little Elephant and Array(莫队模板题)

    题意: m次查询.每次查询范围[L,R]中出现次数等于该数字的数字个数. 题解: 由于分块,在每次询问中,同一块时l至多移动根号n,从一块到另一块也是最多2倍根号n.对于r,每个块中因为同一块是按y排 ...

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

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

  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. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

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

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

随机推荐

  1. C#画图——Graphics

    C#要实现简单的画图功能可以利用Graphics这个类,要使用Graphics必需using命名空间System.Drawing(此名明空间下都是关于图形的操作).首先创建画布: Bitmap bmp ...

  2. [转]无废话SharePoint入门教程一[SharePoint概述]

    本文转自:http://www.cnblogs.com/iamlilinfeng/p/3026332.html 一.前言 听说SharePoint也有一段时间了,可一直处在门外.最近被调到ShareP ...

  3. php中除法取整的方法(round,ceil,floor)

    PHP中遇到需要将除法所得结果取整的情况时,就需要用到以下方法: 1. round:四舍五入 round() 函数对浮点数进行四舍五入. 语法:round(x, prec) 参数 描述 x 可选.规定 ...

  4. css靠左,靠右

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. was--创建概要文件(典型)

    1.第一步 2 .创建 3.创建 4 .典型 5  下一步 6 下一步   7.下一步   8.输入用户和密码,下一步 9.下一步 10.下一步 11.下一步 12.下一步 13.下一步 14.创建 ...

  6. Katalon Studio 安装 配置 简单使用

    本教程只针对Katalon Studio进行演示操作. 一.下载 Katalon 官网下载地址:https://www.katalon.com/download/ (需要注册账号) 二.解压.配置 直 ...

  7. this常用的用法

    1.函数作为对象的方法时,this指的是该对象: var obj ={ name:"bob", age:25, getName:function(){ console.log(th ...

  8. vue1.0生命周期

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. RSA js加密 java解密

    1. 首先你要拥有一对公钥.私钥: ``` pubKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1gr+rIfYlaNUNLiFsK/Kn ...

  10. 本地连接批处理修改IP

    例子: 本地连接修改IP netsh interface ip delete dns "本地连接" addr=allnetsh interface ip add dns " ...