子串的最大差
Posted XINNNNNNNYU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了子串的最大差相关的知识,希望对你有一定的参考价值。
题意
给你一个数组,求所有区间的(最大值-最小值)的和。
思路
对于每个数计算它的贡献。贡献分为两部分,一部分是作为最大值,一部分是作为最小值。当a[i]作为最大值,你要找到它左边第一个大于等于(可能存在连续相等的数,避免重复左闭右开)他的数位置l和右边第一个比他大的数位置r,那么当前树的贡献是a[i] * (i-l)* (r-i)。最小值同理不过是减去。
这里介绍两种寻找每个数的l,r的方法。
- 单调栈
好像就相当是个板子了 - 二分+st表
对于每个数a[i]二分它的左边【0,i-1】和右边[i+1,N],然后用st表来check。一段区间的前缀或后缀的最值是单调的,满足二分。
单调栈代码
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define int long long
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
int qmi(int a, int k, int p)int res = 1;while (k)if (k & 1) res = (ll)res * a % p;a = (ll)a * a % p;k >>= 1;return res;
int qpow(int a,int b)int res = 1;while(b)if(b&1) res *= a;b>>=1;a*=a;return res;
int mo(int x,int p)return x = ((x%p)+p)%p;
int gcd(int a,int b)return b?gcd(b,a%b):a;
const int maxn = 1e6+7;
const int mod = 1e9+7;
const double eps = 1e-6;
int dx[] = 0,0,1,-1, dy[] = 1,-1,0,0;
int T = 1,N,M,K,Q;
void solve()
cin >> N;
vector<int> a(N+2);
for (int i = 1; i <= N; i ++) cin >> a[i];
a[0] = a[N+1] = 1e9;//设置哨兵,
vector<int> maxl(N+2),maxr(N+2),minl(N+2),minr(N+2);
stack<pair<int, int>> q;//pair是值和位置
q.push(make_pair(a[0], 0));
for (int i = 1; i <= N + 1; i ++) //i is max
while (q.size() && q.top().first < a[i])
maxr[q.top().second] = i;//a[i]作为栈顶右边的最大值
q.pop();
if (q.size()) maxl[i] = q.top().second;//栈顶作为a[i]左边的最大值
q.push(make_pair(a[i], i));//a[i]入栈
while(q.size()) q.pop();//清空栈内元素
a[0] = a[N+1] = -1e9;//哨兵
q.push(make_pair(a[0], 0));
//求最小值同理
for (int i = 1; i <= N + 1; i ++) //i is min
while (q.size() && q.top().first > a[i])
minr[q.top().second] = i;
q.pop();
if (q.size()) minl[i] = q.top().second;
q.push(make_pair(a[i], i));
int ans = 0;
for (int i = 1; i <= N; i ++) //枚举每个位置的贡献
int r = maxr[i] - i;
int l = i - maxl[i];
int cnt = r * l;
r = minr[i] - i;
l = i - minl[i];
ans += a[i] * cnt;
cout << ans << endl;
signed main()
ios::sync_with_stdio(false);cin.tie(0);
// cin >> T;
for (int i = 1; i <= T; i ++) solve();
return (0-0); //<3
二分+st表代码
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define int long long
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
int qmi(int a, int k, int p)int res = 1;while (k)if (k & 1) res = (ll)res * a % p;a = (ll)a * a % p;k >>= 1;return res;
int qpow(int a,int b)int res = 1;while(b)if(b&1) res *= a;b>>=1;a*=a;return res;
int mo(int x,int p)return x = ((x%p)+p)%p;
int gcd(int a,int b)return b?gcd(b,a%b):a;
const int maxn = 1e6+7;
const int mod = 1e9+7;
const double eps = 1e-6;
int dx[] = 0,0,1,-1, dy[] = 1,-1,0,0;
int T = 1,N,M,K,Q;
vector<int> A(maxn), dp(maxn), f(maxn), B(maxn);
int a, b, c, x, t;
template<typename T>
struct RMQ
static int highest_bit(unsigned x)
return x == 0 ? -1 : 31 - __builtin_clz(x);
int n = 0;
vector<vector<T>> range_max;
RMQ(const vector<T> &values = )
if (!values.empty())
build(values);
static T better(T a, T b)
return max(a, b);
void build(const vector<T> &values)
n = (int)(values.size());
int levels = highest_bit(n) + 1;
range_max.resize(levels);
for (int k = 0; k < levels; k++)
range_max[k].resize(n - (1 << k) + 1);
if (n > 0)
range_max[0] = values;
for (int k = 1; k < levels; k++)
for (int i = 0; i <= n - (1 << k); i++)
range_max[k][i] = better(range_max[k - 1][i], range_max[k - 1][i + (1 << (k - 1))]);
T query_value(int a, int b) const //[a,b),idx from 1
// assert(0 <= a && a < b && b <= n);
int level = highest_bit(b - a + 1);
return better(range_max[level][a], range_max[level][b - (1 << level) + 1]);
;
template<typename T>
struct RMQ1
static int highest_bit(unsigned x)
return x == 0 ? -1 : 31 - __builtin_clz(x);
int n = 0;
vector<vector<T>> range_min;
RMQ1(const vector<T> &values = )
if (!values.empty())
build(values);
static T better(T a, T b)
return min(a, b);
void build(const vector<T> &values)
n = (int)(values.size());
int levels = highest_bit(n) + 1;
range_min.resize(levels);
for (int k = 0; k < levels; k++)
range_min[k].resize(n - (1 << k) + 1);
if (n > 0)
range_min[0] = values;
for (int k = 1; k < levels; k++)
for (int i = 0; i <= n - (1 << k); i++)
range_min[k][i] = better(range_min[k - 1][i], range_min[k - 1][i + (1 << (k - 1))]);
T query_value(int a, int b) const //[a,b),idx from 1
// assert(0 <= a && a < b && b <= n);
int level = highest_bit(b - a + 1);
return better(range_min[level][a], range_min[level][b - (1 << level) + 1]);
;
void solve()
cin >> N;
vector<int> a(N+2), sum(N+1);
for (int i = 1; i <= N; i ++) cin >> a[i];
a[0] = a[N+1] = 1e9;
RMQ<int> rmq(a);//维护区间最大
a[0] = a[N+1] = -1;
RMQ1<int> rmq1(a);//维护区间最小
int ans = 0;
for (int i = 1; i <= N; i ++) //max
//二分求右边界
int l = i + 1, r = N+1;
while (l < r)
int mid = (l + r) >> 1;
if (rmq.query_value(i + 1,mid) > a[i]) r = mid ;
else
l = mid + 1;
int right = l - i;//暂存右边界的长度
//二分左边界
l = 0, r = i - 1;
while (l < r)
int mid = (l + r + 1) >> 1;
if (rmq.query_value(mid, i - 1) < a[i]) r = mid - 1;
else
l = mid;
ans += right * (i-r) * a[i];//计算贡献
for (int i = 1; i <= N; i ++) //min同理
int l = i + 1, r = N+1;
while (l < r)
int mid = (l + r) >> 1;
if (rmq1.query_value(i + 1,mid) < a[i]) r = mid ;
else
l = mid 代码源#436子串的最大差
代码源 Div1#103子串的最大差 Codeforces - 817D,力扣2104,1900分