51Nod 1019 逆序数(线段树)
题目链接:逆序数
模板题。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define lson i << 1, L, mid
#define rson i << 1 | 1, mid + 1, R const int N = 100010; long long ans = 0; struct node{
int x, y;
friend bool operator < (const node &a, const node &b){
return a.x < b.x;
}
} a[N]; int tree[N << 2];
int c[N];
int n; inline void pushup(int i){
tree[i] = tree[i << 1] + tree[i << 1 | 1];
} void build(int i, int L, int R){
tree[i] = 0;
if (L == R) return ;
int mid = (L + R) >> 1;
build(lson);
build(rson);
} void update(int i, int L, int R, int pos, int val){
if (L == R && L == pos){
tree[i] += val;
return;
} int mid = (L + R) >> 1;
if (pos <= mid) update(lson, pos, val);
else update(rson, pos, val); pushup(i);
} int query(int i, int L, int R, int l, int r){
if (L == l && R == r) return tree[i];
int mid = (L + R) >> 1;
if (r <= mid) return query(lson, l, r);
else if (l > mid) return query(rson, l, r);
else return query(lson, l, mid) + query(rson, mid + 1, r);
} int main(){ scanf("%d", &n); rep(i, 1, n){
scanf("%d", &a[i].x);
a[i].y = i;
} sort(a + 1, a + n + 1);
c[a[1].y] = 1; rep(i, 2, n) c[a[i].y] = a[i].x == a[i - 1].x ? c[a[i - 1].y] : c[a[i - 1].y] + 1; build(1, 1, n); ans = 0; rep(i, 1, n){
ans += (long long)query(1, 1, n, min(c[i] + 1, n), n);
update(1, 1, n, c[i], 1);
} printf("%lld\n", ans); return 0;
}
51Nod 1019 逆序数(线段树)的更多相关文章
- HDU 1394 Minimum Inversion Number(最小逆序数 线段树)
Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...
- HDU 1394 逆序数 线段树单点跟新 | 暴力
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 51nod 1019 逆序数
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...
- 51nod 1019 逆序数(逆序数+离散化)
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是 ...
- (分治)51NOD 1019 逆序数
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是 ...
- POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化
我用的线段树写的. num数组表示已插入的数值的个数. 由于a[i]数值很大,但是n不是很大,所以要离散化处理 9 1 0 5 4 离散化后 4 1 0 3 2 这样保证最大值不会超过n #inclu ...
- 51Nod 1019 逆序数 (归并排序)
#include <iostream> #include <cstring> using namespace std; ; int a[maxn]; int res[maxn] ...
- 51nod1019逆序数(归并排序/树状数组)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1019 题意:中文题诶- 思路: 方法1:归并排序- 归并排序过 ...
- 逆序对 线段树&树状数组 (重制版)
逆序对的定义:长度为n的数组a,求满足i<j时a[i]>a[j]条件的数对个数. 第一次接触这种问题的人可能是更先想到的是n^2去暴力数前面有几个比他大的数. int main() { i ...
随机推荐
- setTimeout相关整理
setTimeout里面函数有无双引号的区别 双引号中的作用域不捕捉局部变量,不用双引号包着的是捕捉局部作用域 var a = function(){ alert(1111) } function a ...
- cf979d Kuro and GCD and XOR and SUM
set做法 正解是trie-- 主要是要学会 \(a\ \mathrm{xor}\ b \leq a+b\) 这种操作 #include <iostream> #include <c ...
- 【Scramble String】cpp
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- python学习之dictionary函数的用法
编写下面这段代码运行出现了报错.#!/usr/bin/env python2.7#-*-coding:utf-8 -*- d=['T']a=raw_input('请输入a的值')if a in d : ...
- php获取当前操作系统类型
如何使用 php 获取当前操作系统类型呢? 严格来说这里分两种情况,一种情况是获取 服务器端 的操作系统类型,一种是获取 客户端 的操作系统类型. 下面将对如何使用php获取这两种情况下的操作系统类型 ...
- Python-伪私有属性
原文:http://blog.itpub.net/26250550/viewspace-1411768/ 通常在 Python 中,我们都被告知可以使用双下划线开头的方法名定义方法来达到私有函数的目标 ...
- 【转】UGUI(小地图的实现)与游戏关卡选择的简单实现
http://www.jianshu.com/p/68637029e9df 游戏中小地图的实现(场景用简单Cube组成先搭建如下图场景,真实场景实现方法也是一样) 图1-1小地图效果图 1.创建好场景 ...
- jquery中attr和prop的区别介绍
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...
- iOS自定义控件创建原理(持续更新)
前言 因为如果要创建各种自定义控件根据需求的不同会有很多的差别,所以我就在这里,分析一些自定义控件的创建实现方法 弹出视图 1.把要弹出的视图装在一个控制器里面,自定义转场动画 2.创建一个弹出视图, ...
- javascript作用域链理解
执行上下文(Execution context,简称EC) 概念 每当控制器到达ECMAScript可执行代码的时候,就进入了一个执行上下文. javascript中,EC分为三种: ...