zoukankan      html  css  js  c++  java
  • bzoj3378[Usaco2004 Open]MooFest 狂欢节*

    bzoj3378[Usaco2004 Open]MooFest 狂欢节

    题意:

    n只奶牛,第i只听力为vi,坐标为xi,两只奶牛聊天时音量是max(vi,vj)*abs(xi-xj)。求n(n-1)/2对奶牛的音量和。n≤20000。

    题解:

    首先所有奶牛按x排序,记录其位置,接着再按它们音量升序排序依次插入树状数组。维护两个树状数组,一个用来求位置比某奶牛大的坐标和和奶牛数,另一个用来求位置比某奶牛小的坐标和和奶牛数。对于每个插入的奶牛i,对答案的贡献是vi*位置比它大的坐标和与奶牛数*该奶牛的坐标的差,加上vi*位置比它小的坐标和与奶牛数*该奶牛的坐标的差的相反数。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <queue>
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 #define maxn 20010
     7 #define ll long long
     8 #define lb(x) x&-x
     9 using namespace std;
    10 
    11 inline int read(){
    12     char ch=getchar(); int f=1,x=0;
    13     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    14     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    15     return f*x;
    16 }
    17 struct nd{int d,sz;}nds1[maxn],nds2[maxn]; int n; ll ans;
    18 struct abc{int v,x,id;}abcd[maxn];
    19 bool cmp1(abc a,abc b){return a.x<b.x;} bool cmp2(abc a,abc b){return a.v<b.v;}
    20 void update1(int x,int y){while(x<=n)nds1[x].d+=y,nds1[x].sz++,x+=lb(x);}
    21 void update2(int x,int y){while(x)nds2[x].d+=y,nds2[x].sz++,x-=lb(x);}
    22 nd query1(int x){nd q=(nd){0,0}; while(x>=1)q.d+=nds1[x].d,q.sz+=nds1[x].sz,x-=lb(x); return q;}
    23 nd query2(int x){nd q=(nd){0,0}; while(x<=n)q.d+=nds2[x].d,q.sz+=nds2[x].sz,x+=lb(x); return q;}
    24 int main(){
    25     n=read(); inc(i,1,n)abcd[i].v=read(),abcd[i].x=read(); sort(abcd+1,abcd+n+1,cmp1);
    26     inc(i,1,n)abcd[i].id=i; sort(abcd+1,abcd+n+1,cmp2);
    27     inc(i,1,n){
    28         nd a=query1(abcd[i].id-1); ans+=(ll)abcd[i].v*(abcd[i].x*a.sz-a.d);
    29         a=query2(abcd[i].id+1); ans+=(ll)abcd[i].v*(a.d-abcd[i].x*a.sz);
    30         update1(abcd[i].id,abcd[i].x); update2(abcd[i].id,abcd[i].x);
    31     }
    32     printf("%lld",ans); return 0;
    33 }

    20161116

  • 相关阅读:
    自考新教材-p145_5
    自考新教材-p144_4
    自考新教材-p144_3
    自考新教材-p143_2
    自考新教材-p142_3(1)
    【SQL server】安装和配置
    【,net】发布网站问题
    【LR】关于宽带与比特之间的关系
    【LR】录制测试脚本中的基本菜单
    【LR】安装LR11后遇到的问题
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/6072309.html
Copyright © 2011-2022 走看看