yyy_2013 @ 2023-06-27 09:10:30
// 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];
}
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;
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));
}
}
}
根据调试,我发现在样例上一输入tp就会3221226536崩掉!
但是!我下载了第一个数据,结果没崩!!!
救救孩子吧
by yyy_2013 @ 2023-06-27 09:11:45
附:评测记录
by _XHY20180718_ @ 2023-06-27 09:24:42
@yyy_2013 懒惰标记下传没清空
by JWRuixi @ 2023-06-27 10:04:48
@yyy_2013 upd
里 tag
没清,TLE
是因为 cerr
太慢了……