【树状数组】cal 题解
来源:互联网 发布:手机商城模板php 编辑:程序博客网 时间:2024/06/07 04:57
求序列顺序对个数
#include <iostream>#include <sstream>#include <cstdio>#include <cstdlib>#include <ctime>#include <cmath>#include <cctype>#include <cstring>#include <algorithm>#ifndef WIN32#define Auto "%lld"#else#define Auto "%I64d"#endifusing namespace std;#define lowbit(__a) (__a & (-__a))typedef class IndexedTree { public: long long *lis; int size; IndexedTree() { } IndexedTree(int size):size(size) { lis = new long long[(size + 1)]; memset(lis, 0, sizeof(long long) * (size + 1)); } inline void add(int idx, long long x) { for(; idx <= size; idx += lowbit(idx)) lis[idx] += x; } inline long long getSum(int idx) { long long ret = 0; for(; idx; idx -= lowbit(idx)) ret += lis[idx]; return ret; }}IndexedTree;#define ll long longint n;int* arr;int* buf;int* f;inline void init() { scanf("%d", &n); arr = new int[(n + 1)]; buf = new int[(n + 1)]; f = new int[(n + 1)]; for(int i = 1; i <= n; i++) scanf("%d", arr + i);}inline void discrete() { memcpy(buf, arr, sizeof(int) * (n + 1)); sort(buf + 1, buf + n + 1); for(int i = 1; i <= n; i++) arr[i] = lower_bound(buf + 1, buf + n + 1, arr[i]) - buf;}IndexedTree it1, it2;ll res;inline void solve() { it1 = IndexedTree(n); it2 = IndexedTree(n); for(int i = 1; i <= n; i++) { it1.add(arr[i], 1); f[i] = it1.getSum(arr[i] - 1); } for(int i = 1; i <= n; i++) { it2.add(arr[i], f[i]); res += it2.getSum(arr[i] - 1); } printf(Auto, res);}int main() { init(); discrete(); solve(); return 0;}
阅读全文
0 0
- 【树状数组】cal 题解
- cal (树状数组)
- 【ZJOI2017】树状数组 题解
- 【树状数组套树状数组】BZOJ1452(JSOI2009)[Count]题解
- 【POJ】【P3321】【Apple Tree】【题解】【树状数组】
- 【POJ】【P2352】【Stars】【题解】【树状数组】
- HDU 1166 敌兵布阵 树状数组题解
- 【BZOJ】【P3155】【Preprefix sum】【题解】【树状数组】
- 【BZOJ】【P3813】【奇数国】【题解】【树状数组】
- 【HDU5775】【树状数组】Bubble Sort 题解
- (CF792 ABCD题解) 模拟,树状数组
- 【贪心+堆+树状数组】JXOI2017[加法]题解
- BZOJ 3155: Preprefix sum 树状数组题解
- 【BZOJ2789】【树状数组】[Poi2012]Letters 题解
- 【DP】【线段树】【树状数组】saber 题解
- cal
- cal
- cal
- jvm和类加载
- LintCode 解题记录 17.10.5 递归
- Sourcetree 上进行git-flow功能测试(超级详细)
- 17.10.6B组总结
- 【51nod】1007 正整数分组
- 【树状数组】cal 题解
- caffe源码阅读——SyncedMemory.cpp
- 数据结构C++小笔记《2017-10-06》
- docker镜像管理命令学习
- vmware12中ubuntu15 16的vmware tools失效,导致不能复制粘贴文件以及自动适应窗口分辨率
- 【模拟】hex 题解
- 数组中重复的数
- nyoj242计算球体积
- 五分钟学会使用spring-data-cassandra快速实现数据的访问