poj 2299 树状数组
来源:互联网 发布:快递单数据录入员兼职 编辑:程序博客网 时间:2024/06/16 01:49
题目链接:http://poj.org/problem?id=2299
题目大意:本题要用到树状数组的离散化处理,因为数据太大(1-999999999),但还是WA了两次,c[i]的范围计算错误,要用到long long .
从后向前处理,每次处理一个数,求比这个数小的个数。
//poj 2299 树状数组#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>using namespace std;#define M 500010int a[M];long long c[M];int m;struct node{ int data; int pos;}s[M];bool cmp(node x,node y){ return x.data<y.data;}int lowbit(int x){ return x&(-x);}void add(int k,int detal){ while(k<=m) { c[k]+=detal; k+=lowbit(k); }}int sum(int k){ int t=0; while(k>0) { t+=c[k]; k-=lowbit(k); } return t;}int main(){ int n; int i,j; while(cin>>n) { if(n==0) break; for(i=1;i<=n;i++) //离散化 { scanf("%d",&s[i].data); s[i].pos=i; } sort(s+1,s+n+1,cmp); int k=0; a[s[1].pos]=1; m=1; for(i=2;i<=n;i++) { if(s[i].data==s[i-1].data) a[s[i].pos]=a[s[i-1].pos]; else a[s[i].pos]=a[s[i-1].pos]+1; if(m<a[s[i].pos]) m=a[s[i].pos]; } //离散化完成 memset(c,0,sizeof(c)); long long ans=0; for(i=n;i>=1;i--) { ans+=sum(a[i]-1); add(a[i],1); } printf("%lld\n",ans); } return 0;}
- poj 2299 树状数组
- POJ 2299 树状数组
- poj 2299 树状数组
- Poj 2299 树状数组
- POJ 2299 树状数组
- poj 2299(树状数组)
- POJ 2299 (树状数组)
- poj 2299 树状数组入门
- poj 2299 poj 2481 树状数组
- poj 2299 逆序数树状数组
- poj 2299树状数组求逆序数
- POJ 2299 树状数组+离散化
- poj 2299 树状数组+离散化
- poj 2299 Ultra-QuickSort (树状数组解法)
- poj - 2299 - Ultra-QuickSort(树状数组)
- [树状数组]POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort(树状数组)
- Poj 2299 Ultra-QuickSort 树状数组 解法
- android学习论坛
- Linux下Weblogic 11g R1安装和配置
- cocos2d-x笔记(一)2.1.4版本安装
- HDU 2131 Probability
- Ora-27102: Out Of Memory: Linux Error: 12: Cannot Allocate Memory with LOCK_SGA=TRUE[ID 401077.1]
- poj 2299 树状数组
- HTML入门教程 - 2.标签、属性和元素(Tags, Attributes, and Elements)
- bat学习之文件遍历
- JavaBean操作详解
- 设置selcet下拉菜单长度
- http缓存控制
- 产品入库表
- win7下安装ubuntu 13.04 双系统及 重启问题
- android 4.3源码下载【自己下载成功的步骤、其中遇到很多你可能会遇到的很多问题】