Accept: 60 Submit: 327
Time Limit: 1000 mSec Memory Limit : 65536 KB
Problem Description
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
Sample Input
Sample Output
//
#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));
}
void addedge(int a,int b)//数字a对应位置b
{
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]);
addedge(a[i],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
*/