我试图从编码网站解决方案中理解代码,但无法弄清楚很多事情。我可以理解 BIT 和 fenwick 部分,但无论我不明白,我都写在下面:
此代码的来源是http://www.codechef.com/viewsolution/1816336
如果有人解释一下,那将非常有帮助为什么我们倾向于使用这些概念而不是使用标准定义的输入输出函数:
char ioSpace[500000 * 17 + 128];
unsigned popcnt(unsigned x)
{
#ifndef ONLINE_JUDGE
return __builtin_popcount(x);
#else
unsigned ret;
asm("popcntl %1, %0;":"=r"(ret) :"r"(x));
return ret;
#endif
}
unsigned readUInt(char*& readPos)
{
unsigned num = 0;
unsigned c;
while ((c = *readPos++) >= '0')
num = num * 10 + (c - '0');
return num;
}
template<unsigned D>
void writeDigit(char*& writePos, unsigned& advance, unsigned& num)
{
unsigned digit = num / D;
num %= D;
if (digit)
advance = 1;
*writePos = digit + '0';
writePos += advance;
}
void writeUInt(char*& writePos, unsigned num)
{
unsigned advance = 0;
writeDigit<100000>(writePos, advance, num);
writeDigit<10000>(writePos, advance, num);
writeDigit<1000>(writePos, advance, num);
writeDigit<100>(writePos, advance, num);
writeDigit<10>(writePos, advance, num);
advance = 1; // for zero number
writeDigit<1>(writePos, advance, num);
*writePos++ = '\n';
}
然后在主输入功能中:
read(STDIN_FILENO, ioSpace, sizeof(ioSpace));
有人能解释一下整个过程吗,因为我一直试图在我的 C++ 编程中加入这种风格,但由于缺乏理解而不可避免地失败了。