poj1990(树状数组好题)
来源:互联网 发布:纪录片推荐 知乎 编辑:程序博客网 时间:2024/06/08 16:26
思路请参考:http://blog.csdn.net/allenlsy/article/details/5884428
下面是我的代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<math.h>#define LL __int64#define N 20005#define inf 0x7ffffff#define eps 1e-9#define pi acos(-1.0)using namespace std;struct node{ int vol,x;}s[N];int c[N],countt[N];//c是树状数组,countt[i]记录比音量比i小下标比牛i小的牛的个数int total[N];//total[i]表示音量比牛i小的牛且下标比牛i小的牛的下标和int alltotal[N];//alltotal[i]表示音量比牛i小的牛的下标和bool cmp(node a,node b){ if(a.vol != b.vol) return a.vol < b.vol; return a.x < b.x;}int lowbit(int x){ return x&(-x);}void update(int x, int v){ for(int i = x; i <= 20000; i += lowbit(i)) c[i] += v;}int sum(int x){ int ans = 0; for(int i = x; i > 0; i -= lowbit(i)) ans += c[i]; return ans;}int main(){//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout); int n; while(scanf("%d",&n) != EOF) { int i; for(i = 0; i < n; i++) scanf("%d%d",&s[i].vol,&s[i].x); sort(s,s+n,cmp); memset(c,0,sizeof(c)); memset(countt,0,sizeof(countt)); for(i = 0; i < n; i++) { countt[i] = sum(s[i].x); update(s[i].x,1); } memset(c,0,sizeof(c)); memset(total,0,sizeof(total)); for(i = 0; i < n; i++) { total[i] = sum(s[i].x); update(s[i].x,s[i].x); } LL ans = 0; memset(alltotal,0,sizeof(alltotal)); for(i = 1; i < n; i++) alltotal[i] = alltotal[i-1] + s[i-1].x; for(i = 1; i < n; i++) { //cout<<countt[i]*s[i].x - total[i]<<" "<< alltotal[i]<<" "<<total[i]<<" "; ans = ans + (1LL)*s[i].vol*(countt[i]*s[i].x - total[i] + (alltotal[i] - total[i] - s[i].x*(i-countt[i]) ) ); //cout<<ans<<endl; } printf("%I64d\n",ans); } return 0;}
0 0
- poj1990(树状数组好题)
- 树状数组(好)-poj1990
- POJ1990 (树状数组)
- 待续poj1990(树状数组)
- POJ1990 MooFest(树状数组)
- POJ1990 MooFest,树状数组
- poj1990 树状数组+排序
- poj1990&&uva1428 树状数组
- poj1990两个树状数组
- poj1990 moofest 树状数组
- POJ1990-MppFest-树状数组
- 【树状数组--思维】poj1990 MooFest
- poj1990--我的第一道AC树状数组题
- POJ1990 MooFest 树状数组(Binary Indexed Tree,BIT)
- poj1990~MooFest(哥拿下的第一个树状数组)
- POJ1990 MooFest——树状数组
- poj1990 MooFest && hdu3015 Disharmony Trees (树状数组)
- 树状数组好题(codeforces828E)
- 位运算符
- Qt通过QNetworkAccessManager实现Http get方法文件下载
- 代码点与代码单元
- spark standalone 安装(手动启动)
- JDBC编程理论知识(1)
- poj1990(树状数组好题)
- 深度探索C++对象模型---Member Function的各种调用方式
- ant实现java项目的自动构建和部署
- Android内存泄露方案管理
- 搜* 索*引*擎
- C++设计模式之原型模式(三)
- 确保对象的唯一性——单例模式
- Servlet实现网络IP过滤
- 【网络编程】UDP/TCP 数据包的大小限制