对于WA/卡常的一些提示/建议
查看原帖
对于WA/卡常的一些提示/建议
683859
complete_binary_tree楼主2024/5/17 09:42
  1. 应该没有人和我一样暴力的时候*2吧(应该是平方)

  2. auto 修改的时候要用 auto&。

  3. 卡常小建议:

  • 使用 fastio,特别是 fread/fwrite。附上板子:
namespace FastIO{
    static char buf[100000],*p1=buf,*p2=buf;
    #define gc p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    inline int read() {int res=0,w=0; char c=gc; while (!isdigit(c)) c=gc; while (isdigit(c)) res=(res<<1)+(res<<3)+(c^48),c=gc; if (w) res=-res; return res;}
    inline void write(int x) {static int sta[35],top=0; do {sta[top++]=x%10,x/=10;}while (x); while (top) putchar(sta[--top]+48); putchar('\n');}
    inline void write(long long x) {static int sta[50],top=0; do {sta[top++]=x%10,x/=10;}while (x); while (top) putchar(sta[--top]+48); putchar('\n');}
};
using FastIO::read, FastIO::write;

理论上来说 buf 数组越大会越快,但实测开到 1e7 没差别。

  • 可以使用“记忆化快速幂”

不过似乎没什么用。

  • 块长可以调小。

实测如下:

块长时间最大点
128.59s10.39s
2012.37s2.32s
5011.91s2.28s
7511.99s2.32s
10011.90s2.29s
15012.12s2.33s
20014.34s3.40s
50028.39s12.13s

发现块长为 20−15020-150 时表现较好。

此外,块长为 nlog⁡2V\sqrt{\frac{n}{\log_2 V}} 的表现也不错(13.11s/2.31s)。

  • 为了内存访问连续,可以使用 vector,不要存下标,直接存数。

这样的效率吊打链式。(链式就算存数访问也是不连续的)

2024/5/17 09:42
加载中...