蒟蒻求助 样例都过不了

P3372 【模板】线段树 1

你的jia函数应该没问题,问题应该出在he函数上 ```cpp #include<iostream> using namespace std; #define int long long int st[1145140], lazy[1145140], a[1145140], n, t, ll, rr, d, com; void build(int l, int r, int where) { if (l == r) { st[where] = a[l]; return; } else { int m = l + ((r - l) / 2); build(l, m, where * 2); build(m + 1, r, where * 2 + 1); st[where] = st[where * 2] + st[where * 2 + 1]; } } void add(int l, int r, int nl, int nr, int where, int adnum) { if (nl >= l && nr <= r) { st[where] += (nr - nl + 1) * adnum; lazy[where] += adnum; return; } else { int m = nl + ((nr - nl) / 2); if (lazy[where] != 0) { st[where * 2] += lazy[where] * (m - nl + 1); st[where * 2 + 1] += lazy[where] * (nr - m); lazy[where * 2] += lazy[where]; lazy[where * 2 + 1] += lazy[where]; lazy[where] = 0; } if (l <= m) { add(l, r, nl, m, where * 2, adnum); } if (r > m) { add(l, r, m + 1, nr, where * 2 + 1, adnum); } st[where] = st[where * 2] + st[where * 2 + 1]; } } int sumup(int l, int r, int nl, int nr, int where) { int m = nl + ((nr - nl) / 2); int sum = 0; if (nl >= l && nr <= r) { return st[where]; } if (lazy[where]) { st[where * 2] += lazy[where] * (m - nl + 1); st[where * 2 + 1] += lazy[where] * (nr - m); lazy[where * 2] += lazy[where]; lazy[where * 2 + 1] += lazy[where]; lazy[where] = 0; } if (l <= m) { sum += sumup(l, r, nl, m, where * 2); } if (r > m) { sum += sumup(l, r, m + 1, nr, where * 2 + 1); } return sum; } signed main() { cin >> n >> t; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, n, 1); for (int i = 1; i <= t; i++) { cin >> com; if (com == 1) { cin >> ll >> rr >> d; add(ll, rr, 1, n, 1, d); } else { cin >> ll >> rr; cout << sumup(ll, rr, 1, n, 1) << '\n'; } } } `````` 这里贴一份我的,你可以看一下这个sumup函数的思路,应该会有帮助 @[sto_0616allen_orz](/user/1037997)
by Tainaka_Ritsu @ 2023-12-26 00:09:21


@[Tainaka_Ritsu](/user/1067742) 谢谢已关
by sto_0616allen_orz @ 2023-12-26 20:57:23


|