题目

按顺序给出N个数字,求出所有的逆序对个数(逆序对指数字 Ai > Aj且 i < j) 
题目链接:hiho_1141 
    数据规模为 100000,必须使用O(nlogn)的算法来进行求解。下标i从0到N-1,依次求出数字Ai,在A[0, i-1]中比Ai大的数字个数K,将所有的K进行加和即可得到结果。 
    这种动态的排序+统计,使用treap。

实现

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
using namespace std;
struct Node{
int val;
int priority;
int count;
int sum;
Node* childs[2];
Node(int v = 0) :val(v){
priority = rand();
childs[0] = childs[1] = NULL;
count = sum = 1;
}
void Update(){
sum = count;
if (childs[0])
sum += childs[0]->sum;
if (childs[1])
sum += childs[1]->sum;
}
};
struct Treap{
Node* root;
Treap(){
root = NULL;
}
void Rotate(Node*& node, bool dir){
Node* ch = node->childs[dir];
node->childs[dir] = ch->childs[!dir];
ch->childs[!dir] = node;
node->Update();
node = ch;
}
void Insert(Node*& node, int val){
if (!node){
node = new Node(val);
return;
}
if (node->val == val){
node->count++;
node->sum++;
return;
}
else{
bool dir = node->val < val;
Insert(node->childs[dir], val);
if (node->childs[dir]->priority > node->priority){
Rotate(node, dir);
}
node->Update();
}
}
int Search(Node* node, int val){
if (!node)
return 0;
if (node->val == val){
if (node->childs[1])
return node->childs[1]->sum;
else
return 0;
}
else if (node->val > val){
return Search(node->childs[0], val) + (node->childs[1]? node->childs[1]->sum + node->count : node->count);
}
else
return Search(node->childs[1], val);
}
};
int main(){
Treap treap;
int N;
scanf("%d", &N);
long long int count = 0;
int val;
for (int i = 0; i < N; i++){
scanf("%d", &val);
count += (treap.Search(treap.root, val));
treap.Insert(treap.root, val);
}
printf("%lld\n", count);
return 0;
}

hiho_1141的更多相关文章

随机推荐

  1. NSString(或者说是UILabel)加入 “行间距” 之后的 “高度”计算

    一.首先,写一个工具类(NSString的分类,增加两个功能,计算高度宽度) #import "NSString+Extension.h" @implementation NSSt ...

  2. linq小笔记;

    1.比较LINQ to Entities的AsQueryable和AsEnumerable方法 C#程序: 复制内容到剪贴板程序代码 using (testContext context = new ...

  3. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  4. favicon.ico的制作

    1.选取需要的图片(jpg的格式等) 2.在网上搜索favicon.ico的制作,将jpg转为ico 3.在html中输入<link rel="shortcut icon" ...

  5. 捕获Insert触发器失败记录

    1.背景 环境:发布服务器A Windows2008+SQL2008,分发服务器B Windows2008+SQL2008,订阅服务器C Windows2008+SQL2012发布服务器A上的用户信息 ...

  6. jQuery源代码学习之五——jQuery.when

    jQuery.when提供了基于一个或多个对象的状态来执行回调函数的功能,通常是基于具有异步事件的异步队列. 如果传入多个异步队列,jQuery.when会返回一个新的主异步队列的只读副本(promi ...

  7. s3c2440 mpll

    S3C2440有两个PLL(phase locked loop)一个是MPLL,一个是UPLL.MPLL用于CPU及其他外围器件,UPLL用于USB.用于产生FCLK, HCLK, PCLK三种频率, ...

  8. Python 进程

    安装Python的paramiko模块 步骤: 1:管理员方式打开cmd,切换到python安装路径的Scripts目录下: 2:执行命令: 1 pip3.5.exe install paramiko ...

  9. MySql5.7.12设置log-bin

    什么是binlog日志 binlog日志记录了MySql数据库的增加.删除.修改操作.用来实现MySql主从复制. 设置binlog日志 在my.cnf中配置binlog日志 [mysqld] log ...

  10. 读书笔记 1 of Statistics :Moments and Moment Generating Functions (c.f. Statistical Inference by George Casella and Roger L. Berger)

    Part 1: Moments Definition 1 For each integer $n$, the nth moment of $X$, $\mu_n^{'}$ is \[\mu_{n}^{ ...