标签:
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.
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).
In m lines print m integers — the answers to the queries. The j-th line should contain the answer to the j-th query.
7 2
3 1 2 2 3 3 7
1 7
3 4
3
1
1 #include <bits/stdc++.h> 2 #define A first 3 #define B second 4 using namespace std; 5 typedef pair<int,int> pii; 6 const int maxn = 100010; 7 int tree[maxn<<2]; 8 inline void pushdown(int v){ 9 if(tree[v]){ 10 tree[v<<1] += tree[v]; 11 tree[v<<1|1] += tree[v]; 12 tree[v] = 0; 13 } 14 } 15 void update(int L,int R,int lt,int rt,int val,int v){ 16 if(lt <= L && rt >= R){ 17 tree[v] += val; 18 return; 19 } 20 pushdown(v); 21 int mid = (L + R)>>1; 22 if(lt <= mid) update(L,mid,lt,rt,val,v<<1); 23 if(rt > mid) update(mid + 1,R,lt,rt,val,v<<1|1); 24 } 25 int query(int L,int R,int pos,int v){ 26 if(L == R) return tree[v]; 27 pushdown(v); 28 int mid = (L + R)>>1; 29 if(pos <= mid) return query(L,mid,pos,v<<1); 30 if(pos > mid) return query(mid + 1,R,pos,v<<1|1); 31 } 32 int ans[maxn],a[maxn],cnt[maxn]; 33 vector<int>pos[maxn]; 34 struct QU{ 35 int x,y,id; 36 bool operator<(const QU &rhs)const{ 37 return y < rhs.y; 38 } 39 }q[maxn]; 40 pii pre[maxn]; 41 int main(){ 42 int n,m; 43 while(~scanf("%d%d",&n,&m)){ 44 memset(tree,0,sizeof tree); 45 memset(cnt,0,sizeof cnt); 46 for(int i = 0; i < n; ++i) 47 scanf("%d",a + i); 48 for(int i = 0; i < m; ++i){ 49 scanf("%d%d",&q[i].x,&q[i].y); 50 q[i].id = i; 51 } 52 for(int i = 0; i < maxn; ++i) pos[i].clear(); 53 sort(q,q + m); 54 for(int i = 0, j = 0; i < n; ++i){ 55 if(a[i] <= n){ 56 pos[a[i]].push_back(i + 1); 57 cnt[a[i]]++; 58 if(cnt[a[i]] == a[i]){ 59 pre[a[i]] = pii(1,pos[a[i]][0]); 60 update(1,n,pre[a[i]].A,pre[a[i]].B,1,1); 61 }else if(cnt[a[i]] > a[i]){ 62 update(1,n,pre[a[i]].A,pre[a[i]].B,-1,1); 63 pre[a[i]] = pii(pre[a[i]].B + 1,pos[a[i]][cnt[a[i]] - a[i]]); 64 update(1,n,pre[a[i]].A,pre[a[i]].B,1,1); 65 } 66 } 67 while(j < m && q[j].y == i + 1){ 68 ans[q[j].id] = query(1,n,q[j].x,1); 69 ++j; 70 } 71 } 72 for(int i = 0; i < m; ++i) 73 printf("%d\n",max(0,ans[i])); 74 } 75 return 0; 76 }
CodeForces 221D Little Elephant and Array
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4887386.html