题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792

题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四个编号互不相同,然后a < b, c < d,a代表的值小于b代表的值,c代表的值大于d代表的值。

解题思路:先考虑a和b这两个编号,遍历每一个编号作为b,然后找到b前面有多少个小于b的值,就是对于这一个编号b合理的编号a,对于每一组a和b,就可以考虑c和d,能够满足条件c和d的很显然就是除去a和b整个序列有多少个逆序对,对吧。然后对于每一个b有x个a满足条件,所以对于当前b,能够满足条件的就是(x*所有的逆序对的个数 - 逆序对中包涵a和b的个数)。所有逆序对很容易就可以用树状数组求出来,x也就是b前面有多少个小于编号b代表的值得个数,也很容易求出来,然后包涵a和b个数的逆序对呢,a前面大于A[a]的个数和a后面小于A[a]的个数,b也是一样的。

设x为对于每个b满足条件的a的个数,na代表和a有关系的逆序对,nb代表和b有关系的逆序对,tot代表所有的逆序对个数,然后对于每一个b就有 tot*x - nb*x - (所有na的值)。

代码:

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#define inf 9223372036854775807
#define INF 9e7+5
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 5e4 + 5;
const int mod = 1e9 + 7;
const db eps = 1e-9;
ll n, c[maxn], a[maxn], num[maxn], tot, cc[maxn], lar[maxn], b[maxn]; struct pos {
ll a, ord, la;
} q[maxn]; void init() {
tot = 0;
memset(c, 0, sizeof(c));
memset(cc, 0, sizeof(cc));
memset(lar, 0, sizeof(lar)); //这个数组好像不用初始化
} ll lowbit(ll x) {
return x & (-x); //求最低位
} void updata(ll x, ll p, ll u[]) { //更新
while (x <= maxn) {
u[x] += p;
x += lowbit(x);
}
} ll getsum(ll x, ll u[]) {
ll ans = 0;
while (x > 0) {
ans += u[x];
x -= lowbit(x);
}
return ans;
}
bool cmp1(const pos&a, const pos&b)
{
return a.a<b.a;
}
bool cmp2(const pos&a, const pos&b){
return a.ord<b.ord;
} void solve() {
while (cin >> n) {
init();
for (int i = 1; i <= n; i++) {
scanf("%I64d", &q[i].a);
q[i].ord = i;
}
int pp = 0; sort(q+1, q+1+n, cmp1);
q[0].a = -1;
for (int i = 1; i <= n; i++) { //离散化
if (q[i].a != q[i-1].a) q[i].la = ++pp;
else {
q[i].la = pp;
}
}
sort(q+1, q+1+n, cmp2);
for (int i = 1; i <= n; i++) {
lar[i] = i - getsum(q[i].la, c) - 1; //求出每个数前面
updata(q[i].la, 1, c); //有多少比他大的数
}
memset(c, 0, sizeof(c));
for (int i = n; i >= 1; i--) {
num[i] = getsum(q[i].la-1, c); //求出每个数的
updata(q[i].la, 1, c); tot += num[i]; //后面比他小的个数
}
ll ans = 0; memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++) {
ll tmp = getsum(q[i].la-1, c); //对当前b满足条件的a的个数
updata(q[i].la, 1, c);
ll tt = getsum(q[i].la-1, cc); //求出和a有关系的逆序对
updata(q[i].la, num[i] + lar[i], cc);//num[i]+lar[i]就是和a有关系的逆序对
ans += tmp * tot - tt - (num[i]+lar[i])*tmp;//这里的num[i]+lar[i]是和b有关系的逆序对
}
cout << ans << endl;
}
} int main(){
//cin.sync_with_stdio(false);
//freopen("tt.txt", "r", stdin);
//freopen("isharp.out", "w", stdout);
solve(); return 0;
}

  

hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化的更多相关文章

  1. HDU 5792:World is Exploding(树状数组求逆序对)

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description   Given a sequ ...

  2. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  3. hdu4630No Pain No Game (多校3)(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=4630 给的题解没看懂..搜解题报告看 了N久  终于在cui大神的指点下 搞明白咋回事了 将1-N中的每个数ai ...

  4. 2015 多校联赛 ——HDU5372(树状数组)

    Sample Input 3 0 0 0 3 0 1 5 0 1 0 0 1 1 0 1 0 0   Sample Output Case #1: 0 0 0 Case #2: 0 1 0 2 有0, ...

  5. Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化

    题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$  $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...

  6. HDU 4630 No Pain No Game(2013多校3 1010题 离线处理+树状数组求最值)

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. 暴力三维树状数组求曼哈顿距离求最值——牛客多校第八场D

    涉及的知识点挺多,但是大多是套路 1.求曼哈顿距离的最值一般对所有情况进行讨论 2.三维树状数组用来求前缀最大值 /* 有一个三维坐标系(x,y,z),取值范围为[1,n],[1,m],[1,h],有 ...

  8. hdu多校第九场 1002 (hdu6681) Rikka with Cake 树状数组维护区间和/离散化

    题意: 在一块长方形蛋糕上切若干刀,每一刀都是从长方形某条边开始,垂直于这条边,但不切到对边,求把长方形切成了多少块. 题解: 块数=交点数+1 因为对于每个交点,唯一且不重复地对应着一块蛋糕. 就是 ...

  9. hdu-5792 World is Exploding(容斥+树状数组)

    题目链接: World is Exploding Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Jav ...

随机推荐

  1. javaScript的几个问题简答

    1.javascript的typeof返回哪些数据类型    Object.number. function. boolean. underfind 2.例举3种强制类型转换和2种隐式类型转换?    ...

  2. bzoj2004公交线路——DP+矩阵加速递推

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 求方案数,想到DP: 因为两个站间距离<=p,所以每p个站中所有车一定都会停靠至 ...

  3. Java中断机制

    1. 引言 当我们点击某个杀毒软件的取消按钮来停止查杀病毒时,当我们在控制台敲入quit命令以结束某个后台服务时……都需要通过一个线程去取消另一个线程正在执行的任务.Java没有提供一种安全直接的方法 ...

  4. make的link_directories命令不起作用

    按照<CMake Practice>中第六章的设置,采用include_directories命令去寻找共享库的路径,src/CMakeLists.txt如下: ADD_EXECUTABL ...

  5. Code:log4

    ylbtech-Code:log4 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. http://logging.apache.org/log4net/ 0 ...

  6. C++笔试题(七)

    微软研究院是一个听起来就牛B的地方啊,反正我是进不去,不过不妨碍我看看他的笔试题到底是怎么样的.下面四道题就是微软研究院的笔试题目,题后附有我的解答.微软研究院(亚洲)的网址是:http://rese ...

  7. CSA Round #50 (Div. 2 only) Min Swaps(模拟)

    传送门 题意 给出一个排列,定义\(value为\sum_{i=1}^{n-1}abs(f[i+1]-f[i])\) \(swap(a[i],a[j])(i≠j)为一次交换\),询问最少的交换次数使得 ...

  8. bzoj 3745: [Coci2015]Norma【分治】

    参考:https://blog.csdn.net/lych_cys/article/details/51203960 真的不擅长这种-- 分治,对于一个(l,r),先递归求出(l,mid),(mid+ ...

  9. 【CodeForces - 651C 】Watchmen(map)

    Watchmen 直接上中文 Descriptions: 钟表匠们的好基友马医生和蛋蛋现在要执行拯救表匠们的任务.在平面内一共有n个表匠,第i个表匠的位置为(xi, yi). 他们需要安排一个任务计划 ...

  10. Java关键字abstract与final总结

    关键字:abstract 用来修饰抽象类与抽象类中的方法 抽象类需要注意的几点: 抽象类不能被实例化.抽象类可以包含属性:方法:构造方法,但是构造方法不能用来new实例,只能被子类调用 有抽象方法的类 ...