【bzoj 2120】维护队列【bzoj 2453】数颜色 双倍经验!!!

bzoj难得的双倍经验
QAQ
卡了我好久……
233333
分块
对于每个元素,我记录pre[i] = 上个颜色与i相同的位置
然后就和教主的魔法那题一样了23333333
如果pre[i] < l则ans++
嗯还算好理解
QAQ一眼正解调一天系列23333333
因为询问很少
所以我可以暴力修改……
太暴力了……
23333333
自己看喽~


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cctype>
#define KILL puts("haha");
using namespace std;
const int MAXN = 10000 + 5;
const int MAXC = 1e6 + 5;
int n,m,M;
int col[MAXN],so[MAXN];
int pre[MAXN],last[MAXC];
void reset(int x)
{
    int l = min(n,x * M),r = min(n,x * M + M);
    for(int i = l;i < r;i ++)
        so[i] = pre[i];
    sort(so + l,so + r);
    return;
}
void make_pre()
{
    for(int i = 0;i < n;i ++)
        last[col[i]] = -1;
    for(int i = 0;i < n;i ++)
    {
        so[i] = pre[i] = last[col[i]];
        last[col[i]] = i;
    }
    for(int i = 0;i <= M;i ++)
        sort(so + min(n,i * M),so + min(n,i * M + M));
    return;
}
void init()
{
    M = sqrt(n) + 1;
    make_pre();
    return;
}
int ask(int x,int y)
{
    int l = x,r = y;
    int ans = 0;
    while(l % M && l < r)
        ans += (pre[l] < x),l ++;
    while(r % M && r > l)
        ans += (pre[r] < x),r --;
    ans += (pre[r] < x);
    while(l < r)
    {
        ans += lower_bound(so + l,so + min(n,l + M),x) - (so + l);
        l += M;
    }
    return ans;
}
void change(int x,int y)
{
    col[x] = y;
    for(int i = 0;i < n;i ++)
        last[col[i]] = -1;
    for(int i = 0;i < n;i ++)
    {
        int t = pre[i];
        pre[i] = last[col[i]];
        if(t != pre[i])
            reset(i / M);
        last[col[i]] = i;
    }
    return;
}
void scanf(char &c)
{
    c = getchar();
    while(!isalpha(c))
        c = getchar();
    return;
}
char q;
int l,r;
int main()
{
    scanf("%d %d",&n,&m);
    for(int i = 0;i < n;i ++)
        scanf("%d",&col[i]);
    init();
    for(int i = 1;i <= m;i ++)
    {
        scanf(q);
        switch(q)
        {
            case 'Q':scanf("%d %d",&l,&r);printf("%d\n",ask(l - 1,r - 1));break;
            case 'R':scanf("%d %d",&l,&r);change(l - 1,r);break;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值