当前我们执行到第\(i\)位置的时候,我们以\(i\)结尾的字串,一共有\(i\)个。
分别是\([1,i],[2,i],[3,i]..[i,i]\)然后令单调栈\(mx,mn\)保存最大值和最小值。我们可以发现的是,以某一个点为终点后,每一个最大/小值是向前作用的。并且具有单调性,即,\([j,i]\)的最大值是\(k\),那么\([1,j-1]\)的最大值就不会小于\(k\)。所以利用单调栈保存最大最小值和各自的位置,利用线段树更新即可。
这道题卡longlong,交了两三发发现好像不太对,计算一下发现上限\(2e19\)。(多校能这么简单就好了qwq)
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<‘0‘ || ch>‘9‘) { if (ch == ‘-‘)f = -1; ch = getchar(); }
while (ch >= ‘0‘ && ch <= ‘9‘) { x = x * 10 + ch - ‘0‘; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 1e5 + 10;
int n;
int v[N];
stack<pir>mx, mn;
struct node {
ull lazy[2];
ull sum;
ull mxn[2];
node operator+(const node temp)const {
node res;
node now = *this;
res.lazy[0] = res.lazy[1] = -1;
res.mxn[0] = temp.mxn[0] + now.mxn[0];
res.mxn[1] = temp.mxn[1] + now.mxn[1];
res.sum = now.sum + temp.sum;
return res;
}
};
struct SEG {
node tr[N << 2];
void pushdown(int root,int l,int r)
{
up(i, 0, 2)
{
if (tr[root].lazy[i]!=-1)
{
ull len = (r - l + 1);
ull lenr = len / 2ull;
ull lenl = len - lenr;
tr[root << 1].lazy[i] = tr[root].lazy[i];
tr[root << 1 | 1].lazy[i] = tr[root].lazy[i];
tr[root << 1].mxn[i] = tr[root].lazy[i] * lenl;
tr[root << 1 | 1].mxn[i] = tr[root].lazy[i] * lenr;
tr[root << 1].sum = tr[root].lazy[i] * tr[root << 1].mxn[i ^ 1];
tr[root << 1 | 1].sum = tr[root].lazy[i] * tr[root << 1 | 1].mxn[i ^ 1];
tr[root].lazy[i] = 0;
}
}
}
void pushup(int root) {
tr[root] = tr[root << 1] + tr[root << 1 | 1];
}
void update(int l, int r, int root, int L, int R,int c, ull val)
{
if (L <= l && r <= R)
{
ull len = r - l + 1;
tr[root].mxn[c] = len * val;
tr[root].lazy[c] = val;
tr[root].sum = val * tr[root].mxn[c ^ 1];
return;
}
pushdown(root, l, r);
int mid = (l + r) >> 1;
if (L <= mid)update(lson, L, R, c, val);
if (R > mid)update(rson, L, R, c, val);
pushup(root);
}
}TR;
int main()
{
n = read();
mx.push({ 0,1e9 });
mn.push({ 0,-1 });
upd(i, 0, n * 4)TR.tr[i].lazy[1] = TR.tr[i].lazy[0] = -1;
ull ans = 0;
upd(i, 1, n)
{
v[i] = read();
while (mx.size() && mx.top().second < v[i])mx.pop();
TR.update(1, n, 1, mx.top().first + 1, i, 1, (ull)v[i]);
while (mn.size() && mn.top().second > v[i])mn.pop();
TR.update(1, n, 1, mn.top().first + 1, i, 0, (ull)v[i]);
mx.push({ i,v[i] });
mn.push({ i,v[i] });
ans += TR.tr[1].sum;
}
cout << ans << endl;
return 0;
}
原文:https://www.cnblogs.com/LORDXX/p/13394718.html