bzoj3745: [Coci2015]Norma
Description

Input
Output
预处理每个位置的数作为最小/大值向左延伸的最大距离,线段树维护序列的前缀的后缀min和后缀max以及这个前缀的后缀对答案的贡献,在前缀末尾加入一个数可以快速维护。
#include<cstdio>
typedef long long i64;
const int N=,P=;
char buf[N*],*ptr=buf-;
int _(){
int x=,c=*++ptr;
while(c<)c=*++ptr;
while(c>)x=x*+c-,c=*++ptr;
return x;
}
int _l,_r,_a,v1,v2,ans=;
inline int mod(int x){return x<P?x:x-P;}
struct node{
node*l,*r;
int f1,f2,sz,L,R;
int s1,s2,s3,ss1,ss2,ss3;
void fil1(int x){
f1=x;
s1=i64(x)*sz%P;
ss1=(i64(sz)*(sz+)>>)%P*x%P;
s3=i64(x)*s2%P;
ss3=i64(x)*ss2%P;
}
void fil2(int x){
f2=x;
s2=i64(x)*sz%P;
ss2=(i64(sz)*(sz+)>>)%P*x%P;
s3=i64(x)*s1%P;
ss3=i64(x)*ss1%P;
}
void dn(){
if(f1)l->fil1(f1),r->fil1(f1),f1=;
if(f2)l->fil2(f2),r->fil2(f2),f2=;
}
void up(){
s1=mod(l->s1+r->s1);
s2=mod(l->s2+r->s2);
s3=mod(l->s3+r->s3);
ss1=(l->ss1+r->ss1+i64(l->s1)*r->sz)%P;
ss2=(l->ss2+r->ss2+i64(l->s2)*r->sz)%P;
ss3=(l->ss3+r->ss3+i64(l->s3)*r->sz)%P;
}
void set1(){
if(_l<=L&&R<=_r){
fil1(_a);
return;
}
dn();
int M=L+R>>;
if(_l<=M)l->set1();
if(_r>M)r->set1();
up();
}
void set2(){
if(_l<=L&&R<=_r){
fil2(_a);
return;
}
dn();
int M=L+R>>;
if(_l<=M)l->set2();
if(_r>M)r->set2();
up();
}
void get(){
if(R<=_r){
v2=(v2+ss3+i64(v1)*sz)%P;
v1=mod(v1+s3);
return;
}
dn();
int M=L+R>>;
l->get();
if(_r>M)r->get();
}
}ns[N*],*np=ns,*rt;
node*build(int L,int R){
node*w=np++;
w->L=L;w->R=R;
w->sz=R-L+;
if(L!=R){
int M=L+R>>;
w->l=build(L,M);
w->r=build(M+,R);
}
return w;
}
int n,a[N],p1[N],p2[N],ss[N],sp=;
int main(){
fread(buf,,sizeof(buf),stdin);
n=_();
for(int i=;i<=n;++i)a[i]=_();
for(int i=n;i;--i){
while(sp&&a[ss[sp]]>a[i])p1[ss[sp--]]=i+;
ss[++sp]=i;
}
while(sp)p1[ss[sp--]]=;
for(int i=n;i;--i){
while(sp&&a[ss[sp]]<a[i])p2[ss[sp--]]=i+;
ss[++sp]=i;
}
while(sp)p2[ss[sp--]]=;
rt=build(,n);
for(int i=;i<=n;++i){
_l=p1[i];_r=i;_a=a[i];
rt->set1();
_l=p2[i];
rt->set2();
v1=v2=;
rt->get();
ans=mod(ans+v2);
}
printf("%d",ans);
return ;
}
bzoj3745: [Coci2015]Norma的更多相关文章
- [BZOJ3745][COCI2015]Norma[分治]
题意 题目链接 分析 考虑分治,记当前分治区间为 \(l,r\) . 枚举左端点,然后发现右端点无非三种情况: 极大极小值都在左边; 有一个在左边; 极大极小值都在右边; 考虑递推 \(l\) 的同时 ...
- bzoj3745: [Coci2015]Norma 分治,单调队列
链接 bzoj 思路 首先\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\sum\limits_{k=i}^{j}max(a_k)\)可以用单调队列求解.参见 ...
- 【BZOJ3745】[Coci2015]Norma cdq分治
[BZOJ3745][Coci2015]Norma Description Input 第1行,一个整数N: 第2~n+1行,每行一个整数表示序列a. Output 输出答案对10^9取模后的结果. ...
- 【BZOJ3745】Norma(CDQ分治)
[BZOJ3745]Norma(CDQ分治) 题面 BZOJ 洛谷 题解 这种问题直接做不好做,显然需要一定的优化.考虑\(CDQ\)分治. 现在唯一需要考虑的就是跨越当前中间节点的所有区间如何计算答 ...
- BZOJ3745:[COCI2015]Norma
浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...
- 【BZOJ3745】Norma [分治]
Norma Time Limit: 20 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input 第1行,一个整数N: ...
- BZOJ3745 : [Coci2014]Norma
考虑枚举右端点,用线段树维护[i,nowr]的答案. 当右端点向右延伸时,需要知道它前面第一个比它大/小的数的位置,这里面的最值将发生改变,这个使用单调队列求出,然后将所有的l都加1. 注意常数优化. ...
- bzoj 3745: [Coci2015]Norma
Description Solution 考虑分治: 我们要统计跨越 \(mid\) 的区间的贡献 分最大值和最小值所在位置进行讨论: 设左边枚举到了 \(i\),左边 \([i,mid]\) 的最大 ...
- BZOJ 3745: [Coci2015]Norma(分治)
题意 给定一个正整数序列 \(a_1, a_2, \cdots, a_n\) ,求 \[ \sum_{i=1}^{n} \sum_{j=i}^{n} (j - i + 1) \min(a_i,a_{i ...
随机推荐
- Ubuntu 安装桌面且远程连接
前言: 一般服务器是不装桌面的,因为会牺牲很多性能,这里就选择很小桌面,牺牲一定性能 这里就采用x2go, 这里是在ubuntu server 14.04 安装成功,其实它是可用于linux所有 ...
- 个人对beta发布的观点
内容:五个小组的beta发布 时间:2016年11月10日 13:40-15:15 地点:传媒西楼202 发布顺序: 1.飞天小女警(选礼物) 2.金州勇士(在线考试) 3.新蜂(俄罗斯方块) 4.天 ...
- point\polyline\polygon的转化(转)
首先你要明白Polyline是由path对象构成,Polygon是由ring对象构成,因此实现polyline向polygon的转换,思路如下:1.提取polyline中的所有path对象2.将pat ...
- angularjs数据异步加载时的绑定事件
// 顶级菜单项的鼠标移入和移出操作 $(document).on({ mouseover: function () { ; ; if (top + $(this).children('ul').ou ...
- Maven引入本地jar包
<dependency> <groupId>${gorup}</groupId> <artifactId>${artifact}</artifac ...
- [ie兼容]ie7浮动左在前,浮动右在后导致右边浮动的元素掉下来
解决办法:左浮动和右浮动元素在结构上互换位置 http://blog.sina.com.cn/s/blog_818a1e5b0100wp5b.html
- LeetCode189——Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- mysql触发器使用
触发器 简要 1.触发器基本概念 2.触发器语法及实战例子 3.before和after区别 一.触发器基本概念 1.一触即发 2.作用: 监视某种情况并触发某种操作 3.观察场景 一个电子商城: 商 ...
- 【Selenium2+Python】定位
定位Frame driver.switch_to_frame("frameID") 多窗口切换 #获得当前窗口 nowhandle = driver.current_window_ ...
- ACE - Reactor模式源码剖析及具体实现(大量源码慎入)
原文出自http://www.cnblogs.com/binchen-china,禁止转载. 在之前的文章中提到过Reactor模式和Preactor模式,现在利用ACE的Reactor来实现一个基于 ...