为什么在 stl 中使用 bitset 时会出现 SIGABRT 错误?
Why do I get a SIGABRT error while using bitset in stl?
以下代码 运行 非常适合我的 g++ 编译器。但是在在线法官上提交它时,会出现 SIGABRT 错误。我读到如果 stl 元素试图访问太多内存,它们会产生这个错误。我看不到任何这样的内存使用。是伯爵吗?我已经实现了自己的计数,但它仍然给出相同的错误。
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define M 100
void range_change(bitset<M>& x, int lower, int upper, bool change){
if (change){
for (unsigned i = lower; i <= upper; ++i)
x.set(i);
}
else{
for (unsigned i = lower; i <= upper; ++i)
x.reset(i);
}
}
int count_Set(bitset<M> & x){
int count = 0;
for(int i = 0; i< 100; i++){
if(1 & x[i]) count++;
}
return count;
}
bitset<100> houses;
int main(){
fastio;
int t;
cin>> t;
int m, x, y;
int reach{0};
int lower{0}, upper{0};
// int t = 1;
while(t--){
houses.set();
// cout<< houses;
cin >> m >> x >> y;
reach = x*y;
vector<int> cat(m, 0);
for(auto& u: cat){
cin>> u;
lower = u - reach -1;
if(lower < 0) lower = 0;
upper = u + reach-1;
if(upper > 100) upper = 99;
range_change(houses, lower, upper, false);
}
// cout<< houses << "\n";
cout<< houses.count() << "\n";
// cout<< count_Set(houses) << "\n";
}
return 0;
}
略读源码,我看到的是这样的:
#define M 100
...
bitset<100> houses;
为了安全起见,您似乎想使用 M
而不是 100
。
然后这里:
if(upper > 100) upper = 99;
这将在 upper = 100
处中断 建议:
if(upper >= M) upper = M - 1;
以下代码 运行 非常适合我的 g++ 编译器。但是在在线法官上提交它时,会出现 SIGABRT 错误。我读到如果 stl 元素试图访问太多内存,它们会产生这个错误。我看不到任何这样的内存使用。是伯爵吗?我已经实现了自己的计数,但它仍然给出相同的错误。
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define M 100
void range_change(bitset<M>& x, int lower, int upper, bool change){
if (change){
for (unsigned i = lower; i <= upper; ++i)
x.set(i);
}
else{
for (unsigned i = lower; i <= upper; ++i)
x.reset(i);
}
}
int count_Set(bitset<M> & x){
int count = 0;
for(int i = 0; i< 100; i++){
if(1 & x[i]) count++;
}
return count;
}
bitset<100> houses;
int main(){
fastio;
int t;
cin>> t;
int m, x, y;
int reach{0};
int lower{0}, upper{0};
// int t = 1;
while(t--){
houses.set();
// cout<< houses;
cin >> m >> x >> y;
reach = x*y;
vector<int> cat(m, 0);
for(auto& u: cat){
cin>> u;
lower = u - reach -1;
if(lower < 0) lower = 0;
upper = u + reach-1;
if(upper > 100) upper = 99;
range_change(houses, lower, upper, false);
}
// cout<< houses << "\n";
cout<< houses.count() << "\n";
// cout<< count_Set(houses) << "\n";
}
return 0;
}
略读源码,我看到的是这样的:
#define M 100
...
bitset<100> houses;
为了安全起见,您似乎想使用 M
而不是 100
。
然后这里:
if(upper > 100) upper = 99;
这将在 upper = 100
处中断 建议:
if(upper >= M) upper = M - 1;