BZOJ 2120: 数颜色【带修莫队】

                                                        2120: 数颜色

                                                                   Time Limit: 6 Sec  Memory Limit: 259 MB

Description

墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?

Input

第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。

Output

对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。

Sample Input

6 5
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6

Sample Output

4
4
3
4

HINT

对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。

【题目链接】 数颜色

【题意】

多个区间询问,询问[l,r]中颜色的种类数。可以单点修改颜色。

【思路】

与普通莫队相比,引入一个修改时间T,表示当前询问是发生在前T个修改操作后的,在进行莫队算法时,看看当前的询问和时间指针(l,r,T)是否相符,然后进行时光倒流或者时光推移操作来保证答案正确性。

在sort的时候,be[l]作为第一关键字,be[r]作为第二关键字,T作为第三关键字进行排序

时间复杂度:

对于l指针:O(blo*n)

对于r指针:O(n*n/blo)

对于T指针:O((n/blo)^2*n)

总时间复杂度在当blo=n^(2/3)时达到最优:O(n^(5/3))

#include <cstdio>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef  long long ll;
const int maxn = 10005;
const ll mod = 1e9+7;
const int INF = 1e9;
const double eps = 1e-6;

int n,m,blo;
int l,r;
int ans,Ans[maxn];
int a[maxn],now[maxn],flag[maxn*100];

struct node
{
    int l,r,ti;
    int id;
    node(int l=0,int r=0,int ti=0,int id=0):l(l),r(r),ti(ti),id(id) {}
} q[maxn];

struct node2
{
    int pos,New,old;
    node2(int pos=0,int New=0,int old=0):pos(pos),New(New),old(old) {}
} c[maxn];

bool cmp(node a,node b)
{
    if(a.l/blo!=b.l/blo)
    {
        if(a.r/blo!=b.r/blo)
        {
            return a.ti<b.ti;
        }
        return a.r<b.r;
    }
    return a.l<b.l;
}

void update(int color,int val)
{
    flag[color]+=val;
    if(val>0) ans+=(flag[color]==1);
    else if(val<0) ans-=(flag[color]==0);
}

void solve(int pos,int color)
{
    if(pos>=l&&pos<=r) update(color,1),update(a[pos],-1);
    a[pos]=color;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        mst(flag,0);
        blo=(int)pow(n,0.66666);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            now[i]=a[i];
        }
        int t=0;
        int cnt=0;
        ans=0;
        for(int i=0; i<m; i++)
        {
            char op[2];
            int x,y;
            scanf("%s%d%d",op,&x,&y);
            if(op[0]=='Q')
            {
                q[cnt++]=node(x,y,t,cnt);
            }
            else
            {
                c[++t]=node2(x,y,now[x]);
                now[x]=y;
            }
        }
        sort(q,q+cnt,cmp);
        l=1,r=0;
        int nowT=0;
        for(int i=0; i<cnt; i++)
        {
            while(nowT<q[i].ti) solve(c[nowT+1].pos,c[nowT+1].New),nowT++;
            while(nowT>q[i].ti) solve(c[nowT].pos,c[nowT].old),nowT--;

            while(q[i].l<l) update(a[l-1],1),l--;
            while(q[i].l>l) update(a[l],-1),l++;
            while(q[i].r<r) update(a[r],-1),r--;
            while(q[i].r>r) update(a[r+1],1),r++;

            Ans[q[i].id]=ans;
        }
        for(int i=0; i<cnt; i++) printf("%d\n",Ans[i]);
    }
}

猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/81135899