World is Exploding 树状数组+离散化
InputThe input consists of multiple test cases.
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯AnA1,A2⋯An.
1≤n≤500001≤n≤50000
0≤Ai≤1e90≤Ai≤1e9OutputFor each test case,output a line contains an integer.Sample Input
4
2 4 1 3
4
1 2 3 4
Sample Output
1
0 因为只考虑相对大小关系,所以先将数据离散化,然后用树状数组记录前后比i大或者比i小的元素,求出所有个数,分类讨论重合情况,减去重合的元素
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<fstream>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
#define MAXN 500009
LL pre_max[MAXN], pre_min[MAXN], post_max[MAXN], post_min[MAXN];
LL a[MAXN], tmp[MAXN];
LL T[MAXN], n;
LL lowbit(LL x)
{
return x&(-x);
}
void update(LL x)
{
while (x <= MAXN)
{
T[x] += ;
x += lowbit(x);
}
}
LL getsum(LL x)
{
LL sum = ;
while (x > )
{
sum += T[x];
x -= lowbit(x);
}
return sum;
}
int main()
{
while (scanf("%lld", &n) != EOF)
{
memset(T, , sizeof(T));
for (LL i = ; i <= n; i++)
scanf("%lld", &a[i]);
memcpy(tmp, a, sizeof(a));
sort(tmp + , tmp + n + );
LL len = unique(tmp + , tmp + n + ) - tmp;
for (LL i = ; i <= n; i++)
a[i] = lower_bound(tmp + , tmp + len + , a[i]) - tmp;
for (LL i = ; i <= n; i++)
{
update(a[i]);
pre_min[i] = getsum(a[i] - );
pre_max[i] = i - getsum(a[i]);
}
LL sum1, sum2;
sum1 = sum2 = ;
for (int i = ; i <= n; i++)
{
post_min[i] = getsum(a[i] - ) - pre_min[i];
post_max[i] = n - getsum(a[i]) - pre_max[i];
sum1 += post_min[i];
sum2 += post_max[i];
}
LL ans = sum1*sum2;
for (int i = ; i <= n; i++)
{
ans -= pre_min[i] * pre_max[i];
ans -= pre_min[i] * post_min[i];
ans -= post_max[i] * pre_max[i];
ans -= post_max[i] * post_min[i];
}
printf("%lld\n", ans);
}
}
World is Exploding 树状数组+离散化的更多相关文章
- hdu 5792 World is Exploding 树状数组+离散化+容斥
World is Exploding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- HDU 5792 World is Exploding(树状数组+离散化)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...
- HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化
题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...
- 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化
http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...
- BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
随机推荐
- MySQL-时间(time、date、datetime、timestamp和year)
情景进入 情境进入: 今天调试某查询页面,偶尔发现一个问题,刚刚插入的数据,没有正常排序显示,经过后台调试sql,发现一个问题??? 经过上面红色对比,不知道你发现问题没,Order by 只是多一个 ...
- IIS中不让下级应用程序继承主域名的web.config配置
<location path="." allowOverride="true" inheritInChildApplications="fals ...
- wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法
原文链接:wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法 最近升级wordpress之后,发现在文章编辑界面的添加新媒体和可视化按钮点击无反应,如下: 然后就在网上找解决办法, ...
- codeforces_1065_D.three pieces_思维
题意:一个正方形棋盘,三种棋子,knight:像中国象棋中的马一样走:bishop:斜着走:rook:中国象棋中的车.棋盘中每个格子中标着1--n*n的互不相同的数字,从1开始任选一种棋子开始走,在每 ...
- uiviewcontroller顶级布局控制
@available(iOS 7.0, *) open var edgesForExtendedLayout: UIRectEdge // Defaults to UIRectEdgeAll @ava ...
- lua 之 and or not
and是与运算,返回值如下 如果前者为false或者nil,则返回前者,否则后者 A and B 类似如下语句 if not A then return A else return B end 总结: ...
- Laravel Excel安装及最简单使用
官网:https://docs.laravel-excel.com/ 1.安装 1.1.安装要求: PHP: ^7.0 Laravel: ^5.5 PhpSpreadsheet: ^1.6 ...
- Python框架Django的入门
本篇文章主要给大家介绍Django的入门知识:
- 根据数据库表自动生成实体类、xml和dao---mybatis
网盘链接: https://pan.baidu.com/s/1AVGz0bDa_Y5zjk7vXa2eHw 提取码: 2gr6 1.记事本打开generatorConfig.xml文件 2(1,2,3 ...
- 微信小程序(template的使用)
小程序的template是一个模版功能,在创建一个template后,其他的页面可以引用,相比component较简单.方便! template只需要两个文件,一个wxss文件和wxml文件,也只有这 ...