题意:
已知\(n\)个数字,进行以下操作:
- \(1.\)给一个区间\([L,R]\) 加上一个数\(x\)
- \(2.\)把一个区间\([L,R]\) 里小于\(x\) 的数变成\(x\)
- \(3.\)把一个区间\([L,R]\) 里大于\(x\) 的数变成\(x\)
- \(4.\)求区间\([L,R]\)的和
- \(5.\)求区间\([L,R]\)的最大值
- \(6.\)求区间\([L,R]\) 的最小值
思路:
吉司机线段树。
假如我们要进行把一个区间\([L,R]\) 里小于\(x\) 的数变成\(x\)。那么我们可以维护一个最小值\(Min\)和次小值\(sMin\)和最小值数量\(Minlen\)。那么,当\(Min\geq x\)时,显然这个区间不需要操作;当\(Min<x\)且\(sMin>x\)时,这时只要更新\(Min=x\);当\(sMin\leq x\)时,继续往下\(dfs\)。操作\(3\)也是同理。
因为这里是没设重置的标记的,所以在\(pushdown\)时如果子节点和父节点产生冲突,那么以父节点为准。
复杂度\(O(mlog^2n)\),\(m\)为操作数。
有点卡常。
代码:
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 5e5 + 5;
const int MAXM = 3e6;
const ll MOD = 998244353;
const ull seed = 131;
const int INF = 1 << 30;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
int a[maxn];
int Max[maxn << 2], Min[maxn << 2], sMax[maxn << 2], sMin[maxn << 2];
int Maxlen[maxn << 2], Minlen[maxn << 2];
ll sum[maxn << 2];
int lazy[maxn << 2];
inline void pushup(int rt){ //这边建议加上inline
sum[rt] = sum[lson] + sum[rson];
if(Max[lson] < Max[rson]){
Max[rt] = Max[rson];
Maxlen[rt] = Maxlen[rson];
sMax[rt] = max(sMax[rson], Max[lson]);
}
if(Max[lson] > Max[rson]){
Max[rt] = Max[lson];
Maxlen[rt] = Maxlen[lson];
sMax[rt] = max(sMax[lson], Max[rson]);
}
if(Max[lson] == Max[rson]){
Max[rt] = Max[lson];
Maxlen[rt] = Maxlen[lson] + Maxlen[rson];
sMax[rt] = max(sMax[lson], sMax[rson]);
}
if(Min[lson] > Min[rson]){
Min[rt] = Min[rson];
Minlen[rt] = Minlen[rson];
sMin[rt] = min(sMin[rson], Min[lson]);
}
if(Min[lson] < Min[rson]){
Min[rt] = Min[lson];
Minlen[rt] = Minlen[lson];
sMin[rt] = min(sMin[lson], Min[rson]);
}
if(Min[lson] == Min[rson]){
Min[rt] = Min[lson];
Minlen[rt] = Minlen[lson] + Minlen[rson];
sMin[rt] = min(sMin[lson], sMin[rson]);
}
}
inline void pushdown(int rt, int l, int r){
int m = (l + r) >> 1;
if(lazy[rt]){
sum[lson] += 1LL * (m - l + 1) * lazy[rt];
sum[rson] += 1LL * (r - m) * lazy[rt];
Max[lson] += lazy[rt];
Max[rson] += lazy[rt];
sMax[lson] += lazy[rt];
sMax[rson] += lazy[rt];
Min[lson] += lazy[rt];
Min[rson] += lazy[rt];
sMin[lson] += lazy[rt];
sMin[rson] += lazy[rt];
lazy[lson] += lazy[rt];
lazy[rson] += lazy[rt];
lazy[rt] = 0;
}
if(Max[lson] > Max[rt]){ //要和父节点保持一致
if(sMax[lson] == Max[lson]) sMax[lson] = Max[rt];
if(Min[lson] == Max[lson]) Min[lson] = Max[rt];
if(sMin[lson] == Max[lson]) sMin[lson] = Max[rt];
sum[lson] += 1LL * (Max[rt] - Max[lson]) * Maxlen[lson];
Max[lson] = Max[rt];
}
if(Max[rson] > Max[rt]){
if(sMax[rson] == Max[rson]) sMax[rson] = Max[rt];
if(Min[rson] == Max[rson]) Min[rson] = Max[rt];
if(sMin[rson] == Max[rson]) sMin[rson] = Max[rt];
sum[rson] += 1LL * (Max[rt] - Max[rson]) * Maxlen[rson];
Max[rson] = Max[rt];
}
if(Min[lson] < Min[rt]){
if(sMin[lson] == Min[lson]) sMin[lson] = Min[rt];
if(Max[lson] == Min[lson]) Max[lson] = Min[rt];
if(sMax[lson] == Min[lson]) sMax[lson] = Min[rt];
sum[lson] += 1LL * (Min[rt] - Min[lson]) * Minlen[lson];
Min[lson] = Min[rt];
}
if(Min[rson] < Min[rt]){
if(sMin[rson] == Min[rson]) sMin[rson] = Min[rt];
if(Max[rson] == Min[rson]) Max[rson] = Min[rt];
if(sMax[rson] == Min[rson]) sMax[rson] = Min[rt];
sum[rson] += 1LL * (Min[rt] - Min[rson]) * Minlen[rson];
Min[rson] = Min[rt];
}
}
void build(int l, int r, int rt){
lazy[rt] = 0;
if(l == r){
sum[rt] = Max[rt] = Min[rt] = a[l];
sMax[rt] = -INF;
sMin[rt] = INF;
Maxlen[rt] = Minlen[rt] = 1;
return;
}
int m = (l + r) >> 1;
build(l, m, lson);
build(m + 1, r, rson);
pushup(rt);
}
void add(int L, int R, int l, int r, int v, int rt){
if(L <= l && R >= r){
sum[rt] += 1LL * v * (r - l + 1);
Max[rt] += v;
Min[rt] += v;
sMax[rt] += v;
sMin[rt] += v;
lazy[rt] += v;
return;
}
pushdown(rt, l, r);
int m = (l + r) >> 1;
if(L <= m)
add(L, R, l, m, v, lson);
if(R > m)
add(L, R, m + 1, r, v, rson);
pushup(rt);
}
void Less(int L, int R, int l, int r, int v, int rt){
if(Min[rt] >= v) return;
if(L <= l && R >= r && sMin[rt] > v){//>保证Minlen不变
if(Max[rt] == Min[rt]) Max[rt] = v;
if(sMax[rt] == Min[rt]) sMax[rt] = v;
sum[rt] += 1LL * (v - Min[rt]) * Minlen[rt] ;
Min[rt] = v;
return;
}
pushdown(rt, l, r);
int m = (l + r) >> 1;
if(L <= m)
Less(L, R, l, m, v, lson);
if(R > m)
Less(L, R, m + 1, r, v, rson);
pushup(rt);
}
void More(int L, int R, int l, int r, int v, int rt){
if(Max[rt] <= v) return;
if(L <= l && R >= r && sMax[rt] < v){ //<保证Maxlen不变
if(Min[rt] == Max[rt]) Min[rt] = v;
if(sMin[rt] == Max[rt]) sMin[rt] = v;
sum[rt] += 1LL * (v - Max[rt]) * Maxlen[rt];
Max[rt] = v;
return;
}
pushdown(rt, l, r);
int m = (l + r) >> 1;
if(L <= m)
More(L, R, l, m, v, lson);
if(R > m)
More(L, R, m + 1, r, v, rson);
pushup(rt);
}
ll querySum(int L, int R, int l, int r, int rt){
if(L <= l && R >= r){
return sum[rt];
}
pushdown(rt, l, r);
int m = (l + r) >> 1;
ll ret = 0;
if(L <= m)
ret += querySum(L, R, l, m, lson);
if(R > m)
ret += querySum(L, R, m + 1, r, rson);
return ret;
}
int queryMax(int L, int R, int l, int r, int rt){
if(L <= l && R >= r){
return Max[rt];
}
pushdown(rt, l, r);
int m = (l + r) >> 1;
int MAX = -INF;
if(L <= m)
MAX = max(MAX, queryMax(L, R, l, m, lson));
if(R > m)
MAX = max(MAX, queryMax(L, R, m + 1, r, rson));
return MAX;
}
int queryMin(int L, int R, int l, int r, int rt){
if(L <= l && R >= r){
return Min[rt];
}
pushdown(rt, l, r);
int m = (l + r) >> 1;
int MIN = INF;
if(L <= m)
MIN = min(MIN, queryMin(L, R, l, m, lson));
if(R > m)
MIN = min(MIN, queryMin(L, R, m + 1, r, rson));
return MIN;
}
inline bool read(int &num){
char in;
bool IsN=false;
in = getchar();
if(in == EOF) return false;
while(in != '-' && (in < '0' || in > '9')) in = getchar();
if(in == '-'){ IsN = true; num = 0;}
else num = in - '0';
while(in = getchar(),in >= '0' && in <= '9'){
num *= 10, num += in-'0';
}
if(IsN) num = -num;
return true;
}
int main(){
int n;
read(n);
for(int i = 1; i <= n; i++) read(a[i]);
build(1, n, 1);
int m;
read(m);
while(m--){
int l, r, x, op;
read(op), read(l), read(r);
if(op <= 3) read(x);
if(op == 1) add(l, r, 1, n, x, 1);
else if(op == 2) Less(l, r, 1, n, x, 1);
else if(op == 3) More(l, r, 1, n, x, 1);
else if(op == 4) printf("%lld\n", querySum(l, r, 1, n, 1));
else if(op == 5) printf("%d\n", queryMax(l, r, 1, n, 1));
else printf("%d\n", queryMin(l, r, 1, n, 1));
}
return 0;
}