yyy_2013 @ 2023-07-09 09:47:31
已过,感谢 @XHY20180718 @JWRuixi 。
然而,这次出现了本地RE提交AC的情况!
附上Code:
// Problem: P3372 【模板】线段树 1
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3372
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> a(100005);
struct seg_tree {
int n;
vector<ll> a;
vector<ll> sum;
vector<ll> tag;
void build(int l, int r, int p) {
if (l == r) {
sum[p] = a[l];
return;
}
int m = (l + r) / 2;
build(l, m, p * 2);
build(m + 1, r, p * 2 + 1);
sum[p] = sum[p * 2] + sum[p * 2 + 1];
// cerr << l << ", " << r << ": " << sum[p] << endl;
}
seg_tree(int nn, vector<ll> x) {
n = nn;
a = vector<ll>(nn, 0);
sum = vector<ll>(4 * nn, 0);
tag = vector<ll>(4 * nn, 0);
for (int i = 1; i <= nn; i++) {
a[i] = x[i];
}
build(1, nn, 1);
}
void _upd(int l, int r, ll c, int s, int t, int p) {
if (l <= s && t <= r) {
sum[p] += c * (t - s + 1);
tag[p] += c;
return;
}
int m = (s + t) / 2;
if (tag[p]) {
sum[p * 2] += tag[p] * (m - s + 1);
sum[p * 2 + 1] += tag[p] * (t - m);
tag[p * 2] += tag[p];
tag[p * 2 + 1] += tag[p];
tag[p] = 0;
}
if (l <= m) {
_upd(l, r, c, s, m, p * 2);
}
if (r > m) {
_upd(l, r, c, m + 1, t, p * 2 + 1);
}
sum[p] = sum[p * 2] + sum[p * 2 + 1];
}
ll _qry(int l, int r, int s, int t, int p) {
if (l <= s && t <= r) {
// cerr << l << " " << r << ": " << s << " " << t << ", " << sum[p] << endl;
return sum[p];
}
int m = (s + t) / 2;
if (tag[p]) {
sum[p * 2] += tag[p] * (m - s + 1);
sum[p * 2 + 1] += tag[p] * (t - m);
tag[p * 2] += tag[p];
tag[p * 2 + 1] += tag[p];
tag[p] = 0;
}
tag[p] = 0;
ll sum = 0;
if (l <= m) {
sum += _qry(l, r, s, m, p * 2);
}
if (r > m) {
sum += _qry(l, r, m + 1, t, p * 2 + 1);
}
return sum;
}
void update(int l, int r, ll val) {
_upd(l, r, val, 1, n, 1);
}
ll query(int l, int r) {
return _qry(l, r, 1, n, 1);
}
};
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
seg_tree sgt(n, a);
while (m--) {
int tp;
scanf("%d", &tp);
if (tp == 1) {
int x, y;
ll k;
scanf("%d %d %lld", &x, &y, &k);
sgt.update(x, y, k);
} else {
int x, y;
scanf("%d %d", &x, &y);
printf("%lld\n", sgt.query(x, y));
}
}
}
样例在CP Editor测试还是3221226356……
P.S. 验证码2ak6祭