记录编号 123547 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 开会的鱼儿 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.254 s
提交时间 2014-09-27 21:43:44 内存使用 4.22 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <set>
#include <list>
#include <iterator>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <functional>
#include <deque>
#define For(i,j,k) for(int i=(j);i<=(k);i++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint;
const int INF=0x7ffffff;
//================struct declaration======================
struct Fishes
{
  LL V,X;
  bool operator <(const Fishes &rhs) const{
    return V<rhs.V;
  }
  friend inline istream &operator >>(istream &is,Fishes &p){
    is>>p.V>>p.X;
    return is;
  }
};
//================var declaration=-========================
const int MAXN=40010;
LL S;LL ans=0;
LL Bittree[MAXN];
int n,C[MAXN];
Fishes fish[MAXN];
//================function declaration====================
int lowbit(int x){return x&-x;}
void update(int k,LL v);
LL query(int x,int &cnt);
//================main code===============================
using namespace std;
int main()
{
  string ProgrammeName="fishb";
  string FloderName="COGS";
  freopen((ProgrammeName+".in").c_str(),"r",stdin);
  freopen((ProgrammeName+".out").c_str(),"w",stdout);
#ifdef DEBUG
  clock_t Start_Time=clock();
  system(("copy C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
#endif
  ios::sync_with_stdio(false);
  cin>>n;
  For(i,1,n)
    cin>>fish[i];
  sort(fish+1,fish+1+n);
  memset(C,0,sizeof(C));
  LL S=0;
  For(i,1,n){
    int Sm=0;
    LL Smaller=query(fish[i].X,Sm);//小于fish[i].x的总和是Smaller,有Sm个
    ans+=(fish[i].X*Sm-Smaller)*fish[i].V;//比他小的贡献答案
    LL Bigger=S-Smaller;//总和是S,比fish[i].x小的是Smaller,比fish[i].x大的总和是S-Smaller,有i-Sm-1个
    ans+=(Bigger-fish[i].X*(i-Sm-1))*fish[i].V;
    S+=fish[i].X;
    update(fish[i].X,fish[i].X); 
  }
  cout<<ans<<endl;
#ifdef DEBUG
  clock_t End_Time=clock();
  printf("\n\nTime Used :%.6lf Ms\n",double(Start_Time-End_Time)/(-CLOCKS_PER_SEC));
#endif
  return 0;
}
//================function code===========================
void update(int k,LL v)
{
   while (k<MAXN){
     Bittree[k]+=v;
     C[k]++;
     k+=lowbit(k);
   }
}
LL query(int x,int &cnt)
{
  LL res=0;
  while (x>0){
    res+=Bittree[x];
    cnt+=C[x];
    x-=lowbit(x);
  }
  return res;
}