题解:

分治好题

首先暴力显然rmq可以做到n^2

比较容易想到是以最值分治,这样在数据随机复杂度是nlogn,不随机还是n^2的

以最值分治只有做多与较小区间复杂度相同才是nlogn的

而这题里我们直接分治

想清楚再搞个暴力对拍还是比较好写的

代码:

#include <bits/stdc++.h>
using namespace std;
#define rint register int
#define IL inline
#define rep(i,h,t) for (rint i=h;i<=t;i++)
#define dep(i,t,h) for (rint i=t;i>=h;i--)
#define ll long long
#define mid ((h+t)>>1)
const int N=6e5;
const int mo=1e9;
int a[N],sum[N],sum2[N],sum3[N],sum4[N],sum5[N],sum6[N];
ll ans=;
void fz(int h,int t)
{
if (h==t)
{
ans=(ans+1ll*a[h]*a[h])%mo;
return;
}
int mina=a[mid],maxa=a[mid];
sum[mid+]=sum2[mid+]=sum3[mid+]=sum4[mid+]=;
dep(i,mid,h)
{
mina=min(a[i],mina);
maxa=max(a[i],maxa);
sum[i]=(sum[i+]+(1ll*mina*maxa)%mo*(mid-i+))%mo;
sum2[i]=(sum2[i+]+1ll*mina*maxa)%mo;
sum3[i]=(sum3[i+]+1ll*(mid-i+)*mina)%mo;
sum4[i]=(sum4[i+]+mina)%mo;
sum5[i]=(sum5[i+]+1ll*(mid-i+)*maxa)%mo;
sum6[i]=(sum6[i+]+maxa)%mo;
}
mina=a[mid],maxa=a[mid];
int pos1=mid,pos2=mid;
rep(i,mid,t)
{
mina=min(a[i],mina);
maxa=max(a[i],maxa);
while (a[pos1]>=mina&&pos1>=h) pos1--; pos1++;
while (a[pos2]<=maxa&&pos2>=h) pos2--; pos2++;
if (pos2<pos1)
{
ans+=(1ll*(*i-mid-pos1+)*(mid-pos1+)/)%mo*mina%mo*maxa%mo;
ans+=(sum3[pos2]-sum3[pos1]+1ll*(i-mid)*(sum4[pos2]-sum4[pos1]))%mo*maxa%mo;
ans+=(sum[h]-sum[pos2]+1ll*(i-mid)*(sum2[h]-sum2[pos2]))%mo;
ans%=mo;
} else
{
ans+=(1ll*(*i-mid-pos2+)*(mid-pos2+)/)%mo*mina%mo*maxa%mo;
ans+=(sum5[pos1]-sum5[pos2]+1ll*(i-mid)*(sum6[pos1]-sum6[pos2]))%mo*mina%mo;
ans+=(sum[h]-sum[pos1]+1ll*(i-mid)*(sum2[h]-sum2[pos1]))%mo;
ans%=mo;
}
}
if (h<=mid-) fz(h,mid-);
if (mid+<=t) fz(mid+,t);
}
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
ios::sync_with_stdio(false);
int n;
cin>>n;
rep(i,,n) cin>>a[i];
fz(,n);
cout<<ans<<endl;
return ;
}

BZOJ 3745的更多相关文章

  1. 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 ...

  2. bzoj 3745 [Coci2015]Norma——序列分治

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3745 如果分治,就能在本层仅算过 mid 的区间了. 可以从中间到左边地遍历左边,给右边两个 ...

  3. bzoj 3745: [Coci2015]Norma

    Description Solution 考虑分治: 我们要统计跨越 \(mid\) 的区间的贡献 分最大值和最小值所在位置进行讨论: 设左边枚举到了 \(i\),左边 \([i,mid]\) 的最大 ...

  4. [BZOJ 3745] [COCI 2015] Norma

    Description 给定一个正整数序列 \(a_1,a_2,\cdots,a_n\),求 \[ \sum_{i=1}^n\sum_{j=i}^n(j-i+1)\min(a_i,a_{i+1},\c ...

  5. 【刷题】BZOJ 3745 [Coci2015]Norma

    Description Input 第1行,一个整数N: 第2~n+1行,每行一个整数表示序列a. Output 输出答案对10^9取模后的结果. Sample Input 4 2 4 1 4 Sam ...

  6. bzoj 3745: [Coci2015]Norma【分治】

    参考:https://blog.csdn.net/lych_cys/article/details/51203960 真的不擅长这种-- 分治,对于一个(l,r),先递归求出(l,mid),(mid+ ...

  7. BZOJ 2127: happiness [最小割]

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1815  Solved: 878[Submit][Status][Di ...

  8. BZOJ 3275: Number

    3275: Number Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 874  Solved: 371[Submit][Status][Discus ...

  9. BZOJ 2879: [Noi2012]美食节

    2879: [Noi2012]美食节 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1834  Solved: 969[Submit][Status] ...

随机推荐

  1. Java链接DB2的4种基本类型【转】

    原文链接:http://doc.chinaunix.net/java/201002/776480.shtml 第一种:目前IBM一直都没有提供 TYPE 1的JDBC驱动程序. 第二种:类型2驱动:C ...

  2. Cassandra索引详解

    转自: https://www.cnblogs.com/bonelee/p/6278943.html 1.什么是二级索引? 我们前面已经介绍过Cassandra之中有各种Key,比如Primary K ...

  3. 【算法】二分查找法&大O表示法

    二分查找 基本概念 二分查找是一种算法,其输入是一个有序的元素列表.如果要查找的元素包含在列表中,二分查找返回其位置:否则返回null. 使用二分查找时,每次都排除一半的数字 对于包含n个元素的列表, ...

  4. iptables防护CC和DDos和PPTP穿透脚本

    一.iptables优化脚本案例 #!/bin/bash #脚本下载地址:#wget www.mrliangqi.com/pack/shell/iptables.sh #脚本使用:#bash ipta ...

  5. Go语言环境安装&搭建(Linux)

    Linux的东西果然不记不行啊~ 下载&安装 下载 我们先找到linux版的下载链接 https://golang.org/dl/ 打开网址找到Linux对应的链接右键复制下载地址 然后连接服 ...

  6. GDOI2018 涛涛摘苹果 [CDQ分治]

    传送门我会让你知道哪里有题面吗(逃 思路 显然不能模拟苹果下掉的过程,考虑计算每个苹果对询问的贡献. 显然一开始就有的苹果可以看做第0天变出来的,于是只需要考虑变出来的苹果了. 设当前询问节点\(x\ ...

  7. js跳转页面(转)

    <span id="tiao">3</span><a href="javascript:countDown"></a& ...

  8. 对象的宽度、top位置,x坐标属性

    DOM对象   DOM对象属性 对应css 说明 读/写 width   obj.clientWidth=20 1. 内联样式 <p style="width:20px"&g ...

  9. WebSocket服务端和客户端使用

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...

  10. U盘权限不足,只读文件系统

    https://blog.csdn.net/baocheng_521/article/details/77161791 用第一种方式成功