跳到正文

休闲·娱乐

论如何在键盘的大部分标点符号都坏掉的情况下实现逆序对

CYFqwq约 29 分钟
原文

这是 2025年12月11日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年12月11日
收录
2025年12月12日
更新
2026年10月14日
篇幅
约 29 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1

(本人平时并不擅长 C++,很多内容和真实语法有偏差,还请见谅。另外,本文中的符号指不能在 C++ 语言中作为变量名的可见字符。)

起因

受到 机房大佬的文章 启发,我就在想既然排序都能用 %:<>() 这 66 种符号实现,那解决 逆序对 又何尝不行呢?于是我就去寻找逆序对中优秀的实现,找到了可能比较好实现 33 种:

  • 归并排序求逆序对。
  • 动态开点权值线段树。
  • 树状数组+离散化。

被淘汰的算法

归并排序需要用到递归,然而只用 %:<>() 想要实现递归是及其困难的,于是归并排序和线段树就遗憾离场了。

一些小技巧

要写树状数组,加减法,那么如何实现且保证复杂度呢?

替代运算符

在 C++ 中,有一种东西叫做替代运算符,下面是一些会用到的替代运算符:

运算符替代运算符
&bitand
|bitor
^xor
~compl
&=and_eq
|=or_eq
^=xor_eq
||or
!not

比较搞笑的是,bitand 还可以作为引用变量使用。

还有一些字符不是运算符,但是也可以被两个字符替代,这是早期 C++ 为了防止大家的键盘上没有一些字符而提供的。

原字符替代字符
#%:
{<%
}%>
[<:
]:>

我们发现这些替代字符恰好都在允许使用的字符的范围内!

不过有很多运算符都是没有替代运算符的,但是我们可以自己实现,比如 a = b 就是 a xor_eq b xor a,a == b 就是 not (a xor b),a <= b 就是 a < b or not (a xor b),等等。

加法

我们已经知道了替代运算符,那怎么用这些运算符完成加减法呢?首先,给出一种 O(log⁡V)O(\log V) 的加法。

我们发现在加法和异或的本质区别就是加法存在进位,因此我们可以将两个数的和转为异或和以及进位两个部分,即 a+b=a xor b+2(a bitand b)a+b=a\ \mathrm{xor}\ b+2(a\ \mathrm{bitand}\ b),我们发现只要一直 a′←a xor b,b′=2(a bitand b)a'\gets a\ \mathrm{xor}\ b,b'=2(a\ \mathrm{bitand}\ b),然后 a←a′,b←b′a\gets a',b\gets b' 直到 a bitand b=0a\ \mathrm{bitand}\ b=0 即可。

复杂度证明

假设 a bitand ba\ \mathrm{bitand}\ b 的最低位是 kk。

进行一步转化后 b′=2(a bitand b)b'=2(a\ \mathrm{bitand}\ b) 的最低位是 k+1k+1,此时 a′ bitand b′a'\ \mathrm{bitand}\ b' 的最低位一定会 >k>k,也就是说,一步转化至少会让 kk 增加 11。

我们发现 kk 肯定不会超过 ⌈log⁡V⌉\left\lceil\log V\right\rceil,因此复杂度为 O(log⁡V)O(\log V)。

C++
while (a bitand b) <%
    if (a xor_eq b) <%%>
    if (b and_eq a xor b) <%%>
    if (b xor_eq b xor (b << 1)) <%%>
%>
if (a or_eq b) <%%>

在 C++ 中,−b=compl b+1-b=\mathrm{compl}\ b+1,因此我们可以将 a−ba-b 转换为 a+compl b+1a+\mathrm{compl}\ b+1。

回到正题

接下来,我们就可以开始实现逆序对了!

需要用到的头文件:iostream,ranges,vector,algorithm,建议使用 C++ 20。

离散化

离散化同样有很多种实现,比如 lower_bound、map 和直接 sort 等等。这里我使用了手写 lower_bound 的方法。

先声明一些变量,由于有符号的限制,我们可以用 for (auto name : {x}) 来声明一个变量,也就是遍历一个只有一个元素的初始化列表。

