分治 + 主席树。

设$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

Luogu 4755 Beautiful Pair相关推荐

  1. luoguP4755 Beautiful Pair

    https://www.luogu.org/problemnew/show/P4755 考虑分治,在 [l, r] 区间中用线段树找到最大的一个点,处理经过它的可行数对的个数,统计个数可以离线树状数组 ...

  2. Luogu4755 Beautiful Pair 最值分治、主席树

    传送门 整天做一些模板题感觉药丸 设\(val_i\)表示第\(i\)个位置的值 看到区间最大值考虑最值分治.对于当前的区间\([l,r]\),找到区间最大值\(mid\),递归\([l,mid-1] ...

  3. P4755 Beautiful Pair (数据结构+分治)

    题意: 小D有个数列a ,当一个数对(i,j)(i≤j)(i,j)(i\leq j)(i,j)(i≤j)满足 aia_iai​和 aj​a_j​aj​​ 的积不大于 ai,ai+1​,........ ...

  4. 洛谷 - P4755 Beautiful Pair(笛卡尔树+主席树)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a,现在一个数对 ( i , j ) 如果满足 a[ i ] * a[ j ] <=max( a[ i ] ~ a[ j ] ),则称其 ...

  5. 【笛卡尔树】【树状数组】Beautiful Pair(P4755)

    正题 P4755 题目大意 给你n个数,问你有多少对二元组 (i,j)(i,j)(i,j) 满足 i≤ji\leq ji≤j 且 ai×aj≤maxi=ijaia_i\times a_j\leq ma ...

  6. luogu P3306 [SDOI2013] 随机数生成器(BSGS,数列求通项,毒瘤特判)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 发个水题的 题解证明我还在() luogu P3306 [SDOI2013] 随机数生成器 Webli ...

  7. 【欧拉回路】解题报告:luogu P6066 [USACO]Watchcow (欧拉回路详解)【模板】

    欧拉回路模板 题目链接:https://www.luogu.com.cn/problem/P6066 O(N+M)O(N+M)O(N+M)非递归版代码 防止栈溢出 欧拉回路就是给一个图,存在一条回路把 ...

  8. Luogu P5244 [USACO2019Feb Platinum] Mowing Mischief (动态规划、决策单调性)

    题目链接 https://www.luogu.com.cn/problem/P5244 题解 首先求出 LIS. 根据 LIS 的值我们可以对整个点集分层,每一层内进行 DP. 将每层的点按 \(x_ ...

  9. BZOJ 5330 Luogu P4607 [SDOI2018]反回文串 (莫比乌斯反演、Pollard Rho算法)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=5330 (Luogu) https://www.luogu.org/prob ...

最新文章

  1. 【经验】【ORACLE】从字符串中截取其中的数字
  2. 免费机器学习课程爆红:从概率与统计到全栈深度学习,英伟达工程师小姐姐整理...
  3. 04 javascirpt基础知识---听课笔记
  4. git用.gitignore忽略指定文件
  5. java的优先队列注意事项
  6. H3C认证云计算高级工程师
  7. android中常见的错误及解决办法
  8. Windows7下安装配置PHP开发环境
  9. 【逆向工具】使用x64dbg+spy去除WinRAR5.40(64位)广告弹框
  10. System.Management.ManagementException: 访问遭到拒绝的解决方案
  11. 暗黑系?No...,打造一款 IDEA 护眼主题方案!
  12. Abaqus怎么切换中英文界面
  13. linux fcntl函数,fcntl函数的使用详解
  14. Redfish接口测试
  15. 用TEXT函数解决日期用连接字符日期变成数字格式的问题
  16. c++ 去除字符串首尾的空白字符
  17. Tensorflow保存模型和加载预训练模型
  18. 阿里高级外包你待得住吗?
  19. 创业公司的产品经理应该怎么去做?
  20. c语言---15 循环语句do while()

热门文章

  1. 周华健,歌声伴我成长(四)
  2. 萧山职称计算机考试培训,浙江萧山2017年职称计算机考试时间安排
  3. 要配置php环境_只需修改,要配置Apache的PHP环境,只需修改()。
  4. object picker 微信小程序_微信小程序 demo分享
  5. 可以使用中文作为变量名_次氯酸可以作为伤口消毒使用吗?
  6. 计算机社团活动丰富多彩,描写社团丰富多彩的句子
  7. 求数的绝对值一定是正数_人教版初中数学七年级上册绝对值公开课优质课课件教案视频...
  8. mysql 设置client char_mysql编码问题:show variables like “%char%”
  9. php接收get参数false是字符串,php解析url (parse_url) 参数成数组 (parse_str)
  10. 测试环境搭建流程_前端构建 DevOps 搭建 DevOps 基础平台(中)