zoukankan      html  css  js  c++  java
  • Disharmony Trees 树状数组

    Disharmony Trees
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    One 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.
     

    Input

    There 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.
     

    Output

    For 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 Input

    2
    10 100
    20 200
    4
    10 100
    50 500
    20 200
    20 100
     

    Sample Output

    1
    13
     
     1 #include <iostream>
     2 #include <string.h>
     3 #include <stdio.h>
     4 #include <algorithm>
     5 using namespace std;
     6 #define ll long long
     7 typedef struct abcd
     8 {
     9     ll x,f,i;
    10 } abcd;
    11 abcd a[110000];
    12 bool cmp1(abcd x,abcd y)
    13 {
    14     return x.x<y.x;
    15 }
    16 bool cmp2(abcd x,abcd y)
    17 {
    18     return x.f<y.f;
    19 }
    20 typedef struct abc
    21 {
    22     ll sum,ci;
    23 }abc;
    24 abc aa[110000];
    25 ll need,need1,n;
    26 ll lowbit(ll x)
    27 {
    28     return x&(-x);
    29 }
    30 void update(ll x)
    31 {
    32     ll y=x;
    33     while(x<=n)
    34     {
    35         aa[x].ci++;
    36         aa[x].sum+=y;
    37         x+=lowbit(x);
    38     }
    39 }
    40 ll fun(ll x,ll y)
    41 {
    42     ll  ans,nu=0,sum=0,z=x;
    43     while(x>0)
    44     {
    45         nu+=aa[x].ci;
    46         sum+=aa[x].sum;
    47         x-=lowbit(x);
    48     }
    49     ans=y*((need-sum)-(need1-nu)*z)+y*(nu*z-sum);
    50     return ans;
    51 }
    52 int main()
    53 {
    54     int i,j,now,noww;
    55     ll ans;
    56     while(~scanf("%d",&n))
    57     {
    58         for(i=0; i<n; i++)
    59             scanf("%I64d%I64d",&a[i].x,&a[i].f);
    60         sort(a,a+n,cmp1);
    61         now=a[0].x,a[0].x=1,noww=2;
    62         for(i=1; i<n; i++)
    63         {
    64             if(a[i].x==now)a[i].x=a[i-1].x;
    65             else now=a[i].x,a[i].x=noww;
    66             noww++;
    67         }
    68         sort(a,a+n,cmp2);
    69         now=a[0].f,a[0].f=1,noww=2;
    70         for(i=1; i<n; i++)
    71         {
    72             if(a[i].f==now)a[i].f=a[i-1].f;
    73             else now=a[i].f,a[i].f=noww;
    74             noww++;
    75         }
    76         need=0,need1=0;
    77         ans=0;
    78         memset(aa,0,sizeof(aa));
    79         for(i=n-1;i>=0;i--)
    80         {
    81             ans+=fun(a[i].x,a[i].f);
    82             update(a[i].x);
    83             need+=a[i].x;
    84             need1++;
    85         }
    86         cout<<ans<<endl;
    87     }
    88 }
    View Code
     
  • 相关阅读:
    ESP32 SDA和SCL
    ESP32的HSPI和VSPI区别
    ffmpeg生成视频封面图
    小程序读取几种不同格式json数据(小程序json解析)
    ajax
    使用Java语言,连接linux服务器,并远程执行shell 脚本
    Echarts饼图的使用
    js提取对象数组中的某一个属性
    java读取文件推送报文
    java读取本地文件内容TXT文件
  • 原文地址:https://www.cnblogs.com/ERKE/p/3842804.html
Copyright © 2011-2022 走看看