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