题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
//对纵坐标y用线段树
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 200010;
int a[N], tr[N], lower[N], bigger[N];
int n;
int lowbit(int x)
{
return x & -x;
}
void add(int x, int c)//修改某个值
{
for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
int sum(int x)//查询某一段的和
{
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++)
{
int y = a[i];
bigger[i] = sum(n) - sum(y);
lower[i] = sum(y - 1);
add(y, 1);
}
memset(tr, 0, sizeof tr);
LL res1 = 0, res2 = 0;//赋初值;
for (int i = n; i; i --)
{
int y = a[i];
res1 += bigger[i] * (LL)(sum(n) - sum(y));
res2 += lower[i] * (LL)sum(y - 1);
add(y, 1);
}
cout << res1 << " " << res2 << endl;
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
踩
为啥都踩,那我也踩了
感谢orz