题目:https://acm.hdu.edu.cn/showproblem.php?pid=6315 。
TLE了,求调,还是复杂度不对?
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n, q, b[100005];
struct node {int l, r, maxa, minb, ans, lazy;} t[400005];
void pushup(int xb) {
t[xb].maxa = max(t[xb * 2].maxa, t[xb * 2 + 1].maxa);
t[xb].minb = min(t[xb * 2].minb, t[xb * 2 + 1].minb);
t[xb].ans = t[xb * 2].ans + t[xb * 2 + 1].ans;
}
void pushdown(int xb) {
if (t[xb].lazy) {
int s = t[xb].lazy;
t[xb * 2].lazy += s; t[xb * 2 + 1].lazy += s;
t[xb * 2].maxa += s; t[xb * 2 + 1].maxa += s;
t[xb].lazy = 0;
}
}
void buld(int xb, int l, int r) {
t[xb].l = l; t[xb].r = r;
if (l == r) {
t[xb].maxa = 0; t[xb].minb = b[l];
t[xb].ans = 0; t[xb].lazy = 0;
return ;
}
int mid = (l + r) / 2;
buld(xb * 2, l, mid); buld(xb * 2 + 1, mid + 1, r);
pushup(xb);
}
void add(int xb, int l, int r) {
if (t[xb].l == 0) return ;
if (l <= t[xb].l && t[xb].r <= r) {
t[xb].maxa++;
if (t[xb].maxa < t[xb].minb) {t[xb].lazy++; return ;}
if (t[xb].l == t[xb].r && t[xb].maxa >= t[xb].minb) {
t[xb].ans++;
t[xb].minb += b[t[xb].l];
return ;
}
}
pushdown(xb);
int mid = (t[xb].l + t[xb].r) / 2;
if (l <= mid) add(xb * 2, l, r);
if (r > mid) add(xb * 2 + 1, l, r);
pushup(xb);
}
int Ask(int xb, int l, int r) {
if (l <= t[xb].l && t[xb].r <= r) return t[xb].ans;
pushdown(xb);
int mid = (t[xb].l + t[xb].r) / 2, res = 0;
if (l <= mid) res += Ask(xb * 2, l, r);
if (r > mid) res += Ask(xb * 2 + 1, l, r);
return res;
}
signed main() {
std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
while (cin >> n) {
cin >> q;
for (int i = 1; i <= n; i++) cin >> b[i];
buld(1, 1, n);
for (int i = 1; i <= q; i++) {
string op; int l, r; cin >> op >> l >> r;
if (op == "add") add(1, l, r);
else cout << Ask(1, l, r) << endl;
}
}
return 0;
}