HDU 5792 World is Exploding(树状数组+离散化)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5792
题意:
思路:
lmin[i]:表示左边比第i个数小的个数。
lmax[i]:表示左边比第i个数大的个数。
rmin[i]:表示右边比第i个数小的个数。
rmax[i]:表示右边比第i个数大的个数。
这些都是可以用树状数组计算出来的,把所有的lmin加起来就是所有(a,b)对的个数,所有lmax加起来就是所有(c,d)对的个数,两者相乘就是所有情况之和了。但是需要注意的是,在这些情况中还存在a=c,a=d,b=c,b=d这四种不符合题意的,需要把这些给减掉。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6 #include<stack> 7 #include<queue> 8 #include<cmath> 9 #include<map> 10 #include<set> 11 using namespace std; 12 typedef long long ll; 13 typedef pair<int,int> pll; 14 const int INF = 0x3f3f3f3f; 15 const int maxn = 50000+5; 16 17 int n; 18 int a[maxn],b[maxn],c[maxn]; 19 int lmin[maxn],lmax[maxn],rmin[maxn],rmax[maxn]; 20 21 int lowbit(int x) 22 { 23 return x&-x; 24 } 25 26 int get_sum(int x) 27 { 28 int ret = 0; 29 while(x>0) 30 { 31 ret+=c[x]; 32 x-=lowbit(x); 33 } 34 return ret; 35 } 36 37 void add(int x) 38 { 39 while(x<=n) 40 { 41 c[x]+=1; 42 x+=lowbit(x); 43 } 44 } 45 46 int main() 47 { 48 //freopen("in.txt","r",stdin); 49 while(~scanf("%d",&n)) 50 { 51 for(int i=1;i<=n;i++) 52 { 53 scanf("%d",&a[i]); 54 b[i]=a[i]; 55 } 56 sort(b+1,b+n+1); 57 int num=unique(b+1,b+n+1)-(b+1); 58 for(int i=1;i<=n;i++) 59 a[i]=lower_bound(b+1,b+num+1,a[i])-(b+1)+1; 60 61 ll suml=0,sumr=0; 62 memset(c,0,sizeof(c)); 63 for(int i=1;i<=n;i++) 64 { 65 lmin[i]=get_sum(a[i]-1); 66 lmax[i]=get_sum(n)-get_sum(a[i]); 67 add(a[i]); 68 suml+=lmin[i]; 69 sumr+=lmax[i]; 70 } 71 memset(c,0,sizeof(c)); 72 for(int i=n;i>=1;i--) 73 { 74 rmin[i]=get_sum(a[i]-1); 75 rmax[i]=get_sum(n)-get_sum(a[i]); 76 add(a[i]); 77 } 78 79 ll ans=suml*sumr; 80 81 for(int i=1;i<=n;i++) 82 { 83 ans-=(ll)rmin[i]*rmax[i];//a==c==a[i] 84 ans-=(ll)lmin[i]*lmax[i];//b==d==a[i] 85 ans-=(ll)lmin[i]*rmin[i];//b==c==a[i] 86 ans-=(ll)lmax[i]*rmax[i];//a==d==a[i] 87 } 88 printf("%lld\n",ans); 89 } 90 return 0; 91 }
转载于:https://www.cnblogs.com/zyb993963526/p/7665826.html
HDU 5792 World is Exploding(树状数组+离散化)相关推荐
- hdu 5792 World is Exploding 树状数组
World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- hdu 5157(manacher+前缀和+树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5157 解题思路: 我们可以先用mancher算法对字符串进行处理,把以每个点为中心的回文串半径求出来, ...
- HDU 5517---Triple(二维树状数组)
题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...
- [HDU - 2852] KiKi's K-Number (树状数组+二分)
链接 http://acm.hdu.edu.cn/showproblem.php?pid=2852 题意 现在需要你对一个空序列做nnn次操作,操作分三种 0x0\ \ x0 x :向序列中加入一个 ...
- HDU - 5775 - Bubble Sort( 树状数组 + 思维 )
题目链接:点击进入 题目 题意 问在给出的冒泡排序过程中,一个数到达的最右边位置与最左边位置距离差. 思路 对于一个数,位置 i ,假设右边比它小的数有 r 个,左边比它大的数有 l 个,最右边到达的 ...
- POJ 3928 hdu 2492 Uva1428 PingPong 【树状数组】
Ping pong Time Limit: 2000/1000 MS (Java/Others) ...
- HDU - 5877 Weak Pair 2016 ACM/ICPC 大连网络赛 J题 dfs+树状数组+离散化
题目链接 You are given a rootedrooted tree of NN nodes, labeled from 1 to NN. To the iith node a non-neg ...
- HDU 3584 Cube (三维树状数组)
Problem Description Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the nu ...
- HDU 3015 Disharmony Trees(树状数组)
题意:给你n棵树,每棵树上有两个权值X H 对于X离散化 :3 7 1 5 3 6 -> 2 6 1 4 2 5,对于H一样 然后F = abs(X1-X2) S=min(H1,H2) 求出 ...
最新文章
- Go 知识点(06)— for range 切片并对切片进行 append 不会造成死循环
- SBB:南土所褚海燕组-pH主导土壤中固氮群落的共存与装配
- nginx 修改并隐藏版本号
- 外键为主键可以重复原因
- 使用StringWriter和StringReader的好处
- 【H.264/AVC视频编解码技术】序章【编码的前世今生】
- skywalking(2)
- PyODPS开发中的最佳实践
- 【图像去模糊】 deconvolution总结
- 阿里云未来三年投2000亿“搞基”背后的野心和公心(2020年4月)
- leetcode python3 简单题198. House Robber
- 伟福6000微型计算机,伟福6000仿真软件
- [论文评析]ArXiv,2021, CrossFormer技术分析
- redis-hmmap-go
- 微商爆粉2.0全自动批量加人模拟手动操作
- C#第九天面向对象的学习
- 计算机二级关于ppt背景音乐,计算机二级考试真题-PPT-校摄影社团优秀作品展
- 苏宁11.11:苏宁双十一大促保障经验
- 学计算机专业必须马上买笔记本吗,大学生刚刚进入学校,需要购买笔记本电脑吗?听听学姐的建议吧...
- 三维模型下构件关联类型系统展示