计蒜客-数组 Posted on 2019-06-12 | Edited on 2019-08-08 | In 计蒜客 | Views: 题意给出长度为 $N$ 的数组 $A$, 定义 求 题解从后往前枚举 $i$ , 用一个单调栈来存在固定 $i$ 的时候, $j\in[i,n]$ , 不同 $j$ 的 $x$ 的取值 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107#include <bits/stdc++.h>using namespace std;#define forl(i, l, r) for (int i = l; i <= r; i++)#define forr(i, r, l) for (int i = r; i >= l; i--)#define for1(i, n) for (int i = 1; i <= n; i++)#define fro1(i, n) for (int i = 1; i <= n; i++)#define for0(i, n) for (int i = 0; i < n; i++)#define fro0(i, n) for (int i = 0; i < n; i++)#define meminf(a) memset(a, inf, sizeof(a))#define mem_1(a) memset(a, -1, sizeof(a))#define mem0(a) memset(a, 0, sizeof(a))#define memcp(a,b) memcpy(a,b,sizeof(b))#define oper(type) bool operator <(const type y)const#define mp make_pair#define pu_b push_back#define pu_f push_front#define po_b pop_back#define po_f pop_front#define fi first#define se second#define whiel while#define retrun returntypedef pair<long long, long long> pll;typedef vector<long long> vll;typedef pair<int, int> pii;typedef unsigned long long ull;typedef vector<int> vii;typedef long double db;typedef long long ll;typedef int itn;int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}int in(int &a,int &b){return scanf("%d%d",&a,&b);}int in(ll &a){return scanf("%lld",&a);}int in(int &a){return scanf("%d",&a);}int in(char *s){return scanf("%s",s);}int in(char &s){return scanf("%c",&s);}int in(db &a){return scanf("%Lf",&a);}void out(int a){printf("%d",a);}void outln(int a){printf("%d\n",a);}void out(ll a){printf("%lld",a);}void outln(ll a){printf("%lld\n",a);}const db pi = acos((db)-1);const ll inf =0x3f3f3f3f;const db eps = 1e-8;const int N = 2.1e5;const ll mod = 1e9+7;int sign(db a) { return a < -eps ? -1 : a > eps;}int db_cmp(db a, db b){ return sign(a-b); }struct da{ int a,r,pre,i; //r 表示取 a 的 j 的区间的右端点 da(int _a,int _r,int _p,ll _i){ a=_a; r=_r; pre=_p; i=_i; } da(){}};stack<da>sta;ll fun(itn a,int l,int r){ return (r-l+1ll)*(l+r)/2%mod*a%mod;}int a[N];int main() { itn n; ll ans=0; in(n); for1(i,n){ in(a[i]); } for(int i=n;i>0;i--){ ll r=n; whiel(sta.size()){ da tmp=sta.top(); if(tmp.a<=a[i]){ sta.pop(); }else{ ll p=(tmp.a*1ll*tmp.i-tmp.a+a[i]-a[i]*1ll*i)/(tmp.a-a[i]); if(p>=tmp.r){ sta.pop(); }else{ r=p; break; } } } // cout<<i<<' '<<r<<"\n"; assert(r<=n); // assert(r>=) int pre=0; if(sta.size()){ da tmp=sta.top(); // cout<<tmp.a<<' '<<tmp.i<<endl; pre=tmp.pre; // assert(r>=tmp.i); pre=(pre+fun(tmp.a,r-tmp.i+2,tmp.r-tmp.i+1))%mod; } // cout<<endl; sta.push(da(a[i],r,pre,i)); ans=(ans+pre+fun(a[i],1,r-i+1))%mod; } outln(ans); return 0;}