HDU 3015 Disharmony Trees 【 树状数组 】
2015-06-30 13:24
363 查看
题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx,
再按照它们的高度排序,记为hh
两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[i],hh[j]),求所有的差异度的和
和上面一道题一样,只不过这题是要Min的值,就将h从大到小排序,保证每一个h都是当前最小的
然后维护比当前x小的坐标的个数,当前区间的总和,前面比x小的坐标的和
View Code
再按照它们的高度排序,记为hh
两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[i],hh[j]),求所有的差异度的和
和上面一道题一样,只不过这题是要Min的值,就将h从大到小排序,保证每一个h都是当前最小的
然后维护比当前x小的坐标的个数,当前区间的总和,前面比x小的坐标的和
#include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<stack> #include<vector> #include<map> #include<set> #include<queue> #include<algorithm> using namespace std; typedef long long LL; const int INF = (1<<30)-1; const int mod=1000000007; const int maxn=100005; int n; struct node{ int x,xx,h,hh,id;}; node a[maxn],b[maxn],q[maxn]; int c[maxn],d[maxn]; int s[maxn]; int cmp1(node n1,node n2){ return n1.x < n2.x; } int cmp2(node n1,node n2){ return n1.h < n2.h;} int cmp3(node n1,node n2){ return n1.h > n2.h;} int lowbit(int x){ return x & (-x);} LL sum(int c[],int x){ LL ret=0; while(x>0){ ret+=c[x]; x-=lowbit(x); } return ret; } void add(int c[],int x,int d){ while(x < maxn){ c[x]+=d;x+=lowbit(x); } } int main(){ while(scanf("%d",&n) != EOF){ memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].h); sort(a+1,a+n+1,cmp1);a[1].xx = 1; for(int i=2;i<=n;i++){ if(a[i].x == a[i-1].x) a[i].xx = a[i-1].xx; else a[i].xx = i; } sort(a+1,a+n+1,cmp2);a[1].hh = 1; for(int i=2;i<=n;i++){ if(a[i].h != a[i-1].h) a[i].hh =i; else a[i].hh = a[i-1].hh; } sort(a+1,a+n+1,cmp3); //for(int i=1;i<=n;i++) //printf("a[%d].x = %d a[%d].h = %d\n",i,a[i].xx,i,a[i].hh); LL ans = 0; for(int i=1;i<=n;i++){ LL x = sum(c,a[i].xx); LL totalfront = sum(d,a[i].xx); LL total = sum(d,maxn); ans += a[i].hh * (x*a[i].xx - totalfront + total - totalfront - (i-x-1)*a[i].xx); // printf("x= %I64d totalfront=%I64d total=%I64d ans = %I64d\n",x,totalfront,total,ans); add(c,a[i].xx,1); add(d,a[i].xx,a[i].xx); } printf("%I64d\n",ans); } return 0; }
View Code
相关文章推荐
- SAT数学常见考点解析之概率
- Objective-C程序设计第10章---More on Variable and Data Types
- linux设置tomcat开机自动启动
- 介绍GRE填空技巧之词项释义法
- Android音频系统之AudioTrack(一)
- 漫谈 iOS Crash 收集框架
- sqoop-1.4.4 编译hbase-0.99.2版本
- 透明状态栏
- spring源码初步学习-自己实现的ioc容器结构
- solr4.7中文分词器(ik-analyzer)配置
- 04 基本元素 进制转换
- Xcode的控制台调试命令
- delphi 截图简单的实现
- 使用AudioTrack进行音频播放
- python
- hdu2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
- PL/SQL8.0 注册码
- 涨停板联盟7月1日分享。
- device_create,device_create_file
- RedHat下更新gcc编译器