#6029. 「雅礼集训 2017 Day1」市场 (势能线段树)
PS:这题正数与负数都需要向下取整,也就是说-4 / 3 = -2
解题思路:
很玄学的写法
线段树上维护最大值max和最小值min,可以发现对于除法而言(max / x) - (min / x)会导致max和min的差值越来越小,这个可以理解为势能的减小。对于一个区间而言,若max和min符号相同 且abs(max - max / x) == abs(min - min / x)那么区间所有数字的除法可以看做减法,都减去abs(max - max/ x),但是还有add操作,这个可能会导致势能增加,不太懂为什么这样就能过,感觉很玄学。
#include <bits/stdc++.h>
using ll = long long;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f * 2;
typedef std::pair<int, int> PII;
int n, m;
int a[N];
ll mn[N << 2], mx[N << 2];
ll sum[N << 2], add[N << 2];
#define ls u << 1
#define rs u << 1 | 1
inline void pushup(int u) {
sum[u] = sum[u << 1] + sum[u << 1 | 1];
mn[u] = std::min(mn[ls], mn[rs]);
mx[u] = std::max(mx[ls], mx[rs]);
}
inline void pushdown(int u, int L, int R) {
if (add[u]) {
int mid = L + R >> 1;
add[ls] += add[u];
add[rs] += add[u];
sum[ls] += 1ll * (mid - L + 1) * add[u];
sum[rs] += 1ll * (R - mid) * add[u];
mn[ls] += add[u];
mn[rs] += add[u];
mx[ls] += add[u];
mx[rs] += add[u];
add[u] = 0;
}
}
inline void build(int u, int l, int r) {
if (l == r) {
sum[u] = mx[u] = mn[u] = a[l];
return ;
}
int mid = l + r >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
pushup(u);
}
inline void DIV(int u, int L, int R, int l, int r, int x) {
if (L >= l && R <= r) {
if (mx[u] >= 0 && mn[u] >= 0) {
ll t1 = mx[u] / x, t2 = mn[u] / x;
if (abs(mx[u] - t1) == abs(mn[u] - t2)) {
sum[u] -= 1ll * (R - L + 1) * abs(mx[u] - t1);
add[u] -= abs(t1 - mx[u]);
mn[u] -= abs(mx[u] - t1);
mx[u] -= abs(mx[u] - t1);
} else {
pushdown(u, L, R);
int mid = L + R >> 1;
if (l <= mid) DIV(ls, L, mid, l, r, x);
if (r > mid) DIV(rs, mid + 1, R, l, r, x);
pushup(u);
}
} else if (mx[u] <= 0 && mn[u] <= 0){
ll t1 = (mx[u] - x + 1) / x, t2 = (mn[u] - x + 1) / x;
if (abs(mx[u] - t1) == abs(mn[u] - t2)) {
sum[u] += 1ll * (R - L + 1) * abs(mx[u] - t1);
add[u] += abs(t1 - mx[u]);
mn[u] += abs(mx[u] - t1);
mx[u] += abs(mx[u] - t1);
} else {
pushdown(u, L, R);
int mid = L + R >> 1;
if (l <= mid) DIV(ls, L, mid, l, r, x);
if (r > mid) DIV(rs, mid + 1, R, l, r, x);
pushup(u);
}
} else {
pushdown(u, L, R);
int mid = L + R >> 1;
if (l <= mid) DIV(ls, L, mid, l, r, x);
if (r > mid) DIV(rs, mid + 1, R, l, r, x);
pushup(u);
}
return ;
}
pushdown(u, L, R);
int mid = L + R >> 1;
if (l <= mid) DIV(ls, L, mid, l, r, x);
if (r > mid) DIV(rs, mid + 1, R, l, r, x);
pushup(u);
}
inline void ADD(int u, int L, int R, int l, int r, int x) {
if (L >= l && R <= r) {
add[u] += x;
sum[u] += 1ll * (R - L + 1) * x;
mn[u] += x;
mx[u] += x;
return ;
}
pushdown(u, L, R);
int mid = L + R >> 1;
if (l <= mid) ADD(ls, L, mid, l, r, x);
if (r > mid) ADD(rs, mid + 1, R, l, r, x);
pushup(u);
}
inline ll querymin(int u, int L, int R, int l, int r) {
if (L >= l && R <= r) return mn[u];
pushdown(u, L, R);
int mid = L + R >> 1;
ll ans = INF;
if (l <= mid) ans = querymin(ls, L, mid, l, r);
if (r > mid) ans = std::min(ans, querymin(rs, mid + 1, R, l, r));
pushup(u);
return ans;
}
inline ll querysum(int u, int L, int R, int l, int r) {
if (L >= l && R <= r) return sum[u];
pushdown(u, L, R);
int mid = L + R >> 1;
ll ans = 0;
if (l <= mid) ans = querysum(ls, L, mid, l, r);
if (r > mid) ans += querysum(rs, mid + 1, R, l, r);
pushup(u);
return ans;
}
inline void solve() {
std::cin >> n >> m;
for (int i = 1; i <= n; i ++)
std::cin >> a[i];
build(1, 1, n);
while (m --) {
int op, l, r, x;
std::cin >> op >> l >> r;
l ++, r ++;
if (op == 1) {
std::cin >> x;
ADD(1, 1, n, l, r, x);
} else if (op == 2) {
std::cin >> x;
DIV(1, 1, n, l, r, x);
} else if (op == 3) {
std::cout << querymin(1, 1, n, l, r) << '\n';
} else {
std::cout << querysum(1, 1, n, l, r) << '\n';
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int _ = 1;
//std::cin >> _;
while (_ --) solve();
return 0;
}