Problem A Count

## Problem Description

Given an array of positive integers and m queries.Each query contains i, j, x, output the number of occurrences of x into the subarray Ai,Ai+1...,Aj.

## Input

There are several cases. The first line of each case contains tow integers n, q(1<=n,q<=100000), indicating the array length and the number of queries.The second line contains n positive integers ai(1 <= ai <= 100000).Next q lines contain three positive integers i,j,x(1<=i<=j<=n).

## Output

For each query output one line, the number of occurrences of x.

3 2
1 2 1
1 2 1
1 3 1

## Sample Output

1
2

//

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100000+100;
int n,m;
int a[maxn];
int p[maxn],next[maxn];
void tinit()
{
memset(p,-1,sizeof(p));
memset(next,-1,sizeof(next));
}
{
if(p[a]==-1)
{
p[a]=b;
return ;
}
next[b]=p[a];
p[a]=b;
}

struct Node
{
int l,r,num,id,ans;
};
Node qy[maxn];
bool cmp(Node h,Node k)
{
return h.num<k.num;
}
bool cmpp(Node h,Node k)
{
return h.id<k.id;
}

int c[maxn];
void cinit()
{
memset(c,0,sizeof(c));
}
int lowbit(int x)
{
return x&(-x);
}
void update(int u,int x)
{
for(int i=u;i<=n;i+=lowbit(i))
{
c[i]+=x;
}
}
int getsum(int u)
{
int cnt=0;
for(int i=u;i>=1;i-=lowbit(i))
{
cnt+=c[i];
}
return cnt;
}
int tans[maxn];
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
tinit();
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&qy[i].l,&qy[i].r,&qy[i].num);
qy[i].id=i;
}
sort(qy,qy+m,cmp);
int pre=-1;
cinit();
for(int i=0;i<m;i++)
{
if(qy[i].num!=pre)
{
pre=qy[i].num;
int nxt=p[pre];
while(nxt!=-1)
{
update(nxt,1);
nxt=next[nxt];
}
}
int ans=getsum(qy[i].r)-getsum(qy[i].l-1);
qy[i].ans=ans;
if(i<m-1&&qy[i].num!=qy[i+1].num)
{
int nxt=p[pre];
while(nxt!=-1)
{
update(nxt,-1);
nxt=next[nxt];
}
}
}
//sort(qy,qy+m,cmpp);
for(int i=0;i<m;i++) tans[qy[i].id]=qy[i].ans;
for(int i=0;i<m;i++) printf("%d\n",tans[i]);
}
return 0;
}
/*
5 5
1 2 1 2 1
1 2 1
1 3 1
1 4 1
1 5 1
2 3 2
*/

#### hdu4417 树状数组（求指定区间比指定数小的数的个数）

2015-04-05 11:54:45

#### 求区间均值大于K区间数---树状数组

2017-09-20 14:34:34

#### spoj D-query 区间不同数个数 主席树||离线+树状数组

2016-08-27 16:45:25

#### 树状数组求区间最大值

2015-09-04 10:42:56

#### 树状数组求区间极值

2016-10-04 16:58:28

#### 【模板】树状数组 区间修改，区间求和 （模板题：洛谷P3368树状数组2）

2017-05-26 14:50:28

#### 求第K小/大的数（树状数组解法）

2013-09-25 23:05:50

#### 树状数组之区间求最值

2014-10-28 17:17:32

#### [CodeVS1082] 线段树练习3(区间修改+询问区间和)

2015-07-26 16:52:05

#### HDU 5901 Count primes （求1e11内素数个数）

2016-09-18 19:40:52

## 不良信息举报

fzu count 求区间之间x的个数 离线+树状数组