在 CP Editor 里或者 VS Code 的 cph 插件里使用快读速度会极慢,cin/scanf 是 300ms,那快读就会到 5000ms。不过我本来就不怎么用,所以没在意。
于是在某次写题的时候,我发现更诡异的事情:两段等效代码一段 5000ms 一段 300ms。这里只放出问题的函数,作用是输出环。
void print(int bg) {
cout << bg << " ";
int cur = ans[bg].nxt;
while (cur && cur != bg) {
cout << cur << " ";
cur = ans[cur].nxt;
}
cout << bg << "\n";
}
只要我把 while 那行换成上面一行就会 5000ms。要注意,cur 那个条件一定是冗余的。事实也证明:两份代码在 codeforces 上的运行时间一模一样,都能通过。
可在 CP Editor 里就是不一样,我对其背后的原理十分好奇。
完整代码在这