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

来源:互联网 发布:iphone 163 smpt 端口 编辑:程序博客网 时间:2024/05/16 01:35

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
原创粉丝点击