C++
for (int n : <%0%>)
for (long long ans : <%0%>) <%
    if (std::cin >> n) <%%>
    for (auto a : <%std::vector<int>(n)%>)
    for (auto b : <%std::vector<int>(n)%>)
    for (auto tr : <%std::vector<int>(n<<1)%>) <%

其中 tr 的下标从 1 开始,因此需要开到比 nn 还大一点,刚好我们有 << 可以用,所以就开到了 n<<1。

接下来就是真正的离散化部分了。

C++
for (int i : std::views::iota(0) bitor std::views::take(n)) <%
    for (int l : <%0%>) for (int r : <%n%>) for (int res : <%0%>) <% // res 是二分结果
        for (int b : <%static_cast<int>(0xffffffffU)%>) <% // static_cast<int>(0xffffffffU) 是 -1,这段代码相当于 r--
            while (r bitand b) <%
                if (r xor_eq b) <%%>
                if (b and_eq r xor b) <%%>
                if (b xor_eq b xor (b << 1)) <%%>
            %>
            if (r or_eq b) <%%>
        %>
        while (l < r or not (l xor r)) <% // l <= r,闭区间二分
            for (int mid : <%l%>) <%
                // 这一部分相当于 mid = (l + r) >> 1
                for (int b : <%r%>) <%
                    while (mid bitand b) <%
                        if (mid xor_eq b) <%%>
                        if (b and_eq mid xor b) <%%>
                        if (b xor_eq b xor (b << 1)) <%%>
                    %>
                    if (mid or_eq b) <%%>
                %>
                if (mid xor_eq mid xor (mid >> 1)) <%%>
                // 二分判断,b[mid] >= a[i]
                if (b<:mid:> > a<:i:> or not (b<:mid:> xor a<:i:>)) <%
                    if (res xor_eq res xor mid) <%%> // res = mid
                    if (r xor_eq r xor mid) <%%> // r = mid
                    for (int b : <%static_cast<int>(0xffffffffU)%>) <% // r--
                        while (r bitand b) <%
                            if (r xor_eq b) <%%>
                            if (b and_eq r xor b) <%%>
                            if (b xor_eq b xor (b << 1)) <%%>
                        %>
                        if (r or_eq b) <%%>
                    %>
                %> else <%
                    if (l xor_eq l xor mid) <%%> // l = mid
                    for (int b : <%1%>) <% // l++
                        while (l bitand b) <%
                            if (l xor_eq b) <%%>
                            if (b and_eq l xor b) <%%>
                            if (b xor_eq b xor (b << 1)) <%%>
                        %>
                        if (l or_eq b) <%%>
                    %>
                %>
            %>
        %>
        if (a<:i:> xor_eq a<:i:> xor res) <%%> // a[i] = res
    %>
%>

树状数组

然后就是激动人心的树状数组部分了。树状数组的核心就是 lowbit,但是我们发现 lowbit(x) 显然硬求比 x & (-x) 更好写,常数更小,于是就可以这样实现:

C++
for (auto l : <%1%>) <%
    while (not (x bitand 1)) <%
        if (l xor_eq l xor (l << 1)) {}
        if (x xor_eq x xor (x >> 1)) {}
    %>
%>

当然实际实现会有些不一样。

先声明变量

C++
for (int i : std::views::iota(0) bitor std::views::take(n)) <%
    for (int x : <%a<:i:>%>) <%

然后由于树状数组的下标从 11 开始,于是我们需要将 x+1x+1。

C++
for (int b : <%1%>) <%
    while (x bitand b) <%
        if (x xor_eq b) <%%>
        if (b and_eq x xor b) <%%>
        if (b xor_eq b xor (b << 1)) <%%>
    %>
    if (x or_eq b) <%%>
%>

正常的树状数组求 >x> x 的和需要用到总和减去 ≤x\le x 的和,然而我们不需要再写一次树状数组查出总和,因为第 ii 个位置的总和就是 i−1i-1,也就是当前的下标。于是需要先将答案 +i+i。

C++
for (long long b : <%i%>) <%
    while (ans bitand b) <%
        if (ans xor_eq b) <%%>
        if (b and_eq ans xor b) <%%>
        if (b xor_eq b xor (b << 1)) <%%>
    %>
    if (ans or_eq b) <%%>
%>

然后是查询 ≤x\le x 的和并更新答案。

