hdu 5147 树状数组

简介:

http://acm.hdu.edu.cn/showproblem.php?pid=5147

Problem Description
Long long ago, there is a sequence A with length n. All numbers in this sequence is no smaller than 1 and no bigger than n, and all numbers are different in this sequence.
Please calculate how many quad (a,b,c,d) satisfy:
1.  1a<b<c<dn
2.  Aa<Ab
3.  Ac<Ad
 

Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with a line contains an integer n.
The next line follows n integers  A1,A2,,An.

[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <=  Ai <= n
 

Output
For each case output one line contains a integer,the number of quad.
 

Sample Input
 
  
1 5 1 3 2 4 5
 

Sample Output
 
  
4
/**
hdu5147 树状数组
解题思路:
        要统计四元组的数量我们能够通过枚举c,然后统计区间[1,c-1]有多少二元组(a,b)满足a<b且Aa<Ab。以及统计出区间[c+1,n]有多少d满足Ac<Ad,
依据乘法原理,把这两项乘起来就能够统计到答案里了.然后我们来处理子问题:区间[1,c-1]内有多少二元组(a,b).那么我们能够枚举b,然后统计
区间[1,b-1]内有多少a满足Aa<Ab,那么这个能够通过用树状数组询问前缀和来实现.

详细实现:b[i]和c[i]中存储的分别为以i结尾的Ax<Ay的对数和从i+1到n中Ax<Ay的对数,二者相乘即为答案。
时间复杂度是O(nlogn).
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;

int C[100005],b[100005],c[100005],a[100005];
int n;

int lowbit(int x)
{
    return x&(-x);
}
int sum(int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=C[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int x,int d)
{
    while(x<=n)
    {
        C[x]+=d;
        x+=lowbit(x);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(C,0,sizeof(C));
        for(int i=1;i<=n;i++)
        {
            b[i]=sum(a[i]);
            add(a[i],1);
        }
        memset(C,0,sizeof(C));
        for(int i=n;i>=1;i--)
        {
            c[i]=sum(n)-sum(a[i])+c[i+1];
            add(a[i],1);
        }
        LL ans=0;
        for(int i=2;i<=n-2;i++)
        {
            LL t1=b[i];
            LL t2=c[i+1];
            ans+=t1*t2;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


 
 

相关文章
|
存储 缓存 固态存储
【vsan数据恢复】vsan分布式存储架构数据恢复案例
VSAN数据恢复环境: 一套有三台服务器节点的VSAN超融合基础架构,每台服务器节点上配置2块SSD硬盘和4块机械硬盘。 每个服务器节点上配置有两个磁盘组,每个磁盘组使用1个SSD硬盘作为缓存盘,2个机械硬盘作为容量盘。三台服务器节点上共配置6个磁盘组,共同组成VSAN存储空间,存放虚拟机文件。 需要恢复服务器节点上的数据库数据。 VSAN故障: 非正常关机导致VSAN逻辑架构出现故障,部分虚拟机磁盘组件出现问题,磁盘文件丢失。
|
数据中心
|
5月前
|
存储 安全 Java
《数据之美》:Java集合框架全景解析
Java集合框架是数据管理的核心工具,涵盖List、Set、Map等体系,提供丰富接口与实现类,支持高效的数据操作与算法处理。
|
运维 Devops jenkins
十六年所思所感,聊聊这些年我所经历的 DevOps 系统
十六年所思所感,聊聊这些年我所经历的 DevOps 系统
294 2
|
数据可视化 API
【Qt 学习笔记】Qt常用控件 | 多元素控件 | Tree Widget的说明及介绍
【Qt 学习笔记】Qt常用控件 | 多元素控件 | Tree Widget的说明及介绍
1115 2
基于EO平衡优化器算法的目标函数最优值求解matlab仿真
本程序基于进化优化(EO)中的平衡优化器算法,在MATLAB2022A上实现九个测试函数的最优值求解及优化收敛曲线仿真。平衡优化器通过模拟生态系统平衡机制,动态调整搜索参数,确保种群多样性与收敛性的平衡,高效搜索全局或近全局最优解。程序核心为平衡优化算法,结合粒子群优化思想,引入动态调整策略,促进快速探索与有效利用解空间。
Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_ZMCertification", referenced from:解决方法
Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_ZMCertification", referenced from:解决方法
374 0
|
存储 C语言
【C语言篇】scanf和printf万字超详细介绍(基本加拓展用法)(上篇)
printf 的作⽤是将参数⽂本输出到屏幕。它名字⾥⾯的 f 代表 format (格式化),表⽰可以定制输出⽂本的格式。
387 1
|
人工智能
WPS AI试用(与GPT、Claude参照对比)
WPS AI试用(与GPT、Claude参照对比)
715 1
|
消息中间件 存储 安全
handler
handler
283 0