K | Dyanmic len(set(a[L:R])) Input: Standard Input Output: Standard Output | |
In python, we can use len(start(a[L:R])) to calculate the number of distinct values ofelements a[L],a[L+1], ..., a[R-1].
Here are some interactive examples that may help you understandhow it is done. Remember that the indices of python lists start from 0.
>>>a=[1,2,1,3,2,1,4]
>>> print a[1:6]
[2, 1, 3, 2, 1]
>>> print set(a[1:6])
set([1, 2, 3])
>>> printlen(set(a[1:6]))
3
>>> a[3]=2
>>> printlen(set(a[1:6]))
2
>>> print len(set(a[3:5]))
1
Your task is to simulate this process.
Input
There will be only one testcase. The first line contains two integers n and m (1 ≤ n,m ≤50,000). The next line contains the original list.
All the integers arebetween 1 and 1,000,000 (inclusive). The next m lines contain the statementsthat you need to execute.
A line formatted as "Mx y" (1 ≤ y ≤ 1,000,000) means "a[x] = y", and aline formatted as "Q x y" means "print len(set(a[x:y]))".
It is guaranteed that thestatements will not cause "index out of range" error.
Output
Print the simulated result, one line for each query.
SampleInput Output for Sample Input
7 4 1 2 1 3 2 1 4 Q 1 6 M 3 2 Q 1 6 Q 3 5
| 3 2 1
|
题意:这个题是spoj3267的升级版,支持数的修改。
想了好长时间才想明白,题目可以转化成这样,L[i]表示下表小于i,并且值跟num[i]相等的下表j,这样求(l,r)区间有多少个不同的数,就转化成了求(l,r)中L[i]的值小于l的有多少个(不明白可以手写一下),然后就可以求了
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=50010;
const int maxm=20000005;
const int maxk=2000010;
int N,M;
int pre[maxn],a[maxn],num;
int cnt[maxm],T[maxn],S[maxn],lson[maxm],rson[maxm];
set<int> s[maxk];
set<int>::iterator it;
int build(int l,int r)
{
int root=num++;
cnt[root]=0;
if(l!=r)
{
int mid=(l+r)>>1;
lson[root]=build(l,mid);
rson[root]=build(mid+1,r);
}
return root;
}
int update(int root,int pos,int val)
{
int newroot=num++,tmp=newroot;
int l=0,r=N;
cnt[newroot]=cnt[root]+val;
while(l<r)
{
int mid=(l+r)>>1;
if(pos<=mid)
{
lson[newroot]=num++,rson[newroot]=rson[root];
newroot=lson[newroot],root=lson[root];
r=mid;
}
else
{
l=mid+1;
rson[newroot]=num++,lson[newroot]=lson[root];
newroot=rson[newroot],root=rson[root];
}
cnt[newroot]=cnt[root]+val;
}
return tmp;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int last)
{
int tmp=x;
while(x<=N)
{
S[x]=update(S[x],pre[tmp],-1);
S[x]=update(S[x],last,1);
x+=lowbit(x);
}
pre[tmp]=last;
}
int query(int root,int pos)
{
int l=0,r=N;
int ans=0;
while(l<r)
{
int mid=(l+r)>>1;
if(pos<=mid)
{
r=mid;
root=lson[root];
}
else
{
ans+=cnt[lson[root]];
root=rson[root];
l=mid+1;
}
}
return ans+(pos<l?0:cnt[root]);
}
int querysum(int l,int r)
{
int ans=query(T[r],l-1)-query(T[l-1],l-1);
for(int i=l-1;i>0;i-=lowbit(i))
ans-=query(S[i],l-1);
for(int i=r;i>0;i-=lowbit(i))
ans+=query(S[i],l-1);
return ans;
}
void modify(int x,int val)
{
if(a[x]==val)return;
s[a[x]].erase(x);
it=s[a[x]].lower_bound(x);
if(it!=s[a[x]].end())
add(*it,pre[x]);
it=s[val].lower_bound(x);
if(it==s[val].begin())
add(x,0);
else {add(x,*(--s[val].lower_bound(x)));}
if(it!=s[val].end()){add(*it,x);}
s[val].insert(x);
a[x]=val;
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(pre,0,sizeof(pre));
for(int i=0;i<maxk;i++)s[i].clear();
num=0;
T[0]=build(0,N);
for(int i=1;i<=N;i++)
{
scanf("%d",&a[i]);
it=s[a[i]].lower_bound(i);
if(it!=s[a[i]].begin())
pre[i]=*(--it);
s[a[i]].insert(i);
T[i]=update(T[i-1],pre[i],1);
}
for(int i=0;i<=N;i++)S[i]=T[0];
while(M--)
{
char op[5];
int l,r;
scanf("%s%d%d",op,&l,&r);
if(op[0]=='Q')printf("%d\n",querysum(l+1,r));
else modify(l+1,r);
}
}
return 0;
}