树状数组套主席树(good)uva12345

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;
}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值