4540: [Hnoi2016] Sequence
https://www.lydsy.com/JudgeOnline/problem.php?id=4540
Analysis:
MO team +rmq+ monotonous stack.
Consider adding a point after the interval has changed. [L,r]->[l,r+1], increased the r-l+1 segment. Set [l,r+1] the minimum value in P, then the left end of the l~p-1 interval, the answer is a[p], p right Also
Code:
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <cmath>5#include <iostream>6#include <cctype>7#include <Set>8#include <vector>9#include <queue>Ten#include <map> One #defineFi (s) freopen (S, "R", stdin); A #defineFo (s) freopen (S, "w", stdout); - using namespacestd; -typedefLong LongLL; the -InlineintRead () { - intx=0, f=1;CharCh=getchar (); for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') f=-1; - for(; isdigit (ch); Ch=getchar ()) x=x*Ten+ch-'0';returnx*F; + } - + Const intN =100005; A at intA[n], Sk[n], bel[n], l[n], r[n], f[n][ -], log[n]; - LL Ans[n], sl[n], sr[n], Answer; - structque{ - intL, R, id; - BOOL operator< (ConstQue &a)Const { - returnBel[l] = = Bel[a.l]? R < A.r:bel[l] <BEL[A.L]; in } - }q[n]; to +InlineintMin (intIintj) { - returnA[i] < A[j]?i:j; the } *InlineintCalc (intLintr) { $ if(R <l) Swap (L, R);Panax Notoginseng intt = log[r-l +1]; - returnMin (F[l][t], F[r-(1<< T) +1][t]); the } +InlinevoidUPDL (intLintRinto) { A intp =Calc (L, R); theLL t = 1ll * a[p] * (R-p +1) + Sr[l]-Sr[p]; +Answer + = O *T; - } $InlinevoidUPDR (intLintRinto) { $ intp =Calc (L, R); -LL t = 1ll * a[p] * (P-l +1) + Sl[r]-Sl[p]; -Answer + = O *T; the } - intMain () {fi ("1.txt");Wuyi intn = Read (), Q = Read (), B = sqrt (n); log[0] = -1; the for(intI=1; i<=n; ++i) { -A[i] =read (); WuBel[i] = (I-1)/B +1; -Log[i] = Log[i >>1] +1; Aboutf[i][0] =i; $ } - for(intI=1; i<=q; ++i) -Q[I].L = Read (), Q[I].R = Read (), q[i].id =i; - A for(intj=1; j<=log[n]; ++j) + for(intI=1; i+ (1<<J)-1<=n; ++i) theF[I][J] = Min (F[i][j-1], F[i + (1<< (J-1))][j-1]); - $ inttop =0; the for(intI=1; i<=n; ++i) { the while(Top && A[sk[top]] >= a[i]) R[sk[top]] = i, top--; theSk[++top] =i; the } - while(top) R[sk[top]] = n +1, Top--; in for(intI=n; i>=1; --i) { the while(Top && a[sk[top]] > A[i]) L[sk[top]] = i, top--; theSk[++top] =i; About } the while(top) L[sk[top]] =0, Top--; the the for(intI=1; i<=n; ++i) Sl[i] = Sl[l[i]] + 1ll * (I-l[i]) *A[i]; + for(intI=n; i>=1; -i) sr[i] = Sr[r[i]] + 1ll * (R[i]-i) *A[i]; - theSort (q +1, q + q +1);Bayi intL =1, r =0; the for(intI=1; i<=q; ++i) { the while(L > Q[i].l) L--, updl (L, R,1);//first add in minus!!! - while(R < Q[I].R) R + +, UPDR (L, R,1); - while(L < Q[I].L) UPDL (L, R,-1), L + +; the while(R > Q[i].r) updr (L, R,-1), R--; theAns[q[i].id] =Answer; the } the for(intI=1; i<=q; ++i) -printf"%lld\n", Ans[i]); the return 0; the}
4540: [Hnoi2016] Sequence