试过下载测试点,本地跑完全没问题。请问是评测机的问题吗?
Segmentation fault with invalid memory reference.
我代码里没有引用。。。
#include "bits/stdc++.h"
using namespace std;
int n, m;
vector<int> tree;
vector<int> lazy;
void build(int node, int start, int end) {
if (start == end) {
// Leaf node will have a single element
cin >> tree[node];
} else {
int mid = (start + end) / 2;
// Recurse on the left child
build(2 * node, start, mid);
// Recurse on the right child
build(2 * node + 1, mid + 1, end);
// Internal node will have the sum of both of its children
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
}
void LAZY(int node, int nodeL, int nodeR) {
if (lazy[node] != 0) {
tree[node] += (nodeR - nodeL + 1) * lazy[node]; // All leaf descendents
if (nodeL != nodeR) { // If not leaf, LAZY to children
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0; // Clear
}
}
void rangeEdit(int l, int r, int value, int node = 1, int nodeL = 0, int nodeR = n - 1) { // Default, 2-2
LAZY(node, nodeL, nodeR);
if (l > nodeR || r < nodeL) return; // No overlap, DNE/out of range, 2-3/3-2/3-3 | 1-3/3-1
if (l <= nodeL && r >= nodeR) { // Total overlap, NL ~ NR < l ~ r, 1-1
lazy[node] += value;
LAZY(node, nodeL, nodeR);
return;
}
// Partial overlap, 1-2/2-1
int mid = (nodeL + nodeR) / 2;
rangeEdit(l, r, value, node * 2, nodeL, mid);
rangeEdit(l, r, value, node * 2 + 1, mid + 1, nodeR);
tree[node] = tree[node * 2] + tree[node * 2 + 1]; // Update current node
}
int sum(int l, int r, int node = 1, int nodeL = 0, int nodeR = n - 1) { // Default, 2-2
LAZY(node, nodeL, nodeR);
// cout << endl << node << "-" << nodeL << "~" << nodeR << endl;
if (l > nodeR || r < nodeL)
return 0; // No overlap, DNE/out of range, 2-3/3-2/3-3 | 1-3/3-1
if (l <= nodeL && r >= nodeR)
return tree[node]; // Total overlap, NL ~ NR < l ~ r, 1-1
// Partial overlap, 1-2/2-1
int mid = (nodeL + nodeR) / 2;
return sum(l, r, node * 2, nodeL, mid) + sum(l, r, node * 2 + 1, mid + 1, nodeR);
}
int main() {
cin >> n >> m;
tree.resize(n * 2);
lazy.resize(n * 2);
build(1, 0, n - 1);
for (int j = 0; j < m; j++) {
int tmp;
cin >> tmp;
if (tmp == 1) {
int x, y, k;
cin >> x >> y >> k;
rangeEdit(x - 1, y - 1, k);
} else {
int x, y;
cin >> x >> y;
cout << sum(x - 1, y - 1) << endl;
}
}
return ~~(0 - 0);
}