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[maxn],c[maxn],l[maxn],r;
 7 ll ans;
 8 inline void modify(int x){while (x<=n) c[x]++,x+=x&-x;}
 9 inline int sum(int x)
10 {
11     int ret=0;
12     while (x>0) ret+=c[x],x-=x&-x;
13     return ret;
14 }
15 inline void read(int &x)
16 {
17     char ch;x=0;
18     while (!isdigit(ch=getchar()));
19     do x=x*10+ch-‘0‘;
20     while (isdigit(ch=getchar()));
21 }
22 int main()
23 {
24     read(T);
25     while (T--)
26     {
27         read(n);
28         for (int i=1;i<=n;i++) read(a[i]);
29         memset(c,0,sizeof(int)*(n+1));
30         for (int i=1;i<=n;i++)
31         {
32             l[i]=sum(a[i]);
33             modify(a[i]);
34         }
35         memset(c,0,sizeof(int)*(n+1));ans=r=0;
36         for (int i=n;i>=1;i--)
37         {
38             ans+=(ll)l[i]*r;
39             r+=sum(n-a[i]+1);
40             modify(n-a[i]+1);
41         }
42         printf("%I64d\n",ans);
43     }
44     return 0;
45 }

时间: 2025-01-03 16:30:37

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

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

HDU1166 敌兵布阵 树状数组水题

中文题目,很简单的题目,区间求和,当然对于线段树来说也很水,为了练习一下树状数组,多做做水题吧,加深理解,并且打好基础,我算是被没打好基础给吓坏了,宁可多花几个小时 刷刷水题扎实点, 很裸的题目 操作也很裸,了解树状数组的肯定能做 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string&g

hdu 1541 Stars 树状数组水题

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5210    Accepted Submission(s): 2053 Problem Description Astronomers often examine star maps where stars are represented by points on a pla

HDU 5147 BestCoder #23(树状数组区间 前缀和,后缀和)类似LA4329

类似LA4329 1..n个数字放到n个格子中,求四元组满足(a,b,c,d)  a<b<c<d 且 Aa<Ab,Ac<Ad.的个数. 这道题刚开始看就知道要用树状数组去做,起先想的是枚举a,c 这样的话复杂度n^2 必然TLE而且a,c之间大于a的数字也无法统计. 题解:枚举c点. 然后得到c之前满足a,b的数量再乘上比c大的d 的数量就是枚举c此时的数量.这里用了一个子问题的技巧,当枚举c到i点的时候,i-1的情况已知,即存储一个满足a,b在i-1时候的量 然后当到i时候

hdu5147 Sequence II树状数组求逆序对

//用树状数组求出在b前面比b小的数的个数 //然后求b后面的顺序对的个数, //枚举b可得quad //由于数列是从1到n的所有数 //那么(n-num[j])-(j-1-totol[j])即为第j个数之后比j大的数的个数 //其中num[j]表示第j个数,total[j]表示在j之前比j小的数的个数 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int

【不可能的任务1/200】bzoj1103树状数组水题

(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 int N=0,n,m,p,q,a[300000],l[300000],pos[300000],end[300000],son[300000],bro[30

Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.