Luogu 4755 Beautiful Pair

分治 + 主席树。

设$solve(l, r)$表示当前处理到$[l, r]$区间的情况,我们可以找到$[l, r]$中最大的一个数的位置$mid$,然后扫一半区间计算一下这个区间的答案。

注意,这时候左半边是$[l, mid]$,而右区间是$[mid, r]$,我们在这个区间处理的时候要算完所有$mid$的情况,然后我们每一次分治的时候去处理$solve(l, mid - 1)$和$solve(mid + 1, r)$,要不然当$mid$是端点的时候就会无限递归下去。

问题转化快速算出一个区间内$\leq$一个数的数,只要一棵主席树就可以解决了,区间最大值可以用$ST$表维护出来。

我们每一次选取一个比较短的区间去枚举然后算另一个区间的答案,这样子每一次计算区间的长度至少减少一半,这样子可以保证时间复杂度。

时间复杂度$O(nlog^2n)$。

Code:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;const int N = 1e5 + 5;
const int Lg = 20;
const ll inf = 1LL << 60; int n, tot = 0;
ll ans = 0LL, a[N], num[N];template <typename T>
inline void read(T &X) {X = 0; char ch = 0; T op = 1;for(; ch > '9' || ch < '0'; ch = getchar())if(ch == '-') op = -1;for(; ch >= '0' && ch <= '9'; ch = getchar())X = (X << 3) + (X << 1) + ch - 48;X *= op;
}template <typename T>
inline void chkMax(T &x, T y) {if(y > x) x = y;
}namespace ST {int st[N][Lg], len[N];inline int bet(int x, int y) {return a[x] > a[y] ? x : y;}inline void prework() {for(int j = 1; j <= 18; j++)for(int i = 1; i + (1 << j) - 1 <= n; i++)st[i][j] = bet(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);}inline int qMax(int x, int y) {int k = len[y - x + 1];return bet(st[x][k], st[y - (1 << k) + 1][k]);}} using namespace ST;namespace SegT {struct Node {int lc, rc;ll sum;} s[N * 40];int root[N], nodeCnt = 0;#define lc(p) s[p].lc#define rc(p) s[p].rc#define sum(p) s[p].sum#define mid ((l + r) >> 1)void ins(int &p, int l, int r, int x, int pre) {s[p = ++nodeCnt] = s[pre];++sum(p);if(l == r) return;if(x <= mid) ins(lc(p), l, mid, x, lc(pre));else ins(rc(p), mid + 1, r, x, rc(pre));}ll query(int r1, int r2, int l, int r, int x, int y) {if(x > y) return 0LL;if(x <= l && y >= r) return sum(r2) - sum(r1);ll res = 0LL;if(x <= mid) res += query(lc(r1), lc(r2), l, mid, x, y);if(y > mid) res += query(rc(r1), rc(r2), mid + 1, r, x, y);return res;}#undef mid} using namespace SegT;void solve(int l, int r) {if(l > r) return;int mid = qMax(l, r);if(mid - l < r - mid) {for(int i = l; i <= mid; i++) {int pos = upper_bound(num + 1, num + 1 + tot, (ll) (num[a[mid]] / num[a[i]])) - num - 1;ans += query(root[mid - 1], root[r], 1, tot, 1, pos);}    } else {for(int i = mid; i <= r; i++) {int pos = upper_bound(num + 1, num + 1 + tot, (ll) (num[a[mid]] / num[a[i]])) - num - 1;ans += query(root[l - 1], root[mid], 1, tot, 1, pos);}}solve(l, mid - 1), solve(mid + 1, r);
}int main() {read(n);for(int i = 1; i <= n; i++) {read(a[i]);len[i] = log2(i), st[i][0] = i;num[++tot] = a[i];}prework();num[++tot] = inf;sort(num + 1, num + 1 + tot);tot = unique(num + 1, num + tot + 1) - num - 1;for(int i = 1; i <= n; i++) {a[i] = lower_bound(num + 1, num + 1 + tot, a[i]) - num;ins(root[i], 1, tot, a[i], root[i - 1]);}/*    for(int i = 1; i <= n; i++) printf("%lld ", a[i]);printf("\n");   */solve(1, n);printf("%lld\n", ans);return 0;
}
View Code

 

转载于:https://www.cnblogs.com/CzxingcHen/p/9905867.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388059.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网络传播动力学_通过简单的规则传播动力

网络传播动力学When a single drop of paint is dropped on a surface the amount of space that the drop will cover depends both on time and space. A short amount of time will no be enough for the drop to cover a greater area, and a small surface will bound the…

【左偏树】【P3261】 [JLOI2015]城池攻占

Description 小铭铭最近获得了一副新的桌游&#xff0c;游戏中需要用 m 个骑士攻占 n 个城池。这 n 个城池用 1 到 n 的整数表示。除 1 号城池外&#xff0c;城池 i 会受到另一座城池 fi 的管辖&#xff0c;其中 fi <i。也就是说&#xff0c;所有城池构成了一棵有根树。这 m…

【原创】数据库中为什么不推荐使用外键约束

引言 其实这个话题是老生常谈&#xff0c;很多人在工作中确实也不会使用外键。包括在阿里的JAVA规范中也有下面这一条 【强制】不得使用外键与级联&#xff0c;一切外键概念必须在应用层解决。 但是呢&#xff0c;询问他们原因&#xff0c;大多是这么回答的 每次做DELETE 或者…

初识Activiti

