zoukankan      html  css  js  c++  java
  • [BZOJ2120]:数颜色(分块?)

    题目传送门


    我感觉这种题没必要扯淡题目大意了,没啥用。

    暴力过掉,擦了个边。

    主要是讲一下这道题我用到的卡常。

    首先,0,1标记我用的位运算,位运算符跑的要比正常的+,-,×,÷,true,false快。

    其次,我使用了

    while(isspace(op=fgetc(stdin)));
    ungetc(op,stdin); 

    来吃字符。

    个人感觉比while快。

    快读当然不可少,位运算快读更快。

    inline int read()
    {
        short x=0;
        char ch=getchar();
        while(ch<'0'||ch>'9')
            ch=getchar();
        while(ch>='0'&&ch<='9'){
            x=(x<<1)+(x<<3)+(ch^48);
            ch=getchar();
        }
        return x;
    }
    

    这些东西足以卡过洛谷的超水数据,不过还是建议大家打一下正解。

    另外,今天从同学那里学到了这个:

    const int L=1<<20|1;
    char buffer[L],*S,*T;
    #define getchar() ((S==T&&(T=(S=buffer)+fread(buffer,1,L,stdin),S==T))?EOF:*S++)
    

     好像是能快不少,不过需要慎用,这个我也用不熟练,在此就不做过多说明,出现问题概不负责。


    代码时刻

    #include<cstdio>
    #include<cstring>
    #include<cctype>
    int a[10001],x,y,ans;
    int wzc[1000001];
    char op;
    inline int read()
    {
        int x=0;
        char ch=getchar();
        while(ch<'0'||ch>'9')
            ch=getchar();
        while(ch>='0'&&ch<='9'){
            x=(x<<1)+(x<<3)+(ch^48);
            ch=getchar();
        }
        return x;
    }
    int main()
    {
        x=read();
        for(register int i=0;i<=x;++i)a[i]=read();
        while(a[0]--)
        {
            while(isspace(op=fgetc(stdin)));
            ungetc(op,stdin);
            op=getchar();
            x=read();
            y=read();
            if(op=='Q')
            {
                ans=0;
                memset(wzc,0,sizeof(wzc));
                for(register int i=x;i<=y;++i)
                    ans+=wzc[a[i]]^1,wzc[a[i]]|=1;
                printf("%d
    ",ans);
            }
            else a[x]=y;
        }
        return 0;
    }
    

    当然是卡常的代码,哈哈哈


    rp++

  • 相关阅读:
    Interrupt、Interrupted、IsInterrupted
    ReentrantLock
    tcp粘包、拆包
    jstat 分析应用垃圾回收状况
    CopyOnWriteArrayList
    storm基础概念
    余弦距离
    websocket
    awk
    sed
  • 原文地址:https://www.cnblogs.com/wzc521/p/11018827.html
Copyright © 2011-2022 走看看