World is Exploding 树状数组+离散化
2024-08-30 18:21:17
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ada≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad.
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);
}
}
最新文章
- google play iap 常见问题
- AJAX JSONP源码实现(原理解析)
- MySql安装与MySQL添加用户、删除用户与授权
- SQL in与exists
- Smart210学习-----lcd驱动
- CSS控制div宽度最大宽度/高度和最小宽度/高度
- 设置outlook自动回复
- [2016-03-15]rabbitmq notes
- Dijkstra and Floyd算法
- xml的xPath解析规则
- 策略模式(Stratety)
- Android BLE与终端通信(三)——客户端与服务端通信过程以及实现数据通信
- [译]Ocelot - Tracing
- SAP S/4 HANA 1709 Fully Activated Appliance
- R语言-简单模型画图
- Linux修改hostname时/etc/hosts、/etc/sysconfig/network ,hostname,三者的区别和联系
- php接口 接受ios或android端图片; php接收NSData数据
- Android.mk (1) 函数
- ReactNative For Android 项目实战总结
- python+selenium之简单介绍继承