洛谷P1637 三元上升子序列(树状数组)
来源:互联网 发布:逆袭的感受知乎 编辑:程序博客网 时间:2024/06/17 00:34
三元上升子序列
题目描述
Erwin最近对一种叫”thair”的东西巨感兴趣。。。
在含有n个整数的序列a1,a2……an中,
三个数被称作”thair”当且仅当i< j< k且ai< aj< ak
求一个序列中”thair”的个数。
输入输出格式
输入格式:
开始一个正整数n,
以后n个数a1~an。
输出格式:
“thair”的个数
输入输出样例
Input
4
2 1 3 4
Output
2
Input
5
1 2 2 3 4
Output
7
对样例2的说明:
7个”thair”分别是
1 2 3
1 2 4
1 2 3
1 2 4
1 3 4
2 3 4
2 3 4
说明
约定 30%的数据n<=100
60%的数据n<=2000
100%的数据n<=30000
大数据随机生成
0<=a[i]<=maxlongint
分析:用树状数组统计第i个数前面的比它小的数以及后面的比它大的数最后乘起来求和即可。
代码
#include <cstdio>#include <cstring>#include <algorithm>#define N 30000#define ll long longusing namespace std;struct arr{ int a,b;}p[N];ll a1[N],a2[N],c[N],ans;int n;int so(arr x,arr y){ if (x.a==y.a) return x.b>y.b; return x.a<y.a;}ll sum1(ll x){ ll s=0; while (x>0) { s+=c[x]; x-=x&(-x); } return s;}void change1(int x){ while (x<=n) { c[x]++; x+=x&(-x); }}ll sum2(ll x){ ll s=0; while (x<=n) { s+=c[x]; x+=x&(-x); } return s;}void change2(int x){ while (x>0) { c[x]++; x-=x&(-x); }}int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&p[i].a); p[i].b=i; } sort(p,p+n+1,so); for (int i=1;i<=n;i++) { a1[p[i].b]=sum1(p[i].b); change1(p[i].b); } memset(c,0,sizeof c); for (int i=n;i>=1;i--) { a2[p[i].b]=sum2(p[i].b); change2(p[i].b); } for (int i=1;i<=n;i++) ans+=a1[i]*a2[i]; printf("%lld\n",ans);}
阅读全文
0 0
- 洛谷 P1637 三元上升子序列(树状数组)
- 洛谷P1637 三元上升子序列(树状数组)
- 洛谷P1637 三元上升子序列Thair(树状数组)
- 三元上升子序列 洛谷p1637
- 【洛谷】1637 三元上升子序列 树状数组
- 【洛谷1637】三元上升子序列
- LuoguP1637 三元上升子序列
- hdu4604 Deque,树状数组,最长上升/下降子序列
- BZOJ 3173 Tjoi2013 最长上升子序列 Treap+树状数组
- [树状数组+上升子序列] HDU 3030 Increasing Speed Limits
- BZOJ3173: [Tjoi2013]最长上升子序列 Treap+树状数组
- hdu 1025 Constructing Roads In JGShining's Kingdom(树状数组求最长上升子序列)
- hdu 3030 Increasing Speed Limits(树状数组求最长上升子序列)
- bzoj 3173: [Tjoi2013]最长上升子序列(离线二分+树状数组)
- 三元上升子序列(二重顺序对与乘法原理的应用)
- [树状数组求第K大][BZOJ 3173][TJOI 2013]最长上升子序列
- HDU3030最长上升子序列个数+树状数组求和+二分优化
- FZU 2236 第十四个目标(严格上升子序列个数 离散化 + 树状数组)
- c语言实现matlab上的filter函数
- 对象反序列化异常原因:java.io.InvalidClassException
- 全面理解 JavaScript 中的 this
- Consul和ZooKeeper的区别
- ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named arguments (labels=..., l
- 洛谷P1637 三元上升子序列(树状数组)
- 为什么写这个博客呢。。。。
- 设计模式理解之工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)
- Sqoop将MySQL数据库表中数据导入到HBase表中
- jQuery2
- 代码传奇 | 身价10亿的程序员 雷军当年也为他打工——WPS之父 求伯君
- Java语言冒泡排序详解
- ubuntu以root身份进入图形管理界面
- Matlab数据分析练习-平滑处理