在C ++中,当我将作为参数传递给构造函数的数组变量分配给作为指针的类变量时,它将获取垃圾值。是什么原因造成的?
在这里,在SegTree构造函数中,将setbits(参数值)分配给类变量(在构造函数中注释的第一行)会在其中保存垃圾值。
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// #define int long long
#define ends ' '
#define endl '\n'
#define pb push_back
#define INFYNODE node(INT_MAX, INT_MIN)
const int maxn = 2e5+5;
int a[maxn][6];
class node
{
public:
int mn, mx;
node(){mn = INT_MAX, mx = INT_MIN;}
node(int _mn, int _mx){mn = _mn, mx = _mx;}
};
class SegTree
{
public:
int *setbits, k, n;
int *arr;
node *tree;
SegTree(int n, int k, int Setbits[])
{
// this->setbits = Setbits;
this->setbits = new int[k];
this->k = k;
this->n = n;
this->arr = new int[n+1];
this->tree = new node[(n+1)<<2];
for(int i = 0; i < k; ++i)setbits[i] = Setbits[i];
for(int i = 1; i <= n; ++i)
{
arr[i] = 0;
for(int j = 0; j < k; ++j)
{
arr[i] += a[i][j]*(setbits[j] ? -1 : 1);
}
}
build(1, 1, n);
}
void display()
{
// cout << this->setbits[0] << '\n';
for(int i = 1; i <= (n<<2); i++)
{
cerr << i << ' ' << tree[i].mn << ' ' << tree[i].mx << '\n';
}
}
void merge(node &a, node &b, node &c)
{
c.mn = min(a.mn, b.mn);
c.mx = max(a.mx, b.mx);
}
void build(int cur, int l, int r)
{
if(l == r)
{
tree[cur].mn = tree[cur].mx = arr[l];
return;
}
int m = (l+r)>>1;
build(cur<<1, l, m);
build((cur<<1)+1, m+1, r);
merge(tree[(cur<<1)], tree[(cur<<1)+1], tree[cur]);
}
node query(int cur, int l, int r, int ql, int qr)
{
if(l == ql and r == qr)
return tree[cur];
int m = (l+r)>>1;
if(qr <= m)return query(cur<<1, l, m, ql, qr);
else if(ql > m)return query((cur<<1)+1, m+1, r, ql, qr);
else
{
node n1 = query(cur<<1, l, m, ql, m);
node n2 = query((cur<<1)+1, m+1, r, m+1, qr);
node ret;
merge(n1, n2, ret);
return ret;
}
}
void update_array(int idx, int new_val[])
{
arr[idx] = 0;
for(int i = 0; i < k; ++i)
{
a[idx][i] = new_val[i];
arr[idx] += new_val[i]*(setbits[i] ? -1 : 1);
}
update(1, 1, n, idx);
}
void update(int n, int l, int r, int idx)
{
if(l == r)
{
tree[n].mn = tree[n].mx = arr[idx];
return;
}
int m = (l+r)>>1;
if(idx <= m)update(n<<1, l, m, idx);
else update((n<<1)+1, m+1, r, idx);
merge(tree[(n<<1)], tree[(n<<1)+1], tree[n]);
}
};
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
IOS;
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; ++i)
for(int j = 0; j < k; ++j)
cin >> a[i][j];
int lim = 1<<k;
vector<SegTree*> v(lim);
for(int i = 0; i < lim; ++i)
{
int setbits[k];
memset(setbits, 0, sizeof setbits);
for(int j = 0; j < k; ++j)
{
if(i & (1<<j))
{
setbits[j] = 1;
}
}
v[i] = new SegTree(n, k, setbits);
}
int q;
cin >> q;
int new_val[k];
int haha = 0;
while(q--)
{
int type;
cin >> type;
if(type == 1)
{
int j;
cin >> j;
for(int i = 0; i < k; ++i)
{
cin >> new_val[i];
}
for(int i = 0; i < lim; ++i)
{
v[i]->update_array(j, new_val);
// v[i]->display();
// cerr << "\n\n";
}
}
else
{
int l, r;
cin >> l >> r;
int ans = 0;
for(int i = 0; i < lim; ++i)
{
node tmp = v[i]->query(1, 1, n, l, r);
ans = max(ans, tmp.mx-tmp.mn);
}
cout << ans << endl;
}
}
return 0;
}
答案 0 :(得分:0)
如果我正确理解了您的问题,则说明您在复制数组时遇到问题。一点点Google-Fu即可解决此问题。 EG:http://www.cplusplus.com/forum/general/199358/
但是,您的问题是模棱两可的,因此我可能遇到了错误的问题。查看评论。