跳到正文

个人记录

常数优化

Tiagim约 2 分钟
原文
文章信息与版本

文章信息

发布
2022年8月19日
收录
2026年10月30日
更新
2026年11月21日
篇幅
约 2 分钟
获赞
2

版本

  1. v1当前

先看c++版本以及是否打开优化开关

  • deque ⇒\Rightarrow vector模拟队列
  • 自写min、max
  • 少用STL
  • 快读快写、fread、fwrite
  • 频繁改变的变量加register
  • 访问a[i]后访问a[i+1]、a[i+2]···
  • 数组开奇数(原因不明)
  • 循环展开:for(int i=1;i<=10000;i++)a[i]++; ⇒\Rightarrow for(int i=1;i<=10000;i+=4)a[i]++,a[i+1]++,a[i+2]++,a[i+3]++;
  • 位运算:
    • x*=(2,4,8,16...) ⇒\Rightarrow x<<=(1,2,3,4...) (( /= ⇒\Rightarrow >>= ))
    • x%2 ⇒\Rightarrow x&1
    • x>y ⇒int:\Rightarrow int: y-x>>31 longlong long:long: y-x>>63
    • -x ⇒\Rightarrow ~x+1
    • swap(x,y) ⇒\Rightarrow x^=y^=x^=y
    • abs(x) ⇒\Rightarrow (x^(x>>31))-(x>>31) longlong long:long: 31 ⇒\Rightarrow 63
  • bool ⇒\Rightarrow int//bitset
  • inline
  • if(a)b;else c; ⇒\Rightarrow a?b:c;
  • if(a)b; ⇒\Rightarrow a&&b;
  • ; ⇒\Rightarrow ,
  • int i=1; ⇒\Rightarrow int i(1);
  • i++; ⇒\Rightarrow ++i;
原文
抓取于
  1. Fze_8

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%