码迷,mamicode.com
首页 > 其他好文 > 详细

Gym 100418B : Sum of sequences

时间:2015-08-08 01:16:11      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

Description

You finished RIUSB ACBJSO university few years ago and started working hard and growing your carrier. At the some moment you tried to pass an interview at the XEDNAY company. You successfully answered all tricky questions about advanced algorithms and data structures and got the last one. Given two sequences A, B you need to find the following sum: 技术分享

Input

Input contains three lines. First contains two numbers lengths of sequences |A|, |B|. Second and third line contains |A| and |B| numbers separated by spaces (1 ≤ |A|, |B| ≤ 1051 ≤ Ai, Bi ≤ 104).

Output

Single line containing answer to the task.

Sample Input

5 4
3 4 5 4 4
1 2 3 4

Sample Output

42



题目大意:
大意就是给你两个序列A,B
让你计算
技术分享



解题报告:
技术分享


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1)

using namespace std;
const int maxn = 1e5 + 500;
typedef struct data
{
  long long val;
  long long idxval;
  int idx;
  friend bool operator < (const data & x,const data & y)
  {
      return x.val < y.val;
  }
};
int LA,LB;
data A[maxn],B[maxn];
long long SB1,SB2,SB3;
long long QQ1,QQ2,QQ3;



int main(int argc,char *argv[])
{
  scanf("%d%d",&LA,&LB);
  for(int i = 0 ; i < LA; ++ i)
  {
      int x;
      scanf("%d",&x);
      A[i].val = x , A[i].idx = i , A[i].idxval = 1LL * i * x;
  }
  SB1 = SB2 = SB3 = QQ1 = QQ2 = QQ3 = 0; // HA HA HA HA 
  for(int i = 0 ; i < LB ; ++ i)
  {
      int x;
      scanf("%d",&x);
      B[i].val = x , B[i].idx = i , B[i].idxval = 1LL * i * x;
  }
  sort(A,A+LA);sort(B,B+LB);
  for(int i = 0 ; i < LB ; ++ i) QQ1 += B[i].idx , QQ2 += B[i].val , QQ3 += B[i].idxval;
  int ptr = 0;
  long long ans = 0;
  for(int i = 0 ; i < LA ; ++ i)
  {
        while(ptr < LB && B[ptr].val <= A[i].val) 
            {
                SB1 += B[ptr].idx;
                SB2 += B[ptr].val;
                SB3 += B[ptr].idxval;
                QQ1 -= B[ptr].idx;
                QQ2 -= B[ptr].val;
                QQ3 -= B[ptr].idxval;
                ptr++;
            }
        ans += 1LL*(A[i].idxval * ptr - A[i].val * SB1  - A[i].idx * SB2  + SB3);
        ans += 1LL*(A[i].idx * QQ2 - QQ3 - A[i].idxval * (LB-ptr) + A[i].val * QQ1);
  }
  printf("%I64d\n",ans);
  return 0;
}

 

Gym 100418B : Sum of sequences

标签:

原文地址:http://www.cnblogs.com/Xiper/p/4712315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!