ADACROP - Ada and Harvest
As you might already know, Ada the Ladybug is a farmer. She has a very long furrow with many kinds of vegetables (represented by integer numbers). Whenever she wants to harvest a single vegetable, she always replace it with anoher vegetable (possibly same kind).
After each replacement, she wants to know the number of vegetables of the same kind (at the new vegetable) which are before it (have lower possition in furrow).
Input
The first line of input containts 1 ≤ N, Q ≤ 2*105 , the length of furrow and number of harvests.
The next line contains N numbers 0 ≤ Ai ≤ 109 the kind of vegetable which is currently on ith spot in furrow (indexed from 0).
The next Q lines contains two numbers 0 ≤ i < N (the index of harvested plant) and 0 ≤ a ≤ 109 (the kind of newly planted vegetable)
Output
For each harvest, print the number of vegetables of the same kind before the newly planted vegetable.
Example Input
5 5 1 2 1 2 1 2 2 4 2 2 3 3 3 4 3
Example Output
1 3 0 1 2
Example Input 2
10 10 2 3 5 3 9 3 5 2 9 9 7 2 0 5 0 2 1 2 9 2 4 3 8 2 4 2 2 5 3 5
Example Output 2
1 0 0 1 3 1 3 2 0 1
对于一个序列修改某个数,之后再查询前面的数当中与新的数相等的个数。由于数值的范围到了1e9,但是数量只有4e5,所以使用离散化,再进行分块预处理查询。
#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;
int a[MAXN],x[MAXN],y[MAXN],rank[2*MAXN],mp[448][2*MAXN];
struct node{
int v,id;
}b[2*MAXN];
bool cmp(node A,node B)
{
return A.v<B.v;
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i].v=a[i];
b[i].id=i;
}
for(int i=0;i<q;i++)
{
scanf("%d%d",&x[i],&y[i]);
b[n+i].v=y[i];
b[n+i].id=n+i;
}
sort(b,b+n+q,cmp);
rank[b[0].id]=0;
for(int i=1;i<n+q;i++)
{
if(b[i].v==b[i-1].v)
rank[b[i].id]=rank[b[i-1].id];
else
rank[b[i].id]=rank[b[i-1].id]+1;
}
for(int i=0;i<n;i++)
a[i]=rank[i];
int sz=(int)sqrt(n);
int rem=n%sz;
int num;
if(rem==0)
num=n/sz;
else
num=n/sz+1;
memset(mp,0,sizeof(mp));
for(int i=0;i<num;i++)
for(int j=0;j<sz&&i*sz+j<n;j++)
mp[i][a[i*sz+j]]++;
for(int i=0;i<q;i++)
{
int g=x[i],h=rank[n+i];
int pos;
if((g+1)%sz==0)
pos=(g+1)/sz-1;
else
pos=(g+1)/sz;
int ans=0;
for(int j=0;j<pos;j++)
ans+=mp[j][h];
for(int j=pos*sz;j<g;j++)
if(a[j]==h)
ans++;
printf("%d\n",ans);
mp[pos][a[g]]--;
mp[pos][h]++;
a[g]=h;
}
return 0;
}