operator 1
ai−aj=x
ai=x+aj
bitset b1[p] 维护是否存在 p,(b1 << x & b1) .any()
O(wn)
operator 2
ai+aj=x
ai=x−ai
bitset b2[p] 维护是否存在 N−p,(b2 >> n - x & b1) .any()
O(wn)
operator 3
ai×aj=x
必然 min(ai,aj)≤x,暴力枚举较小数
O(x)
operator 4
ajai=x
ai=x×aj
暴力枚举 aj,时间复杂度 O(xN),当 x≥N 时可接受
当 x<N 时,注意到满足的 x 很少,考虑预处理。
我们可以维护 lsti,x,表示商为 x 时最后一对的位置,显然当 L≤lstR,x,存在答案。
考虑如何维护 lsti,x。
我们可以维护 posi,v 代表 v 在 i 之前最后出现的位置,即可转移 lsti,x:
lsti,x=max(lsti,x,posi,ai×x,posi,ai÷x)
直接暴力转移 O(NN),然后这里 pos 和 lst 都可以滚动数组优化,然后就做完了
总结
bitset 的 O(wN) 时间复杂度很好用
对于 a×b=x,想到枚举较小数
对于 O(xN) 时间复杂度,考虑对 x 根号分块
使用滚动数组优化空间
总时间复杂度:
O(NN) 莫队指针移动
O(wN) operator 1/2
O(N) operator 3/4 的 x≥N 部分
O(NN) operator 4 的 x<N 部分。
总时间复杂度:
NN+m×wN+m×N+NN=O(N×(N+wN))