题目

按顺序给出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. iOS摄像头和相册-UIImagePickerController常用操作

    在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频.并且从相册中选取我们需要的图片或者视频. 关于iOS摄像头和相册的应用,可以使用UIImagePickerController类来完成控制. ...

  2. Altium Designer 文档信息设置以及模板制作

    原理图文档模板制作方法一.在DXP原理图设计环境下,新建一个自由原理图文档.单击:文件→新建→原理图,或者使用快捷键Ctrl+N打开Files资源面板,在“新建”项目下的选择“Schematic Sh ...

  3. 分析app和wap手机网站的不同

    随着手机3G.4G时代的到来,手机功能的越来越强大,手机App的市场是越来越火爆,时代在更新随之而来的事物也在更新,不能更上时代的步伐是落后的表现,所以不仅仅是手机APP在完善,手机网站WAP也在不断 ...

  4. node + nginx + mongo搭建负载均衡

    基于node和nignx和mongo搭建负载均衡 nginx配置: upstream back {                                                  # ...

  5. Python之路-python数据类型(列表、字典、字符串、元祖)操作

    一.列表: 列表的语法,以中括号开通和结尾,元素以逗号隔开.例如:name = [] 列表是以下标取值,第一个元素下标是0,第二个元素下标是1,最后一个元素下标是-1.   1.增加 #name = ...

  6. 【转】Web性能压力测试工具之ApacheBench(ab)详解

    PS:网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压情况下才能真正体现出各种设置所暴露的问题.Apache中有个自带的,名为ab的程序,可以对Apache或其它类型的服务器进行网 ...

  7. 创建和导出SVG的技巧(转载)

    本文转载自: 创建和导出SVG的技巧

  8. #if、#ifdef、#if defined之间的区别【转】

    转自:http://quanminchaoren.iteye.com/blog/1870977 #if的使用说明 #if的后面接的是表达式 #if (MAX==10)||(MAX==20) code. ...

  9. androidBroadCast总结

    BoradCast广播1.接受广播 BroadCastReceiver(接收系统的广播) 1-1:电话的广播 1-1-1:拨打电话的广播 1.创建一个类,继承BoradcastReceiver 2.重 ...

  10. POJ 2876 Cantoring Along

    Description The Cantor set was discovered by Georg Cantor. It is one of the simpler fractals. It is ...