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

647人阅读 评论(0)

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
*/

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：575924次
• 积分：12915
• 等级：
• 排名：第1031名
• 原创：749篇
• 转载：4篇
• 译文：0篇
• 评论：52条