Codeforces 903D Almost Difference

Posted xuzihanllaa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 903D Almost Difference相关的知识,希望对你有一定的参考价值。

Codeforces 903D Almost Difference

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let‘s denote a function

技术分享图片

You are given an array a consisting of n integers. You have to calculate the sum of d(ai,?aj) over all pairs (i,?j) such that 1?≤?i?≤?j?≤?n.

Input

The first line contains one integer n (1?≤?n?≤?200000) — the number of elements in a.

The second line contains n integers a1, a2, ..., an (1?≤?ai?≤?109) — elements of the array.

Output

Print one integer — the sum of d(ai,?aj) over all pairs (i,?j) such that 1?≤?i?≤?j?≤?n.

Examples
Input
5
1 2 3 1 3
Output
4
Input
4
6 6 5 5
Output
0
Input
4
6 6 4 4
Output
-8
Note

In the first example:

  1. d(a1,?a2)?=?0;
  2. d(a1,?a3)?=?2;
  3. d(a1,?a4)?=?0;
  4. d(a1,?a5)?=?2;
  5. d(a2,?a3)?=?0;
  6. d(a2,?a4)?=?0;
  7. d(a2,?a5)?=?0;
  8. d(a3,?a4)?=??-?2;
  9. d(a3,?a5)?=?0;
  10. d(a4,?a5)?=?2.

 

分析:刚拿到这题,先想到的是:归并排序,仔细构思了一下感觉细节很多,不大好写;转而去想线段树(归并排序能解决的线段树都行),绝对值差在1及以内的直接不考虑就行了,那么对于一个值x,要考虑的区间范围是[1,x-2]U[x+2,+inf],这样先离散化,然后线段树维护离散化后的下标,维护的信息有两个:区间和与区间计数(sum和cnt),因为对于x的查询结果sum和cnt,ans=cnt*x-sum,然后就是基本的单点更新,区间查询了。这道题要用高精度,我偷懒没写。主要原因是这道题其实用不着线段树,只要set或者map维护一下集合,然后维护前缀和即可。。这样写主程序代码长度不超过50行,比我100行的线段树代码清爽多了。

 

代码




以上是关于Codeforces 903D Almost Difference的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 915D Almost Acyclic Graph

C. Almost Equal ( Codeforces Round #580 (Div. 2) )

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

D - Almost Identity Permutations /* Codeforces Round 888 D */

Codeforces 888D Almost Identity Permutations:错排公式

Codeforces 915D Almost Acyclic Graph