JZOJ.5231【NOIP2017模拟8.5】序列问题
Description

Input
首先输入n。
接下来输入n个数,描述序列 A。
Output
输出一行一个整数代表答案。
Sample Input
7
0 35 40 45 56 65 94
Sample Output
66636
Data Constraint
对于60%的数据,n<=50000
对于100%的数据,n<=500000,0<=A[i]<=10^9
ST加暴力枚举区间显然过不了,复杂度为O(n2)
考虑分治。对于区间[l,r],我们可以轻易求得[l,mid]和[mid+1,r]的值,关键在于求$\sum_{x\in[l,\,mid],\,y\in[mid+1,\,r]}f(x,\,y)*g(x,\,y)$
我们可以先枚举右端点r,求得每个点i到mid+1的最大值和最小值max[i]和min[i],然后从mid开始右往左枚举每一个左端点j,求得min_l,max_l,然后可以求得右端点中从左到右的第一个大于max_l,第一个小于min_l,的位置i,如图所示
(我们假设min的位置在max位置的左边)
这样右边就被分成三段。
对于第一段(mid-min之间),由于最大值和最小值都是max_l,min_l,所以右端点这一段的值就是min_l*max_l*(min-mid)
对于第二段(min-max之间),由于最大值是max_l,最小值不定,不过我们可以把max_l提取出来,剩下的就是这段区间min_l的和了,所以我们可以用前缀和维护一下。smin[max-1]-smin[min-1]
对于第三段(max-r之间),由于最大值最小值都是不定,但是它们都在mid的右边,我们在分治的时候就已经算好了,直接加上去就好了 。 spul[r]-spul[max-1]
min在max右边同理。
#include<iostream>
#include<cstdio>
#define qaq 1000000007
using namespace std;
const int N=;
long long a[N],minn[N],maxx[N],sminn[N],smaxx[N],spul[N],ans;
int n;
void fz(long long l,long long r){
if (r<l) return;
if (l==r){
ans=(ans+a[l]*a[r])%qaq;
return;
}
long long mid=(l+r)>>;
fz(l,mid);
fz(mid+,r);
minn[mid]=1e12;
maxx[mid]=;
sminn[mid]=;
smaxx[mid]=;
spul[mid]=;
for (int i=mid+;i<=r;i++){
if (a[i]>maxx[i-]) maxx[i]=a[i];
else maxx[i]=maxx[i-];
if (a[i]<minn[i-]) minn[i]=a[i];
else minn[i]=minn[i-];
sminn[i]=sminn[i-]+minn[i];
smaxx[i]=smaxx[i-]+maxx[i];
spul[i]=(spul[i-]+maxx[i]*minn[i])%qaq;
}
long long mi=mid;
long long ma=mid;
long long mis=1e12;
long long mas=;
for (int i=mid;i>=l;i--){
if (a[i]<mis) mis=a[i];
if (a[i]>mas) mas=a[i];
while ((mi<r)&&(minn[mi+]>=mis)) mi++;
while ((ma<r)&&(maxx[ma+]<=mas)) ma++;
if (mi<ma)
ans=(ans+mis%qaq*mas%qaq*(mi-mid)%qaq+(sminn[ma]-sminn[mi])%qaq*mas%qaq+(spul[r]-spul[ma])%qaq)%qaq;
else
ans=(ans+mis%qaq*mas%qaq*(ma-mid)%qaq+(smaxx[mi]-smaxx[ma])%qaq*mis%qaq+(spul[r]-spul[mi])%qaq)%qaq;
}
}
int main(){
freopen("seq.in","r",stdin);
freopen("seq.out","w",stdout);
scanf("%d",&n);
for (int i=;i<=n;i++)
scanf("%lld",&a[i]);
ans=;
fz(,n);
printf("%lld\n",ans);
return ;
}
神奇的代码
乘的顺序很重要,乘的顺序很重要,乘的顺序很重要!!!(苦悲的ans那里乘的放到后面没有事先%%%就变负数waaaaaaaa了QAQ)
JZOJ.5231【NOIP2017模拟8.5】序列问题的更多相关文章
- [jzoj 5343] [NOIP2017模拟9.3A组] 健美猫 解题报告 (差分)
题目链接: http://172.16.0.132/senior/#main/show/5343 题目: 题解: 记旋转i次之后的答案为$ans_i$,分别考虑每个元素对ans数组的贡献 若$s_i& ...
- JZOJ 5246. 【NOIP2017模拟8.8A组】Trip(trip)
5246. [NOIP2017模拟8.8A组]Trip(trip) (File IO): input:trip.in output:trip.out Time Limits: 1500 ms Memo ...
- JZOJ 5236. 【NOIP2017模拟8.7A组】利普希茨
5236. [NOIP2017模拟8.7A组]利普希茨 (File IO): input:lipschitz.in output:lipschitz.out Time Limits: 1000 ms ...
- JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠
JZOJ [NOIP2017提高A组模拟9.14]捕老鼠 题目 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕 ...
- JZOJ 5235. 【NOIP2017模拟8.7A组】好的排列
5235. [NOIP2017模拟8.7A组]好的排列 (File IO): input:permutation.in output:permutation.out Time Limits: 1000 ...
- JZOJ 5230. 【NOIP2017模拟A组模拟8.5】队伍统计
5230. [NOIP2017模拟A组模拟8.5]队伍统计 (File IO): input:count.in output:count.out Time Limits: 1500 ms Memory ...
- JZOJ【NOIP2013模拟联考14】隐藏指令
JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...
- JZOJ.5335【NOIP2017模拟8.24】早苗
Description
- JZOJ.5281【NOIP2017模拟8.15】钦点
Description
随机推荐
- Spring MVC 单元测试异常 Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file
Sping 3.2.8.RELEASE + sping mvc + JDK 1.8运行异常. java.lang.IllegalStateException: Failed to load Appli ...
- php json_decode失败,返回null
在使用json_decode之前,一定得保证字符串是utf-8编码,而执行json_decode失败的原因有很多,罗列如下: 1)编码不对: 2)字符串格式不对: 3)字符串格式对,但是有异常字符: ...
- 表格行与行的间距设置,通过margin无效,要这么设置
CSS border-collapse 属性设置表格的边框是否被合并为一个单一的边框 值 描述 separate 默认值.边框会被分开.不会忽略 border-spacing 和 empty-cell ...
- ip地址库选择
目前市面上常用的ip地址库,有以下几种 1,新浪的api接口(限制未知)http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js& ...
- 通过SectionIndexer实现微信通讯录
这里主要参考了使用SectionIndexer实现微信通讯录的效果 在这里做个记录 效果图 页面使用RelativeLayout,主要分为三个部分,match_parent的主listView,右边字 ...
- Excel 출력
NativeExcel 참조 사이트 http://www.nika-soft.com/dwnld.htm IWorkbook book = Factory.CreateWorkbook(); lWo ...
- EularProject 32: 数字1-9排列构成乘法等式
Pandigital products Problem 32 We shall say that an n-digit number is pandigital if it makes use of ...
- Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案
Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案 1. 业务场景 android+webview h5 css背景图性能提升1 2. ...
- ArcGIS教程:Geostatistical Analyst 应用演示样例
Geostatistical Analyst 满足多种不同应用的需求. 下面是 Geostatistical Analyst 的一小部分应用演示样例. 探索性空间数据分析 Geostatistical ...
- (1)Smali系列学习之Smali函数调用语句分析
一.函数调用smali中的函数和成员变量也分为两种,分别为 direct 和 virtual.两者的区别如下: 1.direct method 是指调用private方法.2.virtual meth ...