逆序对__归并排序__树状数组 Inversions SGU - 180
Input
Output
Sample test(s)
Input
2 3 1 5 4
Output
#include <cstdio>
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm> #define sf scanf
#define pf printf
#define fp(x) freopen((x), "r", stdin) typedef long long ll; using namespace std; const int maxn = 1e5; int qarr[maxn];
ll qans; //long long 不然会over void merge(int arr[], int left, int right, int tarr[])
{
if (left>=right) return ;
int m = (left + right) >> ;
merge(arr, left, m, tarr);
merge(arr, m+, right, tarr);
int i, j, cnt;
i = left;
j = m + ;
cnt = ;
while (i<=m && j<=right) {
if (arr[i] <= arr[j])
tarr[cnt++] = arr[i++];
else {
tarr[cnt++] = arr[j++];
qans += (m - i + );
} }
while (i<=m) tarr[cnt++] = arr[i++];
while (j<=right) tarr[cnt++] = arr[j++];
for (i=; i<cnt; ++i) arr[left++] = tarr[i];
} void mergesort(int arr[], int left, int right)
{
int *p = new int[right-left+];
merge(arr, left, right, p);
} int main()
{
int n;
sf("%d", &n); for (int i=; i<=n; ++i) sf("%d", &qarr[i]); mergesort(qarr, , n);
cout << qans << endl; return ;
}
#include <cstdio>
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm> #define sf scanf
#define pf printf
#define fp(x) freopen((x), "r", stdin) typedef long long ll; using namespace std; const int maxn = 1e5; struct nobe{
int id;
int val;
int ls;
bool operator < (const nobe &a) const {
if (val != a.val) return val < a.val;
return id < a.id;
}
}te[maxn]; int qsum[maxn];
ll qans; bool cmp(const nobe &a, const nobe &b)
{
return a.id < b.id;
} inline int lowbit(int id)
{
return id&-id;
} int update(int id, int _maxn)
{
while (id <= _maxn) {
qsum[id] += ;
id+=lowbit(id);
}
} int getsum(int id)
{
int res = ;
while (id) {
res += qsum[id];
id -= lowbit(id);
}
return res;
} int main()
{
int n;
sf("%d", &n); for (int i=; i<=n; ++i) {
sf("%d", &te[i].val);
te[i].id = i;
}
sort(te+, te++n);
int lst = te[].val;
int cnt = ;
for (int i=; i<=n; ++i) te[i].val = i;
sort(te+, te++n, cmp);
for (int i=; i<=n; ++i) {
update(te[i].val, n);
qans += i - - getsum(te[i].val-);
}
cout << qans << endl; return ;
}
逆序对__归并排序__树状数组 Inversions SGU - 180的更多相关文章
- BZOJ 3295 [CQOI2011]动态逆序对 (三维偏序CDQ+树状数组)
题目大意: 题面传送门 还是一道三维偏序题 每次操作都可以看成这样一个三元组 $<x,w,t>$ ,操作的位置,权值,修改时间 一开始的序列看成n次插入操作 我们先求出不删除时的逆序对总数 ...
- P1966 火柴排队——逆序对(归并,树状数组)
P1966 火柴排队 很好的逆序对板子题: 求的是(x1-x2)*(x1-x2)的最小值: x1*x1+x2*x2-2*x1*x2 让x1*x2最大即可: 可以证明将b,c数组排序后,一一对应的状态是 ...
- [luogu3157][bzoj3295][CQOI2011]动态逆序对【cdq分治+树状数组】
题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...
- 用归并排序或树状数组求逆序对数量 poj2299
题目链接:https://vjudge.net/problem/POJ-2299 推荐讲解树状数组的博客:https://blog.csdn.net/int64ago/article/details/ ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- nyoj322 sort 归并排序,树状数组
Sort 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You want to processe a sequence of n distinct integers b ...
- 康拓展开 & 逆康拓展开 知识总结(树状数组优化)
康拓展开 : 康拓展开,难道他是要飞翔吗?哈哈,当然不是了,康拓具体是哪位大叔,我也不清楚,重要的是 我们需要用到它后面的展开,提到展开,与数学相关的,肯定是一个式子或者一个数进行分解,即 展开. 到 ...
- poj 2299 Ultra-QuickSort(归并排序,树状数组,线段树)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- poj3067 Japan 树状数组求逆序对
题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
随机推荐
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
- javascript void函数
<a href="javascript:doTest2();void(0);">here</a> 但这儿的void(0)究竟是何含义呢? Javascrip ...
- day05_python_1124
---恢复内容开始--- 改l1[1:4:2]=[1:32] 对于增加个数 列表按切片取就是列表 ---恢复内容结束--- 01 昨日内容回顾 list: 增: append insert(ind ...
- 如何写java求和源代码
1.设计思想:利用eclipse编写. 2.程序流程图:先建立一个包->建立一个类->写代码->运行->修正错误,完善代码. 3.源程序代码: package dijia; p ...
- json 2017-04-21 10 17
jo := SO(); jo.S['tttt'] := 'tttt'; TbSendedTidJson['jo'] := jo; ja := TbSendedTidJson['jo'];//取出来后, ...
- C++定义自己的异常
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- vue-router-2-动态路由配置
const User = { template: '<div>User{{ $route.params.id }}</div>' } const router = new Vu ...
- vue-11-自定义指令
用于对纯 DOM 元素进行底层操作. // 注册一个全局自定义指令 v-focus Vue.directive('focus', { // 当绑定元素插入到 DOM 中. inserted: func ...
- table-cell 布局
table-cell能实现段落文字相对于div的垂直居中: 将div设置为display:table-cell; *display:inline-block;text-align:center; ve ...
- python第一个程序HelloWorld
在写第一个python程序之前,我们还需要了解的一个东西就是python解释器 解释器,顾名思义,就是解释一段代码的机器,程序运行的平台,例如Java的解释器就是jdk. 我们在写好的python代码 ...