不引用懒惰标记的方法,会超时
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 1e6 + 10;
int nums[N];
typedef long long ll;
struct Tree {
int l, r;
ll sum;
int tag;
}tree[N<<2];
void pushup(int u) {
tree[u].sum = tree[u << 1].sum + tree[u << 1 | 1].sum;
}
void build(int u, int l, int r) {
if (l == r) {
tree[u] = { l,r,nums[l] };
return;
}
tree[u] = { l,r };
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void pushdown(int u, int ln, int rn) {
if (tree[u].tag) {
tree[u << 1].tag += tree[u].tag;
tree[u << 1 | 1].tag += tree[u].tag;
tree[u << 1].sum += tree[u].tag * ln;
tree[u << 1 | 1].sum += tree[u].tag * rn;
tree[u].tag = 0;
}
}
ll query(int u, int L, int R) {
int l = tree[u].l, r = tree[u].r;
int mid = (l + r) >> 1;
if (l >= L && r <= R) {
return tree[u].sum;
}
pushdown(u, mid - l + 1, r - mid);//有懒惰标记时要下推标记
ll sum = 0;
if (L <= mid) sum += query(u << 1, L, R);
if (R > mid) sum += query(u << 1 | 1, L, R);
return sum;
}
void modify_span(int u, int L,int R, int val) {
int l = tree[u].l, r = tree[u].r;
int mid = (l + r) >> 1;
if (l == r) {
tree[u].sum += val;
return;
}
if(L <= mid)modify_span(u << 1, L, R, val);
if(R > mid) modify_span(u << 1 | 1, L, R, val);
pushup(u);
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> nums[i];
}
build(1,1,n);
while (m--) {//操作数
int op;
cin>>op;
if (op == 1) {
int x, y, k;
cin >> x >> y >> k;
modify_span(1, x, y, k);
}
else {
int x, y;
cin >> x >> y;
cout << query(1, x, y) << endl;
}
}
return 0;
}
记得sum、query要开long long 否则会WA
下面将介绍一种引入一种不超时的方法,时间复杂度变为O(nlogn)
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 1e6 + 10;
int nums[N];
typedef long long ll;
struct Tree {
int l, r;
ll sum;
int tag;
}tree[N<<2];
void pushup(int u) {
tree[u].sum = tree[u << 1].sum + tree[u << 1 | 1].sum;
}
void build(int u, int l, int r) {
if (l == r) {
tree[u] = { l,r,nums[l] };
return;
}
tree[u] = { l,r };
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void pushdown(int u, int ln, int rn) {
if (tree[u].tag) {
tree[u << 1].tag += tree[u].tag;
tree[u << 1 | 1].tag += tree[u].tag;
tree[u << 1].sum += tree[u].tag * ln;
tree[u << 1 | 1].sum += tree[u].tag * rn;
tree[u].tag = 0;
}
}
ll query(int u, int L, int R) {
int l = tree[u].l, r = tree[u].r;
int mid = (l + r) >> 1;
if (l >= L && r <= R) {
return tree[u].sum;
}
pushdown(u, mid - l + 1, r - mid);//有懒惰标记时要下推标记
ll sum = 0;
if (L <= mid) sum += query(u << 1, L, R);
if (R > mid) sum += query(u << 1 | 1, L, R);
return sum;
}
void modify_span(int u, int L,int R, int val) {
int l = tree[u].l, r = tree[u].r;
int mid = (l + r) >> 1;
if (l >= L && r <= R) {
tree[u].sum += (r - l + 1) * val;
tree[u].tag += val;
pushdown(u, mid - l + 1, r - mid);
return;
}
pushdown(u, mid - l + 1, r - mid);
if(L <= mid)modify_span(u << 1, L, R, val);
if(R > mid) modify_span(u << 1 | 1, L, R, val);
pushup(u);
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> nums[i];
}
build(1,1,n);
while (m--) {//操作数
int op;
cin>>op;
if (op == 1) {
int x, y, k;
cin >> x >> y >> k;
modify_span(1, x, y, k);
}
else {
int x, y;
cin >> x >> y;
cout << query(1, x, y) << endl;
}
}
return 0;
}