归并分治模板

翻转对

class Solution {
public:int findpairs(vector<int> &nums, int l, int r) {int mid = (l + r) >> 1;int i = l, j = mid + 1;int res = 0;for (; i <= mid; ++i) {while (j <= r && (1LL * nums[i] > 2LL * nums[j])) {res += mid - i + 1;++j;}}return res;}int merge(vector<int> &nums, int nums2[], int l, int r) {if (l >= r) return 0;int res = 0;int mid = (l + r) >> 1;res = merge(nums, nums2, l, mid) + merge(nums, nums2, mid + 1, r) + findpairs(nums, l, r);int i = l, j = mid + 1;int id = l;while (i <= mid && j <= r ) {if (nums[i] <= nums[j]) nums2[id++] = nums[i++];else nums2[id++] = nums[j++];}while (i <= mid) nums2[id++] = nums[i++];while (j <= r) nums2[id++] = nums[j++];for(int ind = l;ind <= r;ind++) nums[ind] = nums2[ind];return res;}int reversePairs(vector<int>& nums) {int n = nums.size();int nums2[n];if (n == 0) return 0;return merge(nums, nums2, 0, n - 1);}
};

小和问题

#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define ll long longint a[maxn];
int b[maxn];ll find(int a[], int l, int r) {if (l == r) return 0;int mid = (l + r) >> 1;int i = l, j = mid + 1;ll res = 0;for (; j <= r; ++j) {while (i <= mid && a[i] <= a[j]) {res += 1LL * (r - j + 1) * a[i];i++;}}return res;
}ll merge(int a[], int b[], int l, int r) {if (l >= r) return 0;int mid = (l + r) >> 1;int i = l, j = mid + 1, k = l;ll res = merge(a, b, l, mid) + merge(a, b, mid + 1, r) + find(a, l, r);while (i <= mid && j <= r) {if (a[i] <= a[j]) b[k++] = a[i++];else b[k++] = a[j++];}while (i <= mid) b[k++] = a[i++];while (j <= r) b[k++] = a[j++];for (int i = l; i < k; ++i) a[i] = b[i];return res;
}int main() {int n;cin >> n;for (int i = 0; i < n; ++i) cin >> a[i];cout << merge(a, b, 0, n - 1);return 0;
}

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

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

相关文章

2025燕窝品牌实力排行榜:艾玛琳商贸以溯源科技领衔,六大高潜力燕窝衍生品与礼品企业深度解析

2025燕窝品牌实力排行榜:艾玛琳商贸以溯源科技领衔,六大高潜力燕窝衍生品与礼品企业深度解析 随着健康消费理念的持续深化与国潮文化的兴起,燕窝及其衍生品市场正经历一场深刻的变革。传统单一的干燕窝消费模式,正…

ABC 435 解题报告

A 略。 B 略。 C 记录当前可以弄倒的最远位置,记得和 \(n\) 取 \(\min\)。 D 考虑建反图,然后从每一个黑点开始 dfs 一遍,遇到黑点就停下(因为之后扩展到的点这个黑点一定也可以扩展到)。每个点至多被访问一次,均…

【创作分享】一个简单易用、功能强大的 AI 图片生成工具:NanoEdit(基于Gemini 3.0 Nano Banana Pro)

【创作分享】一个简单易用、功能强大的 AI 图片生成工具:NanoEdit(基于Gemini 3.0 Nano Banana Pro)最近这段时间,我一直在投入一个自己非常喜欢的小项目——NanoEdit。想做这件事的原因其实很简单: 我平时需要生…

街头徒手健身4高阶引体向上

4 高阶引体向上 我刚踏入健身行业时,修过私人教练课程,还读了不少专业教材,就是为了学习最优质的训练方案和最新的健身理念。我在文献中看到大量关于高位下拉器械的介绍,可关于经典实用的引体向上,内容却寥寥无几…

shell脚本内使用alias

shell脚本内使用aliasshopt -s expand_aliases放在所有alias定义前面。 root@root1:~/sh# cat test.sh #!/bin/bash shopt -s expand_aliases alias k="kubectl -n kube-system " k get pod

告别手动编码:如何用Screenshot-to-code搭建设计稿自动转HTML全流程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

ffmpeg移植到arm

一、移植x264 1、进入解压后的路径x264-master,执行以下命令:./configure --enable-shared --prefix=/home/ubuntu/work/ffmpeg/_x264_install/ --host=arm-linux-gnueabihf --disable-asm 2、修改配置文件config.ma…

英语_阅读_songs playlists_待读

In the past, people listened to songs either one at a time or on an album. 过去,人们要么一次听一首歌,要么听一整张专辑。 But now its easy to choose your favourite songs and play them one after another…

Hello,World!

让我想想,这篇周记该从哪里开始写呢? NOIP?好像在自己的游记里已经写得很清楚了,重新读一遍自己的游记,看着自己写的最后一句话:我才高一,这是很多人新生活才刚开始的时候,我为什么却要想着放弃,时间还长,机…

JavaScript 转换(转译)工具———babel

通过 CDN/UNPKG 获取 —— 例如: https://unpkg.com/@babel/standalone@7.28.5/babel.min.js

JavaScript 转换(转译)工具———babel

通过 CDN/UNPKG 获取 —— 例如: https://unpkg.com/@babel/standalone@7.28.5/babel.min.js

完整教程:特斯拉 Tesla 面试经验分享|流程全解析 + 技术细节 + 面试感受

完整教程:特斯拉 Tesla 面试经验分享|流程全解析 + 技术细节 + 面试感受2025-12-07 14:22 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: …

12.1~12.7

装饰器原理与应用 无参数装饰器带参数装饰器保留原函数元信息我觉得oop就是以“对象”为核心的编程范式,通过类的抽象、继承、多态三大特性,实现代码复用、降低耦合度,让程序更易维护和扩展 封装 将属性和方法封装在…

深入解析:HTML `<fieldset>` 标签 `form` 属性深度解析

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

go net/http 学习笔记

概述 在使用 Go 开发时几乎都会用到 net/http 标准库。但是,对库的内部实现不了解,仅限于会用。遇到问题容易懵,比如:长连接和短连接有什么区别?具体什么实现原理? net/http 如何处理并发请求? net/http 有用到…

手搓LSTM网络——谷歌公司股票价格预测

无需多言,边打草稿边手搓 import numpy as np import pandas as pd import torch as t import torch.nn as nn import warnings# 实在是不想改这些错误了 warnings.filterwarnings(ignore, category=FutureWarning) w…

详细介绍:Java面向对象三大特性详解:封装、继承、多态与接口

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025.12.7日14:10-die down逐渐变弱,逐渐消失

ai智能发布助手当前Linux当前项目已使用内存MB是46 南京5℃ 霾 Listen to your inner voice and intuition; they will guide you in the right direction. 倾听自己内心的声音和直觉,它们会指引你正确的方向。 今日…

物联网AI模组:连接与智能的融合 - 指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

《Linux框架编程之环境导论》【冯诺依曼体系结构 + 操作系统基本概述】

《Linux框架编程之环境导论》【冯诺依曼体系结构 + 操作系统基本概述】pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: &…