两个码的Time复杂度



为什么第一个代码比第二个代码快?我尝试了这两个代码,一个给了我超过1000毫秒的时间限制,另一个在CodeForces测试

的长测试输入下工作140ms。
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int n, m, a, l, r, c(0), d(0);
scanf("%d%d", &n, &m);
while (n--)
{
scanf("%d", &a);
if (a == 1)
{
c += 1;
}
else
{
d += 1;
}
}
int up = min(c, d) * 2;
while (m--)
{
scanf("%d%d", &l, &r);
printf((r - l) % 2 == 1 && r - l < up ? "1n" : "0n");
}
return 0;
}

慢的是:

#include <bits/stdc++.h>
int main()
{
int n, ones{}, _ones{}, l, r, q, temp;
std::cin >> n >> q;
while(n--)
{
std::cin >> temp;
if(temp == 1)
ones++;
else
_ones++;
}
int common = std::min(ones, _ones) * 2;
while(q--)
{
std::cin >> l >> r;
std::cout << ((r - l) % 2 == 1 && r - l < common ? "1n" : "0n");
}
}

我不确定库导入/包含是否被计算在内,但是做#include <bits/stdc++.h>通常需要很多时间

可能正因为如此,可能只包含你需要的