hdu 3743 树状数组
2024-10-12 22:50:14
思路:我们只需坚守一个原则,本来就在左边的坚决不把它换到右边。也就是相邻的两个数,左边小,右边大,那么就不调换。这样对每个数,只要统计左边比它大的数的个数。可以从后面开始用树状数组统计比它小的数的个数是一样的。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define Maxn 1000010
#define lowbit(x) (x&(-x))
using namespace std;
int C[Maxn],num[Maxn],n,r[Maxn];
void init()
{
memset(C,,sizeof(C));
}
int cmp(int a,int b)
{
return num[a]<num[b];
}
int Sum(int pos)
{
int sum=;
while(pos>)
{
sum+=C[pos];
pos-=lowbit(pos);
}
return sum;
}
void update(int pos)
{
while(pos<=n)
{
C[pos]++;
pos+=lowbit(pos);
}
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
init();
for(i=;i<=n;i++)
scanf("%d",num+i);
for(i=;i<=n;i++)
r[i]=i;
sort(r+,r+n+,cmp);
int cnt=;
int temp=num[r[]];
num[r[]]=++cnt;
for(i=;i<=n;i++)
{
if(num[r[i]]!=temp) temp=num[r[i]],num[r[i]]=++cnt;
else num[r[i]]=temp;
}
__int64 ans=;
for(i=n;i>=;i--)
{
ans+=(__int64)Sum(num[i]);
update(num[i]);
}
printf("%I64d\n",ans);
}
return ;
}
最新文章
- Ubuntu13.04安装历险记--Mono,Nginx,Asp.Net一个都不能少
- css3 实现逐帧动画
- java日期类型转换总结date timestamp calendar string
- Java compiler level does not match the version of the installed Java project facet.	springmvc1 和 Target runtime Apache Tomcat v7.0 is not defined.
- es增量自定义更新的脚本
- 为什么SSL证书流量暴增?
- 将Sublime Text3添加到右键菜单中
- OpenCV(7)-图像直方图
- dedeCMS修改文章更新发布时间问题
- Netty4具体解释二:开发第一个Netty应用程序
- oracle创建主键序列和在ibatis中应用
- git常见操作和常见错误
- C++、Java语法差异对照表
- 面向对象内置方法之--__str__、__call__、__del__
- (转)JMeter学习逻辑控制器
- PCF学习知识
- 爬虫3 requests基础之下载图片用content(二进制内容)
- 如何在phpstorm中查看yaf框架源码
- 强制windows系统重启at命令
- e620. Activating a Keystroke When Any Component in the Window Has Focus