题意
给定一个数列,求(sum^n_{i=1}sum^n_{j=i}(j-i+1)min(a_i,...,a_j)max(a_i,...,a_j))。
思路
采用CDQ分治解决问题。
对于当前的区间([l,r]),如果为点直接更新答案。
否则可以分成左右区间([l,mid],[mid+1,r])
枚举左侧区间中的每一个点作为左端点(ptr_l),计算右端点在右侧区间的贡献。
设([ptr_l,mid])中最小值为(mn),最大值为(mx)。
我们枚举出对于([mid+1,r]),(mn,mx)从(mid+1)开始的最大有效范围,不妨设为(x,y)。
假设(xleq y),
那么([mid+1,r])被划分为三个区间([mid+1,x],[x+1,y],[y+1,r])
对于([mid+1,x]),答案应该更新(mn*mx*sum^x_{i=mid+1}(i-ptr_l+1))。这显然可以通过等差数列求和公式(O(1))求解。
对于([x+1,y]),答案应该更新(mx*sum^y_{i=x+1}min(x+1,i)(i-ptr_l+1)),即(mx*sum^y_{i=x+1}min(x+1,i)*i-min(x+1,i)*(ptr_l-1)),我们可以通过维护前缀和来解决。
对于([y+1,r]),答案应该更新(sum^r_{i=y+1}min(y+1,r)max(y+1,r)(i-ptr_l+1)),同理可化为类似上面的结构。
对于(y<x)的情况,也可分为如上三类。
总共我们需要维护六个前缀和,最后注意取模问题即可。
代码
等会再贴上来。