hdu 5147 Sequence II

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

题意:问有多少个这样的四元组(a,b,c,d),满足条件是 1<=a<b<c<d; Aa<Ab; Ac<Ad;

思路:用树状数组求,从右向左求在这个数之前形成多少个逆序数对记录在r数组里面,然后在从左向右求出在输入这个数之前形成多少个逆序数对存在l数组里面,然后枚举b就行;

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 50001
 5 #define ll long long
 6 using namespace std;
 7
 8 int t;
 9 int n;
10 int a[maxn];
11 int c[maxn];
12 ll l[maxn],r[maxn],sum[maxn];
13
14 int lowbit(int x)
15 {
16     return x&-x;
17 }
18
19 void insert(int x,int v)
20 {
21     while(x<maxn)
22     {
23         c[x]+=v;
24         x+=lowbit(x);
25     }
26 }
27
28 ll getsum(int x)
29 {
30     ll ans=0;
31     while(x>0)
32     {
33         ans+=c[x];
34         x-=lowbit(x);
35     }
36     return ans;
37 }
38
39 int main()
40 {
41     scanf("%d",&t);
42     while(t--)
43     {
44         memset(sum,0,sizeof(sum));
45         memset(a,0,sizeof(a));
46         scanf("%d",&n);
47         for(int i=1; i<=n; i++)
48         {
49             scanf("%d",&a[i]);
50         }
51         memset(c,0,sizeof(c));
52         sum[n+1]=0;
53         for(int i=n; i>=1; i--)
54         {
55             r[i]=getsum(n)-getsum(a[i]);
56             insert(a[i],1);
57             sum[i]=sum[i+1]+r[i];
58         }
59         memset(c,0,sizeof(c));
60         for(int i=1; i<=n; i++)
61         {
62             l[i]=getsum(a[i]-1);
63             insert(a[i],1);
64         }
65         ll ans=0;
66         for(int i=2; i<=n; i++)
67         {
68             ans+=(l[i]*sum[i+1]);
69         }
70         printf("%lld\n",ans);
71     }
72     return 0;
73 }

时间: 2024-12-22 17:02:35

hdu 5147 Sequence II的相关文章

hdu 5147 Sequence II(树状数组)

题目链接:hdu 5147 Sequence II 预处理每个位置作为b和c可以组成的对数,然后枚举b的位置计算. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 50005; int N, arr[maxn], fenw[maxn], lef[maxn], rig[maxn]; #d

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

题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 331    Accepted Submission(s): 151 Problem Description Long long ago, there is a sequence A with length n. All numbers in this se

[ACM] hdu 5147 Sequence II (树状数组,前缀和,后缀和)

Sequence II 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) satis

Hdu 5147 Sequence II(树状数字 or 线段树 + 输入外挂 前缀和+后缀和)

题意: 给定1~n的一个排列 用A[]数组保存,问有多少下标(a,b,c,d)四元组满足: a 解析: 题目中n的范围是50000,O(n^2) 复杂度肯定超时.那么这题明显考察的是log2(n)的算法,对于这题可以用线段树或者树状数组,同时要用到输入外挂,不然会超时. 思路(参考别人做法) 枚举c的位置,那么每一次枚举中的方法数为 1~c-1 中(a,b)的个数 乘以 c~n中(c,d)的个数.累加起来即为答案. 1~c-1中(a,b)的个数相当于枚举b的位置,然后计算出b前面有多少数比A[b

HDU 5147 Sequence II 树状数组水题

无聊咯,学某y,水一发 给你n个数的排列,A[1]到A[n],统计多少四元组(a,b,c,d)满足,1<=a<b<c<d<=n,且A[a]<A[b],A[c]<A[d] 树状数组统计前缀和咯 1 #include<cstdio> 2 #include<cstring> 3 #include<cctype> 4 typedef long long ll; 5 const int maxn=5e4+10; 6 int T,n,a[m

HDU 5919 Sequence II(主席树+逆序思想)

Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1422    Accepted Submission(s): 362 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2

HDOJ 5147 Sequence II 树状数组

树状数组: 维护每一个数前面比它小的数的个数,和这个数后面比他大的数的个数 再枚举每个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 121    Accepted Submission(s): 58 Problem Description Long long ago, there is a seq

HDU 5919 Sequence II 主席树

Sequence II Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,?,an There are m queries. In the i-th query, you are given two integers li and ri. Consider the subsequence ali,ali+1,ali+2,?,ari. We can deno

HDOJ 5147 Sequence II 树阵

树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 121    Accepted Submission(s): 58 Problem Description Long long ago, there is a sequen