I have this code here: https://codeforces.net/contest/444/submission/310588587. It is really weird that:
On custom invocation, it sometimes output 6, then after that it just do -1
The res[id][i] value never seems to be written to the correct value (2), even though I have checked for overflow and the value is correct
On my machine, it output 2 as normal.
Putting res as a global variable fixes the above.
Can anyone explains why this happen, is it a bug in my code or a bug on Codeforces?
You're using GCC-extension Variable Length Array, which is not standard C++. You need to initialize it with braces:
int a[n]
int a[n]{0}
vector<int> v[n]
vector<int> v[n]{vector<int>()}
Fixed code: 310833679