開始用瓜神说的方法撸了一发线段树。早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组

树状数组:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
using namespace std;
#define INF 0xfffffff
const int maxn=100010;
typedef long long LL;
int c[maxn],lmin[maxn],lmax[maxn],rmin[maxn],rmax[maxn],a[maxn];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int l)
{
while(i<maxn)
{
c[i]+=l;
i+=lowbit(i);
}
}
int sum(int i)
{
int ans=0;
while(i>0)
{
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
update(a[i],1);
lmin[i]=sum(a[i]-1);
lmax[i]=i-1-lmin[i];
//cout<<lmin[i]<<" "<<lmax[i]<<endl;
}
memset(c,0,sizeof(c));
for(int i=n;i>=1;i--)
{
update(a[i],1);
rmin[i]=sum(a[i]-1);
rmax[i]=n-i-rmin[i];
//cout<<rmin[i]<<" "<<rmax[i]<<endl;
}
LL ans=0;
for(int i=1;i<=n;i++)
{
ans+=lmin[i]*rmax[i];
ans+=lmax[i]*rmin[i];
}
cout<<ans<<endl;
}
return 0;
}

线段树:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
using namespace std;
#define INF 0xfffffff
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 100000+10
int a[maxn];
int sum[maxn<<2],zmax[maxn],zmin[maxn],ymax[maxn],ymin[maxn];
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
sum[rt]=0;
if(l==r) return ;
int m=(l+r)>>1;
build(lson);
build(rson);
}
int ask(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r) return sum[rt];
int m=(l+r)>>1;
int ans=0;
if(L<=m) ans+=ask(L,R,lson);
if(R>m) ans+=ask(L,R,rson);
return ans;
}
void update(int pos,int add,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=1;
return ;
}
int m=(l+r)>>1;
if(pos<=m) update(pos,add,lson);
else update(pos,add,rson);
pushup(rt);
}
int main()
{
int t;
cin>>t;
int n;
while(t--)
{
cin>>n;
int Max=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
Max=max(Max,a[i]);
}
build(1,Max,1);
for(int i=1;i<=n;i++)
{
zmin[i]=ask(1,a[i],1,Max,1);
zmax[i]=ask(1,Max,1,Max,1)-zmin[i];
update(a[i],1,1,Max,1);
}
build(1,Max,1);
for(int i=n;i>=1;i--)
{
ymin[i]=ask(1,a[i],1,Max,1);
ymax[i]=ask(1,Max,1,Max,1)-ymin[i];
update(a[i],1,1,Max,1);
}
LL ans=0;
for (int i = 1; i <= n; i++)
{
ans += zmin[i] * ymax[i];
ans += zmax[i] * ymin[i];
}
cout<<ans<<endl;
}
return 0;
}

最新文章

  1. Scalaz(57)- scalaz-stream: fs2-多线程编程,fs2 concurrency
  2. PHP时区配置
  3. ubuntu下Eclipse下添加GBK编码
  4. (转) C++中基类和派生类之间的同名函数的重载问题
  5. xfce 双击窗口标题栏无法最大化解决办法
  6. 使用LVS实现负载平衡之Windows Server 2008配置
  7. Java Session 介绍;
  8. 记录远程桌面登录者的IP和MAC
  9. 日志管理 rsyslog服务浅析
  10. JS轮播图
  11. Hbase实例
  12. VisualStudio2013&amp;VS2015内置SQLServer入门 (三)
  13. yii 的mvc工作流
  14. all about AIX MPIO
  15. vue.js - 奇怪的 event 对象
  16. Filebeat 日志收集器 安装和配置
  17. 解读经典《C#高级编程》最全泛型协变逆变解读 页127-131.章4
  18. jenkins将构建成功或失败的信息发送给指定URL(eg: pomelo采用jenkins持续集成)
  19. 中文chrome font-size 10px,11px,12px,rem只为12px解决办法
  20. json 登陆协议分析

热门文章

  1. HTML、CSS知识点总结_D
  2. jmeter压力测试入门
  3. Knockout v3.4.0 中文版教程-13-控制文本内容和外观-css绑定
  4. cf950d A Leapfrog in the Array
  5. static对局部变量的作用
  6. Centos7 安装python3详细教程,解决升级后不兼容问题
  7. ubuntu系统下如何禁用笔记本触摸板
  8. .NET重构(四):窗体继承+模板方法,完美实现组合查询
  9. 刷题总结——玉蟾宫(bzoj3039单调栈)
  10. 【HDOJ5955】Guessing the Dice Roll(概率DP,AC自动机,高斯消元)