树状数组 LA 4329 亚洲赛北京赛区题
复习下树状数组
还是蛮有意思的一道题:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=501&page=show_problem&problem=4174
学到几点:
1、树状数组C[i]的构建,一则c[i]=s[i]-s[i-lowbit(i)];这是一直用的做法。如今学到一种新的,直接add(i,a[i]),(s[i]为a[1]到a[i]的和)
2、前缀和思想,树状数组的Sum本身就是基于前缀和的思想。本题把比某数小的数的个数,通过开大量空间+后缀数组,高效的统计出来比某数小的数的个数
3、事实上我认为通过这个题。能够做出来一种O(nlogn)的排序算法。当然不完好的地方就是仅仅能是整数了,可是应该能够用vector+map解决?
贴自己的代码先。,,由于后面的Lrj大牛的代码太简洁...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <functional> using namespace std; #define MAXN 20010
#define SIZE 100015 int a[MAXN],sma[SIZE],c[SIZE],s[SIZE],e[SIZE],tot[SIZE];
int n,mmax; int lowbit(int i)
{
return i & (-i);
} int sum(int i)
{
int ans=0;
for(;i>0;i-=lowbit(i))
ans+=c[i];
return ans;
} void add(int x, int d) {
while(x <= SIZE) {
c[x] += d; x += lowbit(x);
}
} void Init()
{
memset(c,0,sizeof(c));
memset(a,0,sizeof(a));
memset(sma,0,sizeof(sma));
memset(s,0,sizeof(s));
memset(e,0,sizeof(e));
memset(tot,0,sizeof(tot));
} int main()
{
//freopen("la4329.txt","r",stdin);
int t;
long long ans; scanf("%d",&t); while(t--)
{
mmax=0;
ans=0;
Init(); scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
mmax=max(mmax,a[i]);
add(a[i],1);
//c[i]=sum(a[i]-1);
e[a[i]]=1;
sma[a[i]]=sum(a[i]-1); }
memset(c,0,sizeof(c));
for(int i=1;i<=mmax;i++)
{
s[i] =s[i-1]+e[i];
c[i]=s[i]-s[i-lowbit(i)];
tot[i]=sum(i-1);
}
///////////////////////
//for(int i=1;i<=n;i++)
//{
// printf("sma[a[%d]] = %d tot(%d) = %d\n",i,sma[a[i]],a[i],tot[a[i]]);
//}
///////////////////////
for(int i=1;i<=n;i++)
{
int tmp = tot[a[i]]-sma[a[i]];/*a[i]之后比a[i]小的个数*/
ans+=(long long )tmp*(i-1-sma[a[i]])+(long long)sma[a[i]]*(n-i-tmp);
} printf("%lld\n",ans);
} return 0;
}
标程
// LA4329 Ping pong
// Rujia Liu
#include<cstdio>
#include<vector>
using namespace std; //inline int lowbit(int x) { return x&(x^(x-1)); }
inline int lowbit(int x) { return x&-x; } struct FenwickTree {
int n;
vector<int> C; void resize(int n) { this->n = n; C.resize(n); }
void clear() { fill(C.begin(), C.end(), 0); } // ¼ÆËãA[1]+A[2]+...+A[x] (x<=n)
int sum(int x) {
int ret = 0;
while(x > 0) {
ret += C[x]; x -= lowbit(x);
}
return ret;
} // A[x] += d (1<=x<=n)
void add(int x, int d) {
while(x <= n) {
C[x] += d; x += lowbit(x);
}
}
}; const int maxn = 20000 + 5;
int n, a[maxn], c[maxn], d[maxn];
FenwickTree f; int main() {
freopen("la4329.txt","r",stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
int maxa = 0;
for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); maxa = max(maxa, a[i]); }
f.resize(maxa);
f.clear();
for(int i = 1; i <= n; i++) {
f.add(a[i], 1);
c[i] = f.sum(a[i]-1);
}
f.clear();
for(int i = n; i >= 1; i--) {
f.add(a[i], 1);
d[i] = f.sum(a[i]-1);
} long long ans = 0;
for(int i = 1; i <= n; i++)
ans += (long long)c[i]*(n-i-d[i]) + (long long)(i-c[i]-1)*d[i];
printf("%lld\n", ans);
}
return 0;
}
树状数组 LA 4329 亚洲赛北京赛区题的更多相关文章
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)
<题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...
- 二维树状数组+差分【p4514】上帝造题的七分钟
Description "第一分钟,X说,要有矩阵,于是便有了一个里面写满了\(0\)的\(n\times m\)矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为\((a,b)\),右 ...
- 树状数组+二分答案查询第k大的数 (团体程序设计天梯赛 L3-002. 堆栈)
前提是数的范围较小 1 数据范围:O(n) 2 查第k大的数i:log(n)(树状数组查询小于等于i的数目)*log(n)(二分找到i) 3 添加:log(n) (树状数组) 4 删除:log(n) ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
- poj 2155 Matrix---树状数组套树状数组
二维树状数组模版,唯一困难,看题!!(其实是我英语渣) Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22098 ...
- poj2155 树状数组 Matrix
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14826 Accepted: 5583 Descripti ...
- Why Did the Cow Cross the Road III(树状数组)
Why Did the Cow Cross the Road III 时间限制: 1 Sec 内存限制: 128 MB提交: 65 解决: 28[提交][状态][讨论版] 题目描述 The lay ...
- [BZOJ4765]普通计算姬(分块+树状数组)
4765: 普通计算姬 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 1725 Solved: 376[Submit][Status][Discus ...
随机推荐
- 并发-5CAS与AQS
juc: java.util.concurrent 锁: 悲观锁:写的比较多,对数据的增删改,读(查)少.Lock 乐观锁:反之,读多写少.版本 并发编程之 CAS 的原理 什么是CAS CAS (c ...
- C#导出word [无规则表结构+模板遇到的坑]
1)当然可以考虑使用aspose.word.使用书签替换的方案替换模板中对应的书签值. 2)但是我使用了Interop.Word,下面记录使用类及要注意的地方 3)使用类 Report.cs 来自于网 ...
- Mysql:零散记录
limit用法 查询第4行记录 select * from tablename limit 3,1; limit 3,1:截取第3行加1行的数据 查询第6-15行 select * from tabl ...
- Jquery validate自定义验证
http://www.runoob.com/jquery/jquery-plugin-validate.html addMethod(name,method,message)方法 参数 name 是添 ...
- 【转】jquery 注册事件的方法
原文链接:http://outofmemory.cn/code-snippet/2123/jquery-zhuce-event-method 1.使用事件名来绑定,可用的事件名有 change,cli ...
- Javascript类型转换的规则全面&附有实例
Javascript的变量是松散类型的,它可以存储Javascript支持的任何数据类型,其变量的类型可以在运行时被动态改变.请看示例: 1 2 3 var n = 10; n = "hel ...
- CSU 1214 找最大异或值
题目大意: 给定一堆数,从中找2个数异或得到的最大值 直接暴力会超时,我们要考虑对于每一个数去匹配找到异或的最大值,我们希望2进制越前面的数尽可能都为1 所以我们用 0-1 字典树保存这些数,因为一个 ...
- 【KMP+最小循环节】F. Cyclic Nacklace
https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/F [题意] 给定一个字符串,问在字符串后最少添加多少个字母,得到的新字符串能是前 ...
- uva10537 最短路 倒推
题意:知道了,最后需要的,那么就倒着最短路,推出去就可以了. 以最短路的方式来解决.
- 常见问题:Linux安装Python3步骤、Windows无法利用pip
Linux安装python3.6和第三方库的步骤: 我的Linux是CentOS 6.5版本 Linux下大部分系统默认自带python2.x的版本,最常见的是python2.6或python2.7, ...