Sequence II

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 652    Accepted Submission(s): 164

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. 1≤a<b<c<d≤n
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
 
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 50005+100
const int inf=0x7fffffff; //无限大
int a[maxn];
ll dp_qmin[maxn];
ll dp2[maxn],sum[maxn];
ll dp_hmax[maxn];
int n;
long long ans; int lowbit(int x)
{
return x&(-x);
} void update(int x,ll val)
{
while(x <= n)
{
sum[x] += val;
x += lowbit(x);
}
} long long query(int x)
{
long long s=;
while(x>)
{
s += sum[x];
x -= lowbit(x);
}
return s;
} int main()
{
//freopen("D.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
dp_qmin[i]=;
dp2[i]=;
dp_hmax[i]=;
sum[i]=;
}
for(int i=;i<=n;i++){
ll t=query(a[i]-);
dp_qmin[i]=t;
dp_hmax[i]=(n-i)-(a[i]--t);
//printf("i=%d dpmin=%lld dpmax=%lld\n",i,dp_qmin[i],dp_hmax[i]);
update(a[i],);
}
ll ans=;
ll sum1=;
sum1=dp_qmin[]+dp_qmin[];
for(int i=;i<=n-;i++)
{
ans+=sum1*dp_hmax[i];
sum1+=dp_qmin[i];
}
printf("%I64d\n",ans);
}
}
 
 

bestcoder#23 1002 Sequence II 树状数组+DP的更多相关文章

  1. hdu 5147 Sequence II 树状数组

    Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  2. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))

    传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...

  4. POJ 2464 Brownie Points II --树状数组

    题意: 有点迷.有一些点,Stan先选择某个点,经过这个点画一条竖线,Ollie选择一个经过这条直接的点画一条横线.Stan选这两条直线分成的左下和右上部分的点,Ollie选左上和右下部分的点.Sta ...

  5. HDU 5273 Dylans loves sequence【 树状数组 】

    题意:给出n个数,再给出q个询问,求L到R的逆序对的个数 先自己写的时候,是每次询问都重新插入来求sum(r)-sum(l) 果断T 后来还是看了别人的代码---- 预处理一下,把所有可能的区间的询问 ...

  6. 【USACO】奶牛抗议 树状数组+dp

    题目描述 约翰家的 N 头奶牛正在排队游行抗议.一些奶牛情绪激动,约翰测算下来,排在第 i 位的奶牛 的理智度为 A i ,数字可正可负. 约翰希望奶牛在抗议时保持理性,为此,他打算将这条队伍分割成几 ...

  7. CodeForces - 314C Sereja and Subsequences (树状数组+dp)

    Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...

  8. [Codeforces261D]Maxim and Increasing Subsequence——树状数组+DP

    题目链接: Codeforces261D 题目大意:$k$次询问,每次给出一个长度为$n$的序列$b$及$b$中的最大值$maxb$,构造出序列$a$为$t$个序列$b$连接而成,求$a$的最长上升子 ...

  9. hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

    Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

随机推荐

  1. mysql状态查看 QPS/TPS/缓存命中率查看【转】

    运行中的mysql状态查看   对正在运行的mysql进行监控,其中一个方式就是查看mysql运行状态.    (1)QPS(每秒Query量)  QPS = Questions(or Queries ...

  2. Django BoundField

    一.BoundField from django.forms.boundfield import BoundField BoundField是一个将字段添加数据的一个类,给对应的form字段封装上数据 ...

  3. 【小程序开发总结】微信小程序开发常用技术方法总结

    1.获取input的值 <input bindinput="bindKeyInput" placeholder="输入同步到view中"/>   b ...

  4. centos7的防火墙(firewalld)

    Centos7中默认将原来的防火墙iptables升级为了firewalld,firewalld跟iptables比起来至少有两大好处: 1.firewalld可以动态修改单条规则,而不需要像ipta ...

  5. AdvStringGrid 垂直居中 、水平居中

    官网faq,解答: 结果:

  6. 移动端布局 - REM方式

    默认以宽度为640px的设计稿为基准页面,然后通过JS获取当前显示设备的尺寸,对应的调整 html 标签的font-size大小,从而实现通过以rem为单位的移动端布局适配. 具体代码 (functi ...

  7. SQL 2008 还原SQL 2005备份文件不成功的解决方

    在SQL Server 2008 r2上还原一个SQL Server 2005时,还原不成功,提示如下信息: 按照如下情况则还原成功: -- 查看备份文件的类型 RESTORE FILELISTONL ...

  8. KAFKA随机产生JMX 端口指定的问题

    https://blog.csdn.net/weixin_40209426/article/details/82217987

  9. 【LOJ】#2054. 「TJOI / HEOI2016」树

    题解 一写过一交A的水题 只要求一个dfs序,新加一个标记在子树所在的区间上覆盖上该点,维护深度最大的答案 代码 #include <bits/stdc++.h> #define ente ...

  10. Java 中常见数据类型的相互转换

    1.将String 转化成 long , 转化成 double String 转化成 long : long i = Long.parseLong(String s);    或 long i = L ...