Can someone tell me which functions may cause SIGABRT in my code? My code is 235 lines long and I have no clue which functions can cause SIGABRT. If someone can tell me which functions are at risk, It’ll be easier for me to debug my code.
Question : ICM2004 Problem - CodeChef
It works for the sample cases and a few other larger cases.
My Code
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
const ll p =1e9 + 7;
struct matrix{
vector<vector<ll>> mat;
bool isempty=1;
matrix(int n, int m){
isempty=0;
mat.resize(n,vector<ll>(m));
}
matrix(){
mat.resize(2,vector<ll>(2));
mat[0][0]=1;
mat[1][1]=1;
}
matrix operator*(matrix a){
matrix ans(this->mat.size(),a.mat[0].size());
for(int i=0;i<this->mat.size();i++){
for(int j=0;j<a.mat[0].size();j++){
for(int k=0;k<a.mat.size();k++){
ans.mat[i][j]+=(this->mat[i][k]*a.mat[k][j])%p;
ans.mat[i][j]%=p;
}
}
}
return ans;
}
void print(){
if(isempty){
return;
}
for(int i=0;i<this->mat.size();i++){
for(int j=0;j<this->mat[0].size();j++){
cout<<this->mat[i][j]<<" ";
}
cout<<'\n';
}
}
};
matrix matpower(matrix base,ll power){
matrix ans(base.mat.size(),base.mat.size());
for(int i=0;i<base.mat.size();i++){
ans.mat[i][i]=1;
}
while(power){
if(power & 1){
ans=ans*base;
}
base=base*base;
power>>=1;
}
return ans;
}
vector<matrix> A(20, matrix(2,2));
vector<matrix> B(20, matrix(2,2));
matrix join(matrix a, matrix b){
return a*b;
}
matrix assign(int h, matrix lazy, matrix seg){
if(lazy.mat[0][1]==-1){
return B[h-1];
}
return A[h-1];
}
template<typename T>
struct lazy_segment_tree{
int n;
int H;
T base;
vector<T> segtree;
vector<T> lazytree;
T (*join)(T,T);
T (*assign)(int,T,T);
lazy_segment_tree(vector<T> &seq, T (*merge)(T,T), T (*create)(int, T,T), T defvalue){
join=merge;
assign=create;
base=defvalue;
n=seq.size();
H = sizeof(int) * 8 - __builtin_clz(n);
segtree.resize(2*n, T());
lazytree.resize(n, T());
for(int i=0;i<n;i++){
segtree[n+i]=seq[i];
}
for(int i=n-1;i>=1;i--){
segtree[i]=join(segtree[(i<<1)], segtree[(i<<1)|1]);
}
}
void calc(int pos, int h){
if(lazytree[pos].isempty){
segtree[pos]=join(segtree[(pos<<1)],segtree[(pos<<1)|1]);
}
else{
segtree[pos]=assign(h, lazytree[pos],segtree[pos]);
}
}
void apply(int pos, T value, int h){
segtree[pos]=assign(h, value, segtree[pos]);
if(pos<n){
lazytree[pos]=value;
}
}
void updatenode(int pos){
int h=1;
pos+=n;
while(pos>1){
h*=2;
pos>>=1;
calc(pos, h);
}
}
void push(int pos){
int h=H;
int k=1<<(H-1);
for(pos+=n;h;--h, k>>=1){
int x=(pos>>h);
if(!lazytree[x].isempty){
apply((x<<1), lazytree[x], h);
apply((x<<1)|1, lazytree[x], h);
lazytree[x].isempty=1;
}
}
}
void updaterange(int l, int r, T value){
push(l);
push(r-1);
int k=1;
int l0=l, r0=r;
for(l+=n, r+=n;l<r;l>>=1, r>>=1, k++){
if(l&1){
apply(l++, value, k);
}
if(r&1){
apply(--r, value,k);
}
}
updatenode(l0);
updatenode(r0-1);
}
T query(int l, int r){
push(l);
push(r-1);
T ansl=base;
T ansr=base;
for(l+=n, r+=n;l<r;l>>=1, r>>=1){
if(l&1){
ansl=join(ansl, segtree[l++]);
}
if(r&1){
ansr=join(segtree[--r], ansr);
}
}
return join(ansl,ansr);
}
void printTree(){
cout<<"SEGMENT TREE:\n";
for(int i=0;i<2*n;i++){
cout<<i<<":\n";
segtree[i].print();
}
cout<<"LAZY TREE:\n";
for(int i=0;i<n;i++){
cout<<i<<":\n";
lazytree[i].print();
}
}
};
ll solve(){
int n,Q;
cin>>n;
vector<matrix> seq(n);
for(int i=0;i<n;i++){
char a;
cin>>a;
if(a=='A'){
seq[i]=A[0];
}
else{
seq[i]=B[0];
}
}
matrix base(2,2);
base.mat={{1,0},{0,1}};
lazy_segment_tree<matrix> segtree(seq, join, assign, base);
cin>>Q;
for(int i=0;i<Q;i++){
int type;
cin>>type;
if(type==1){
int l,r;
char c;
cin>>l>>r>>c;
--l;
if(c=='A'){
segtree.updaterange(l, r, A[0]);
}
else{
segtree.updaterange(l,r,B[0]);
}
}
else{
int l,r,P,Q;
cin>>l>>r>>P>>Q;
--l;
matrix ans(1,2);
ans.mat={{P,Q}};
ans=ans*segtree.query(l,r);
cout<<(ans.mat[0][0]+p)%p<<" "<<(ans.mat[0][1]+p)%p<<"\n";
}
}
}
void precompute(){
A[0].mat={{1,1},{-1,1}};
B[0].mat={{1,-1},{1,1}};
for(int i=1;i<20;i++){
A[i]=A[i-1]*A[i-1];
B[i]=B[i-1]*B[i-1];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >>t;
precompute();
while(t--){
solve();
}
}