hdoj 3015 Disharmony Trees(树状数组)
来源:互联网 发布:广州海颐软件 编辑:程序博客网 时间:2024/04/29 19:15
思路和poj1990一样,开两个树状数组,分别维护左右的数量和距离和。
注意要使用long long。
代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;ll tree_num[maxn], tree_dis[maxn];struct node{ int x, h, rx, rh;}a[maxn];bool cmp(const node &x, const node &y){ return x.rh > y.rh;}bool cmp1(const node &x, const node y){ return x.x < y.x;}bool cmp2(const node &x, const node &y){ return x.h < y.h;}int lowbit(int x) { return x&(-x); }void update(ll *tree, int pos, int val){ while(pos < maxn) { tree[pos] += val; pos += lowbit(pos); }}ll query(ll *tree, int pos){ ll sum = 0; while(pos) { sum += tree[pos]; pos -= lowbit(pos); } return sum;}int main(void){ int n, k; while(cin >> n) { memset(tree_num, 0, sizeof(tree_num)); memset(tree_dis, 0, sizeof(tree_dis)); for(int i = 0; i < n; i++) scanf("%d%d", &a[i].x, &a[i].h); // sort(a, a+n, cmp1); k = 2; a[0].rx = 1; for(int i = 1; i < n; i++, k++) if(a[i].x == a[i-1].x) a[i].rx = a[i-1].rx; else a[i].rx = k; // sort(a, a+n, cmp2); a[0].rh = 1; k = 2; for(int i = 1; i < n; i++, k++) if(a[i].h == a[i-1].h) a[i].rh = a[i-1].rh; else a[i].rh = k; // sort(a, a+n, cmp); ll ans = 0; for(int i = 0; i < n; i++) { ll lnum = query(tree_num, a[i].rx-1); ll rnum = i-query(tree_num, a[i].rx); ll lsum = query(tree_dis, a[i].rx-1); ll rsum = query(tree_dis, maxn-1)-query(tree_dis, a[i].rx); ans += a[i].rh*((lnum*a[i].rx-lsum)+(rsum-rnum*a[i].rx)); update(tree_num, a[i].rx, 1); update(tree_dis, a[i].rx, a[i].rx); } printf("%lld\n", ans); } return 0;}
Disharmony Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 942 Accepted Submission(s): 466
Problem DescriptionOne day Sophia finds a very big square. There are n trees in the square. They are all so tall. Sophia is very interesting in them.
She finds that trees maybe disharmony and the Disharmony Value between two trees is associated with two value called FAR and SHORT.
The FAR is defined as the following:If we rank all these trees according to their X Coordinates in ascending order.The tree with smallest X Coordinate is ranked 1th.The trees with the same X Coordinates are ranked the same. For example,if there are 5 tree with X Coordinates 3,3,1,3,4. Then their ranks may be 2,2,1,2,5. The FAR of two trees with X Coordinate ranks D1 and D2 is defined as F = abs(D1-D2).
The SHORT is defined similar to the FAR. If we rank all these trees according to their heights in ascending order,the tree with shortest height is ranked 1th.The trees with the same heights are ranked the same. For example, if there are 5 tree with heights 4,1,9,7,4. Then their ranks may be 2,1,5,4,2. The SHORT of two trees with height ranks H1 and H2 is defined as S=min(H1,H2).
Two tree’s Disharmony Value is defined as F*S. So from the definition above we can see that, if two trees’s FAR is larger , the Disharmony Value is bigger. And the Disharmony value is also associated with the shorter one of the two trees.
Now give you every tree’s X Coordinate and their height , Please tell Sophia the sum of every two trees’s Disharmony value among all trees.
InputThere are several test cases in the input
For each test case, the first line contain one integer N (2 <= N <= 100,000) N represents the number of trees.
Then following N lines, each line contain two integers : X, H (0 < X,H <=1,000,000,000 ), indicating the tree is located in Coordinates X and its height is H.
OutputFor each test case output the sum of every two trees’s Disharmony value among all trees. The answer is within signed 64-bit integer.
Sample Input210 10020 200410 10050 50020 20020 100
Sample Output113
Source2009 Multi-University Training Contest 12 - Host by FZU
Recommendgaojie
She finds that trees maybe disharmony and the Disharmony Value between two trees is associated with two value called FAR and SHORT.
The FAR is defined as the following:If we rank all these trees according to their X Coordinates in ascending order.The tree with smallest X Coordinate is ranked 1th.The trees with the same X Coordinates are ranked the same. For example,if there are 5 tree with X Coordinates 3,3,1,3,4. Then their ranks may be 2,2,1,2,5. The FAR of two trees with X Coordinate ranks D1 and D2 is defined as F = abs(D1-D2).
The SHORT is defined similar to the FAR. If we rank all these trees according to their heights in ascending order,the tree with shortest height is ranked 1th.The trees with the same heights are ranked the same. For example, if there are 5 tree with heights 4,1,9,7,4. Then their ranks may be 2,1,5,4,2. The SHORT of two trees with height ranks H1 and H2 is defined as S=min(H1,H2).
Two tree’s Disharmony Value is defined as F*S. So from the definition above we can see that, if two trees’s FAR is larger , the Disharmony Value is bigger. And the Disharmony value is also associated with the shorter one of the two trees.
Now give you every tree’s X Coordinate and their height , Please tell Sophia the sum of every two trees’s Disharmony value among all trees.
For each test case, the first line contain one integer N (2 <= N <= 100,000) N represents the number of trees.
Then following N lines, each line contain two integers : X, H (0 < X,H <=1,000,000,000 ), indicating the tree is located in Coordinates X and its height is H.
210 10020 200410 10050 50020 20020 100
113
0 0
- hdoj 3015 Disharmony Trees(树状数组)
- HDU 3015 Disharmony Trees 树状数组
- HDU 3015 Disharmony Trees (树状数组)
- hdu 3015 Disharmony Trees(树状数组)
- hdu 3015 Disharmony Trees(树状数组)
- HDU 3015 Disharmony Trees(树状数组)
- 树状数组+公式推导 hdu 3015 Disharmony Trees
- hdu 3015 Disharmony Trees(树状数组+离散)
- hdu3015 Disharmony Trees(树状数组)
- 【TOJ 3401.】Disharmony Trees【树状数组】
- poj1990 MooFest && hdu3015 Disharmony Trees (树状数组)
- hdu3015 Disharmony Trees(树状数组+排序)
- HDU-3015 Disharmony Trees (离散+树状数组)(2009 Multi-University Training Contest 12)
- HDU 5876 Disharmony Trees(离散化 树状数组)
- hdu 3015 Disharmony Trees
- hdu 3015 Disharmony Trees
- HDU 3015 Disharmony Trees
- HDU - 3015 Disharmony Trees
- 读写csv、xml、mat、json
- Windows电脑重装系统亲身经历
- 字符串倒置 in C
- 记录一次core文件调试
- myeclise svn 同步报错
- hdoj 3015 Disharmony Trees(树状数组)
- 【洛谷 1363】 幻象迷宫
- 拓展方法
- leetcode 160. Intersection of Two Linked Lists
- HTTP协议
- Springboot 404 500错误页面处理
- printf的宏定义版本及多重使用方法
- question:setValue:forUndefinedKey: this class is not key value coding-compliant for the key
- java缓冲流,数据流和对象流