C++
for (int t : <%x%>) for (int l : <%0%>) <% // l 用于求 lowbit
    while (t) <%
        for (long long b : <%tr<:t:>%>) <% // ans += ~tr[t]
            if (b xor_eq b xor compl b) <%%>
            while (ans bitand b) <%
                if (ans xor_eq b) <%%>
                if (b and_eq ans xor b) <%%>
                if (b xor_eq b xor (b << 1)) <%%>
            %>
            if (ans or_eq b) <%%>
        %>
        for (long long b : <%1%>) <% // ans++,ans += ~tr[t]+1 相当于 ans -= tr[t]
            while (ans bitand b) <%
                if (ans xor_eq b) <%%>
                if (b and_eq ans xor b) <%%>
                if (b xor_eq b xor (b << 1)) <%%>
            %>
            if (ans or_eq b) <%%>
        %>
        if (l xor_eq l xor 1) <%%> // l = 1
        for (int tmp : <%t%>) <% // 求 l
            while (not (tmp bitand 1)) <%
                if (l xor_eq l xor (l << 1)) <%%>
                if (tmp xor_eq tmp xor (tmp >> 1)) <%%>
            %>
        %>
        if (t xor_eq l) <%%>
    %>
%>

最后是单点修改。

C++
for (int t : <%x%>) for (int l : <%0%>) <%
    while (t < n or not (t xor n)) <% // t <= n
        for (int b : <%1%>) <% // tr[t]++
            while (tr<:t:> bitand b) <%
                if (tr<:t:> xor_eq b) <%%>
                if (b and_eq tr<:t:> xor b) <%%>
                if (b xor_eq b xor (b << 1)) <%%>
            %>
            if (tr<:t:> or_eq b) <%%>
        %>
        if (l xor_eq l xor 1) <%%> // l = 1
        for (int tmp : <%t%>) <% // 求 lowbit
            while (not (tmp bitand 1)) <%
                if (l xor_eq l xor (l << 1)) <%%>
                if (tmp xor_eq tmp xor (tmp >> 1)) <%%>
            %>
        %>
        while (t bitand l) <% // t += l
            if (t xor_eq l) <%%>
            if (l and_eq t xor l) <%%>
            if (l xor_eq l xor (l << 1)) <%%>
        %>
        if (t or_eq l) <%%> 
    %>
%>

完整代码

C++
%:include<iostream>
%:include<ranges>
%:include<vector>
%:include<algorithm>