http://wenku.baidu.com/view/bb7364ad4693daef5ff73d32.html 1. 初识Activiti 1.1. 工作流与工作流引擎 工作流&#xff08;workflow&#xff09;就是工作流程的计算模型&#xff0c;即将工作流程中的工作如何前后组织在一起的逻辑和规则在计算机中以恰当的模型进行表示并对其…

开源软件 安全风险_3开源安全风险及其解决方法

开源软件 安全风险Open source software is very popular and makes up a significant portion of business applications. According to Synopsys, 99% of commercial databases contain at least one open source component, and nearly 75% of these codebases contain open…

React-Router 源码分析1

1、单页面应用的路由基本原理 demo1 router1.html 复制代码以 hash 形式为例。 1、init 监听浏览器 url hash 更新事件。 2、route 存储路由更新时的回调到回调数组routes中&#xff0c;回调函数将负责对页面的更新。 3、refresh 执行当前url对应的回调函数&#xff0c;更新页面…

linux安装日志切割程序

linux安装日志切割程序 安装 gcc&#xff08;1&#xff09; yum insatll gcc &#xff08;2&#xff09;# cd cronolog-1.6.2 4、运行安装 # ./configure# make# make install 5、查看cronolog安装后所在目录&#xff08;验证安装是否成功&#xff09; # which cronolog 一般情…

自助分析_为什么自助服务分析真的不是一回事

自助分析That title probably got your attention and now you think I have some explaining to do! The key word in the title is the word “A”. Self-service analytics isn’t a thing if “a thing” means a single, distinct corporate initiative or set of require…

BPMN2.0-概要

BPMN2.0-概要 作者&#xff1a;AliKevin2011&#xff0c;发布于2012-6-27 一、BPMN简介 BPMN&#xff08;Business Process Model And Notation&#xff09;- 业务流程模型和符号 是有BPMI&#xff08;Business Process Management Initiative&#xff09;开发的一套变准的业务…

如何用Phaser实现一个全家福拼图H5

一、Phaser介绍 二、整体框架搭建 三、资源加载 四、游戏逻辑五、完成六、总结参考文档 最近用Phaser做了一个全家福拼图h5的项目&#xff0c;这篇文章将会从零开始讲解如何用Phaser实现&#xff0c;最终效果如下&#xff1a; 源码&#xff1a;https://github.com/ZENGzoe/phas…

angularjs 默认跳转

angularjs 的 $state.go() 跳转页面 &#xff0c;目标页面的js函数 的执行 先于 $locationChangeStart 的监听函数。 故意 添加 timeout 可以使 controller 在locationchangestart 之后触发。转载于:https://www.cnblogs.com/RoadAspenBK/p/9923332.html

错误录入 算法_如何使用验证错误率确定算法输出之间的关系

错误录入 算法Monument (www.monument.ai) enables you to quickly apply algorithms to data in a no-code interface. But, after you drag the algorithms onto data to generate predictions, you need to decide which algorithm or combination of algorithms is most re…

Activiti 简易教程

一搭建环境 1.1 JDK 6 activiti 运行在版本 6以上的 JDK上。转到 Oracle Java SE下载页面&#xff0c;点击按钮“下载 JDK”。网页中也有安装说明。要核实安装是否成功&#xff0c;在命令行上运行 java–version。将打印出安装的 JDK的版本。 1.2 Ant 1.8.1 从 Ant[http://…

xargs命令详解,xargs与管道的区别

在工作中经常会接触到xargs命令&#xff0c;特别是在别人写的脚本里面也经常会遇到&#xff0c;但是却很容易与管道搞混淆&#xff0c;本篇会详细讲解到底什么是xargs命令&#xff0c;为什么要用xargs命令以及与管道的区别。为什么要用xargs呢&#xff0c;我们知道&#xff0c;…

pytorch回归_PyTorch:用岭回归检查泰坦尼克号下沉

pytorch回归In this notebook, we shall use this dataset containing data about passengers from the Titanic. Based on this data, we will use a Ridge Regression model which just means a Logistic Regression model that uses L2 Regularization for predicting wheth…

Java后台与VUE跨域交接

后台代码&#xff1a;package com.cn.Mr.Zhong.filter;import org.springframework.stereotype.Component;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;impor…

koa2 中使用 svg-captcha 生成验证码

1. 安装svg-captcha $ npm install --save svg-captcha 2. 使用方法 生成有4个字符的图片和字符串const svgCaptcha require(svg-captcha)const cap svgCaptcha.create({size: 4, // 验证码长度width:160,height:60,fontSize: 50,ignoreChars: 0oO1ilI, // 验证码字符中排除 …

Weblogic 节点启动

1.启动管理理节点export JAVA_OPTIONS"$JAVA_OPTIONS -Dcom.sun.xml.namespace.QName.useCompatibleSerialVersionUID1.0 -Djava.security.egdfile:/dev/./urandom"nohup ./startWebLogic.sh >admin.log &tail -f admin.log2.启动节点ssonohup ./startManaged…

[Swift]LeetCode74. 搜索二维矩阵 | Search a 2D Matrix

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号&#xff1a;山青咏芝&#xff08;shanqingyongzhi&#xff09;➤博客园地址&#xff1a;山青咏芝&#xff08;https://www.cnblogs.com/strengthen/&#xff09;➤GitHub地址&a…

iris数据集 测试集_IRIS数据集的探索性数据分析

iris数据集 测试集Let’s explore one of the simplest datasets, The IRIS Dataset which basically is a data about three species of a Flower type in form of its sepal length, sepal width, petal length, and petal width. The data set consists of 50 samples from …