CF E. Enemy is weak 线段树
来源:互联网 发布:棋牌源码论坛eenot 编辑:程序博客网 时间:2024/05/17 15:02
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an army is equal to the number of tripletsi, j, k such that i < j < k and ai > aj > ak whereax is the power of man standing at positionx. The Roman army has one special trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line containsn different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to usecout (also you may use %I64d).
33 2 1
1
32 3 1
0
410 8 3 1
4
41 5 4 3
1思路: big[i] 记录是比a[i] 大且位于a[i] 前面的数的个数;little[i] 是记录比a[i] 小 且位于 a[i] 后面数的个数;那么 big[i]个数中选一个,a[i], little[i]中选一个, 这样的组合肯定满足题目要求;所以 ans= ans+ big[i]*1*little[i] (for i=0 到 n )怎么最快的求big[i],little[i]呢? 用线段树了;#include<stdio.h>#include<string.h>#include<math.h>#include<string>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<list>#include<map>#include<set>using namespace std;const int N=1000010;int n;int a[N];int big[N],little[N];int cnt[N*4];map<int,int>m;int all;void init(){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); big[i]=a[i]; } sort(big,big+n); all=unique(big,big+n)-big; for(int i=1;i<=all;i++) m[big[i-1]]=i; for(int i=0;i<n;i++) a[i]=m[a[i]];}int require(int x,int y,int st,int ed,int i){ if(x>ed || y<st) return 0; if(x<=st && y>=ed) return cnt[i]; int mid=(st+ed)>>1; return require(x,y,st,mid,i*2)+require(x,y,mid+1,ed,i*2+1);}void updata(int x,int st,int ed,int i){ if(x<st || x>ed) return ; cnt[i]++; if(st==ed) return ; int mid=(st+ed)>>1; updata(x,st,mid,i*2); updata(x,mid+1,ed,i*2+1);}void deal_big(){ memset(cnt,0,sizeof(big)); for(int i=0;i<n;i++){ big[i]=require(a[i]+1,all,1,all,1); updata(a[i],1,all,1); }// for(int i=0;i<n;i++)// printf("big==%d\n",big[i]);}void deal_little(){ memset(cnt,0,sizeof(cnt)); for(int i=n-1;i>=0;i--){ little[i]=require(1,a[i]-1,1,all,1); updata(a[i],1,all,1); }// for(int i=0;i<n;i++)// printf("ll=%d\n",little[i]);}void work(){ long long ans=0; for(int i=0;i<n;i++) ans=ans+(1LL)*big[i]*little[i]; cout << ans << endl;}int main(){// freopen("in.in","r",stdin); init(); deal_big(); deal_little(); work(); return 0;}
- CF E. Enemy is weak 线段树
- CF - 61E - Enemy is weak(树状数组)
- 61E Enemy is weak
- cf 61E. Enemy is weak 树状数组求逆序数
- cf 61E. Enemy is weak 树状数组求逆序数(WA)
- CodeForces - 61E Enemy is weak
- [CF 61E]Enermy is weak[线段树求二重逆序数]
- Codeforces Beta Round #57 (Div. 2) E. Enemy is weak
- CodeForces 61E Enemy is weak 【树状数组】
- 【Codeforces61E】Enemy is weak
- Enemy is weak
- Enemy is weak 树状数组
- Codeforces Beta Round #57 (Div. 2)E---Enemy is weak(树状数组+离散化)
- CF 558E 线段树
- Codeforces 61E Enemy is weak 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组
- cf 219E parking lot 线段树
- CF 338E Optimize! (线段树)
- [CF 338E] Optimize! · 线段树
- linux平台与windows平台的float差异
- 网站在架构时要考虑的事情
- B/S、C/S、SaaS到底有什么区别?
- IE不能下载MSG文件的解决方案
- (forward)看到的一篇比较好的AudioFlinger分析
- CF E. Enemy is weak 线段树
- android设置post请求服务器时参数的设置以及JSON参数的设置
- MyBatis流程和框架
- VS2010 Ribbon测试案例
- 如何判断 ios设备的类型(iphone,ipod,ipad)
- Android之SurfaceView使用总结
- Linux read 用法
- 第十八章 TCP连接的建立和终止
- Eclipse启动时弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法