莫名其妙的错误
莫名其妙的错误:
\(\color{red}{(+1)}1.\mathrm{min}\) 初始化时一定要设最大值,一般设为\(\mathrm{0x3f3f3f3f}\)。
\(\color{red}{(+1)}2.\) 十年 \(\mathrm{OI}\) 一场空,打错文件见祖宗。
\(\color{red}{(+1)}3.\) 背模板真的很重要!
\(\color{red}{(+4)}4.\) 数组一定要开大。
\(\color{red}{(+1)}5.\) 一定要记得开 \(\mathrm{long}\) \(\mathrm{long}\)。
\(\color{red}{(+1)}6.\) 树状数组在处理 \(0\) 时会出锅,所以输入时给每个数都 \(+1\)。
\(\color{red}{(+1)}7.\) 树状数组 modify 操作时应注意上限,即:
void modify(int x,int w)
{
for(int i=x;i<=n;i+=(i&-i)) tree[i]+=w;
}
//原版
若在输入时用了第 \(6\) 条的操作,需改为:
void modify(int x,int w)
{
for(int i=x;i<=n+1;i+=(i&-i)) tree[i]+=w;
}
//+1版
所以,最好写成:
const int maxn=...//即最大值
void modify(int x,int w)
{
for(int i=x;i<maxn;i+=(i&-i)) tree[i]+=w;
}
//通用版
\(\color{red}{(+1)}8.\) 树状数组的 erase 操作的循环应与 add 操作一样,从 \(\mathrm{x}\) 到 \(\mathrm{maxn}\)。
void erase(int x)
{
for(int i=x;i;i-=(i&-i)) tree[i]=0;
}
//错误写法
void erase(int x)
{
for(int i=x;i<maxn;i+=(i&-i)) tree[i]=0;
}
//正确写法
\(\color{red}{(+1)}9.\) 局部变量与全局变量一定不能命名相同,否则不会报错,会有奇妙的 bug。