改写以下c++代码,改变原始代码的思路和结构,但保持了代码准确性:#include<bits/stdc++.h> using namespace std; #define int long long #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr) #define DEBUG(X) cout << #X << ": " << X << '\n' #define ls p << 1 #define rs p << 1 | 1 typedef pair<int, int> PII; const int N = 2e5 + 10, INF = 0x3f3f3f3f; struct sa { int l, r, dt, mn; }; sa tr[N << 2]; int a[N]; void pushup(int p) { tr[p].mn = min(tr[ls].mn, tr[rs].mn); } void pushdown(int p) // 父亲的帐加在儿子身上 { tr[ls].dt += tr[p].dt; tr[rs].dt += tr[p].dt; // 儿子账本发生了变化,所以自身的属性也要变 tr[ls].mn += tr[p].dt; tr[rs].mn += tr[p].dt; // 父亲账本清0 tr[p].dt = 0; } void build(int p, int l, int r) { tr[p] = {l, r, 0, a[l]}; if (l == r) // 是叶子就返回 return; int mid = l + r >> 1; // 不是叶子就裂开 build(ls, l, mid); build(rs, mid + 1, r); pushup(p); } void update(int p, int L, int R, int d) // 大写的L,R代表数组的区间LR { if (tr[p].l >= L && tr[p].r <= R) // 覆盖了区间就修改 { tr[p].dt += d; tr[p].mn += d; return; } int mid = tr[p].l + tr[p].r >> 1; // 没覆盖就裂开 // 先pushdown,最后pushup pushdown(p); // 看mid在哪边子树里,就进哪边 if (L <= mid) update(ls, L, R, d); if (R > mid) update(rs, L, R, d); pushup(p); } int query(int p, int L, int R) { if (tr[p].l >= L && tr[p].r <= R) { return tr[p].mn; } int mid = tr[p].l + tr[p].r >> 1; pushdown(p); int res = INF; if (L <= mid) res = min(res, query(ls, L, R)); if (R > mid) res = min(res, query(rs, L, R)); return res; } int n, m; signed main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); build(1, 1, n); scanf("%lld", &m); while (m--) { int l, r; char c; scanf("%lld %lld%c", &l, &r, &c); l++, r++; if (c == '\n') { if (l <= r) printf("%lld\n", query(1, l, r)); else printf("%lld\n", min(query(1, 1, r), query(1, l, n))); } else { int d; scanf("%lld", &d); if (l <= r) update(1, l, r, d); else update(1, 1, r, d), update(1, l, n, d); } } return 0; }
时间: 2024-04-18 08:23:22 浏览: 80
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
#define int long long
const int N = 2e5 + 10, INF = 0x3f3f3f3f;
struct SegmentTree {
int l, r, dt, mn;
};
vector<SegmentTree> tr;
vector<int> a;
void pushup(int p) {
tr[p].mn = min(tr[p << 1].mn, tr[p << 1 | 1].mn);
}
void pushdown(int p) {
tr[p << 1].dt += tr[p].dt;
tr[p << 1 | 1].dt += tr[p].dt;
tr[p << 1].mn += tr[p].dt;
tr[p << 1 | 1].mn += tr[p].dt;
tr[p].dt = 0;
}
void build(int p, int l, int r) {
tr[p] = {l, r, 0, a[l]};
if (l == r) return;
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}
void update(int p, int L, int R, int d) {
if (tr[p].l >= L && tr[p].r <= R) {
tr[p].dt += d;
tr[p].mn += d;
return;
}
pushdown(p);
int mid = (tr[p].l + tr[p].r) >> 1;
if (L <= mid) update(p << 1, L, R, d);
if (R > mid) update(p << 1 | 1, L, R, d);
pushup(p);
}
int query(int p, int L, int R) {
if (tr[p].l >= L && tr[p].r <= R) {
return tr[p].mn;
}
pushdown(p);
int mid = (tr[p].l + tr[p].r) >> 1;
int res = INF;
if (L <= mid) res = min(res, query(p << 1, L, R));
if (R > mid) res = min(res, query(p << 1 | 1, L, R));
return res;
}
signed main() {
int n;
scanf("%lld", &n);
a.resize(n + 1);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
tr.resize(4 * n);
build(1, 1, n);
int m;
scanf("%lld", &m);
while (m--) {
int l, r;
char c;
scanf("%lld %lld%c", &l, &r, &c);
l++, r++;
if (c == '\n') {
if (l <= r) printf("%lld\n", query(1, l, r));
else printf("%lld\n", min(query(1, 1, r), query(1, l, n)));
} else {
int d;
scanf("%lld", &d);
if (l <= r) update(1, l, r, d);
else update(1, 1, r, d), update(1, l, n, d);
}
}
return 0;
}
阅读全文