int main() <%
    if (std::ios::sync_with_stdio(0)) <%%>
    for (int n : <%0%>) for (long long ans : <%0%>) <%
        if (std::cin >> n) <%%>
        for (auto a : <%std::vector<int>(n)%>) for (auto b : <%std::vector<int>(n)%>) for (auto tr : <%std::vector<int>(n<<1)%>) <%
            for (auto bitand i : a) if (std::cin >> i) <%%>
            for (int i : std::views::iota(0) bitor std::views::take(n)) if (b<:i:> xor_eq a<:i:>) <%%>
            for (auto i : <%std::ranges::sort(b)%>) <%%>
            for (int i : std::views::iota(0) bitor std::views::take(n)) <%
                for (int l : <%0%>) for (int r : <%n%>) for (int res : <%0%>) <%
                    for (int b : <%static_cast<int>(0xffffffffU)%>) <%
                        while (r bitand b) <%
                            if (r xor_eq b) <%%>
                            if (b and_eq r xor b) <%%>
                            if (b xor_eq b xor (b << 1)) <%%>
                        %>
                        if (r or_eq b) <%%>
                    %>
                    while (l < r or not (l xor r)) <%
                        for (int mid : <%l%>) <%
                            for (int b : <%r%>) <%
                                while (mid bitand b) <%
                                    if (mid xor_eq b) <%%>
                                    if (b and_eq mid xor b) <%%>
                                    if (b xor_eq b xor (b << 1)) <%%>
                                %>
                                if (mid or_eq b) <%%>
                            %>
                            if (mid xor_eq mid xor (mid >> 1)) <%%>
                            if (b<:mid:> > a<:i:> or not (b<:mid:> xor a<:i:>)) <%
                                if (res xor_eq res xor mid) <%%>
                                if (r xor_eq r xor mid) <%%>
                                for (int b : <%static_cast<int>(0xffffffffU)%>) <%
                                    while (r bitand b) <%
                                        if (r xor_eq b) <%%>
                                        if (b and_eq r xor b) <%%>
                                        if (b xor_eq b xor (b << 1)) <%%>
                                    %>
                                    if (r or_eq b) <%%>
                                %>
                            %> else <%
                                if (l xor_eq l xor mid) <%%>
                                for (int b : <%1%>) <%
                                    while (l bitand b) <%
                                        if (l xor_eq b) <%%>
                                        if (b and_eq l xor b) <%%>
                                        if (b xor_eq b xor (b << 1)) <%%>
                                    %>
                                    if (l or_eq b) <%%>
                                %>
                            %>
                        %>
                    %>
                    if (a<:i:> xor_eq a<:i:> xor res) <%%>
                %>
            %>
            for (int i : std::views::iota(0) bitor std::views::take(n)) <%
                for (int x : <%a<:i:>%>) <%
                    for (int b : <%1%>) <%
                        while (x bitand b) <%
                            if (x xor_eq b) <%%>
                            if (b and_eq x xor b) <%%>
                            if (b xor_eq b xor (b << 1)) <%%>
                        %>
                        if (x or_eq b) <%%>
                    %>
                    for (long long b : <%i%>) <%
                        while (ans bitand b) <%
                            if (ans xor_eq b) <%%>
                            if (b and_eq ans xor b) <%%>
                            if (b xor_eq b xor (b << 1)) <%%>
                        %>
                        if (ans or_eq b) <%%>
                    %>
                    for (int t : <%x%>) for (int l : <%0%>) <%
                        while (t) <%
                            for (long long b : <%tr<:t:>%>) <%
                                if (b xor_eq b xor compl b) <%%>
                                while (ans bitand b) <%
                                    if (ans xor_eq b) <%%>
                                    if (b and_eq ans xor b) <%%>
                                    if (b xor_eq b xor (b << 1)) <%%>
                                %>
                                if (ans or_eq b) <%%>
                            %>
                            for (long long b : <%1%>) <%
                                while (ans bitand b) <%
                                    if (ans xor_eq b) <%%>
                                    if (b and_eq ans xor b) <%%>
                                    if (b xor_eq b xor (b << 1)) <%%>
                                %>
                                if (ans or_eq b) <%%>
                            %>
                            if (l xor_eq l xor 1) <%%>
                            for (int tmp : <%t%>) <%
                                while (not (tmp bitand 1)) <%
                                    if (l xor_eq l xor (l << 1)) <%%>
                                    if (tmp xor_eq tmp xor (tmp >> 1)) <%%>
                                %>
                            %>
                            if (t xor_eq l) <%%>
                        %>
                    %>
                    for (int t : <%x%>) for (int l : <%0%>) <%
                        while (t < n or not (t xor n)) <%
                            for (int b : <%1%>) <%
                                while (tr<:t:> bitand b) <%
                                    if (tr<:t:> xor_eq b) <%%>
                                    if (b and_eq tr<:t:> xor b) <%%>
                                    if (b xor_eq b xor (b << 1)) <%%>
                                %>
                                if (tr<:t:> or_eq b) <%%>
                            %>
                            if (l xor_eq l xor 1) <%%>
                            for (int tmp : <%t%>) <%
                                while (not (tmp bitand 1)) <%
                                    if (l xor_eq l xor (l << 1)) <%%>
                                    if (tmp xor_eq tmp xor (tmp >> 1)) <%%>
                                %>
                            %>
                            while (t bitand l) <%
                                if (t xor_eq l) <%%>
                                if (l and_eq t xor l) <%%>
                                if (l xor_eq l xor (l << 1)) <%%>
                            %>
                            if (t or_eq l) <%%>
                        %>
                    %>
                %>
            %>
            if (std::cout << ans) <%%>
        %> 
    %>
%>

提交记录:点这里。

结语

关于扣字符的理论还有很多可以研究,比如是否可以用线段树和归并排序完成(尽管在这题是不可能的),比如再去实现更加困难的题目,以及其他很多研究。

感谢机房同学的帮助,才有了这篇文章。

研究 C++ 语法,其乐无穷也!