三元逆序对 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组Codeforces 61E Enemy is weak
来源:互联网 发布:linux shell 嵌套 编辑:程序博客网 时间:2024/09/21 06:18
http://codeforces.com/problemset/problem/61/E
题意是求 i<j<k && a[i]>a[j]>a[k] 的对数
如果只有2元组那就是求逆序数的做法
三元组的话就用一个树状数组x表示 数字i前面有多少个比自己大的个数
然后每次给这个y数组求和,再把x中>a[i]的个数存入y中即可
即使是4元也可以求了#include <algorithm>#include <cctype>#include <cassert>#include <cstdio>#include <cstring>#include <climits>#include <vector>#include<iostream>using namespace std;#define ll long longinline void rd(int &ret){char c;do { c = getchar();} while(c < '0' || c > '9');ret = c - '0';while((c=getchar()) >= '0' && c <= '9')ret = ret * 10 + ( c - '0' );}#define N 1000005#define eps 1e-8#define inf 1000000ll n;struct node{ll c[N];inline ll lowbit(ll x){return x&-x;}void init(){memset(c, 0, sizeof c);}ll sum(ll x){ll ans = 0;while(x<=n+10)ans += c[x], x+=lowbit(x);return ans;}void change(ll x, ll y){while(x)c[x] +=y, x-=lowbit(x);}}x, y;int haifei[1000000], panting[1000000];int main(){ll i, j;while(cin>>n){ll ans = 0;for(i = 0; i < n; i++)rd(haifei[i]), panting[i] = haifei[i];x.init(); y.init();sort(haifei, haifei+n);for(i = 0; i < n; i++){ll b = (lower_bound(haifei, haifei+n, panting[i]) - haifei) +1;ll siz = y.sum(b);ans += siz;y.change(b, x.sum(b));x.change(b, 1);}cout<<ans<<endl;}return 0;}
0 0
- 三元逆序对 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组Codeforces 61E Enemy is weak
- Codeforces 61E Enemy is weak 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组
- 找出数组a[]中符合a[i]+a[j]=K的数对
- 三个已排序的数组a,b,c,求i,j,k使a[i]=b[j]=c[k]
- CareerCup Find 3 indexes i,j,k such that, i<j<k and a[i] < a[j] < a[k]
- 两个有序数组,A[k]和B[k]长度都为k。求前k个最小的(a[i]+b[j])
- 一个数组a[0...n-1],求a[i]-a[j]的最大值,其中i>j
- 一个数组a[0...n-1],求a[j]-a[i]的最大值,其中i<j
- 求一个数组(a(i,j))中元素相减的最大值,且i<=j
- 数组a[i++]=j 与 a[i]++ 的区别
- 对数组 {'f','F','K','A','a','j','z'} 排序
- 2012年成都Onsite (A,B,I,J,K)
- 【分治法】在数组A中,返回i<j,且A[i]>A[j]的(i,j)配对的数量
- cf 61E. Enemy is weak 树状数组求逆序数
- cf 61E. Enemy is weak 树状数组求逆序数(WA)
- 求a[j]-a[i]的最大值,其中j>=i
- 找数组的i,j(j>i)使得a[j] - a[i]的值最大(算法)
- CodeForces 61E Enemy is weak 【树状数组】
- 覆盖索引VS非覆盖索引
- oracle日期时间段查询遇到问题to_date ora-01847 day of month must be between 1 and last day of month
- 【记忆化DFS】HDOJ1242 Rescue
- C# Winform跨线程更新UIkongjian
- C语言多文件编译
- 三元逆序对 求i<j<k && a[i]>a[j]>a[k] 的对数 树状数组Codeforces 61E Enemy is weak
- LaTeX调用算法宏包
- Sqlite学习笔记(11)-API(2)
- 详述int,longlong int的大小
- Android笔记
- Callback函数详解
- k-d tree算法
- [Exercises]24 Graphical User Interfaces
- canvas实现简易时钟效果