提供一个卡常方案(若你TLE #5
查看原帖
提供一个卡常方案(若你TLE #5
526677
封禁用户楼主2023/8/22 09:55

可以不要用CYJian给的快读,用这个:

static char space[40000000],*sp=space;
template<typename T>
struct myalloc:allocator<T>{
    myalloc(){}
    template<typename T2>
    myalloc(const myalloc<T2> &a){}
    template<typename T2>
    myalloc<T>& operator=(const myalloc<T2> &a){return *this;}
    template<typename T2>
    struct rebind{typedef myalloc<T2> other;};
    inline T* allocate(size_t n){
        T *result=(T*)sp;sp+=n*sizeof(T);
        return result;
    }
    inline void deallocate(T* p,size_t n){}
};
vector <ll,myalloc<ll>> c[N];
static char pbuf[1000000],*p1=pbuf,*p2=pbuf,obuf[1000000],*o=obuf;
#define getchar() p1==p2&&(p2=(p1=pbuf)+fread(pbuf,1,1000000,stdin),p1==p2)?EOF:*p1++
#define putchar(x) (o-obuf<1000000)?(*o++=(x)):(fwrite(obuf,o-obuf,1,stdout),o=obuf,*o++=(x))
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    return;
}
struct flusher{~flusher(){fwrite(obuf,o-obuf,1,stdout);}}autoflush;

使用方法:n=read(),write(n);

你细心的话会注意到我在这段代码中间有一句:vector <ll,myalloc<ll>> c[N];

如果你用了vector,那就用这句话来定义,优化显著。当然可以不用vector是我死脑筋

PS:这段代码是我从之前写的,也是一道神仙毒瘤数据结构卡常题整来的。我只能说CYJian的数据太强力了。

2023/8/22 09:55
加载中...