Luo gu 2657 bow a clan

Source: Internet
Author: User

Luo gu 2657 bow a clan

Address: http://www.luogu.org/problem/show?pid=2657

Title Description

A group of young people lined up to chat with each other on their mobile phones.
Each person's cell phone has a signal receiving indicator, and the first person's receive indicator is set to V[i].
If the person in X[i] is to chat with the person who is in XJ, then the two-member signal emission intensity is abs (X[I]-X[J]) *max (V[i],v[j]).
Now we want to know the sum of the intensity of the signals emitted by these people in all pairs.

Input/output format

Input format:

The first line is an integer n, followed by n lines, two integers per line v[i] and X[i].

Output format:

The total signal emission intensity of all pairs.

Input/Output sample

Input Sample # #:

4

3 1

2 5

2 6

4 3

Sample # # of output:

57

Description

For 40% of data, n<=5,000
For 100% of data, n<=100,0001≤x[i]≤20,000
Note: There may be two people in the same position
The answer is within the range of int64 or long long

Ideas

A tree-like array.

First sort the people in order of V from large to small, which is equivalent to eliminating the impact of V, the person previously considered v must be less than or equal to the current v.

Second consider the ABS (X[i]-x[j]) part, just beginning to think is the prefix and, but note that

ABS (X-X1) +abs (x-x2)!=abs (2*x-(X1+X2))

Here, NUML represents the number of previous x smaller than the current X, SUML represents the sum of x, a similar definition SUMR,NUMR the previous x is smaller than the current x. The current

ans=v* (x*numl-suml+sumr-numr*x)

The numl suml here can be obtained by bit in the time of O (Logn). The total time complexity is O (NLOGN).

It is important to note that starting with X or starting a query from X-1 is no different from the subject.

Code

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 #definefor (A,B,C) for (int a= (b); a<= (c); a++)6 using namespacestd;7 8typedefLong LongLL;9 Const intMAXN =100000+Ten, maxm=20000+Ten;Ten structnode{ One     intx,v; A     BOOL operator< (ConstNODE&AMP;RHS)Const{ -         returnv<rhs.v; -     } the }NODES[MAXN]; - intN,max; - ////bit - intLowbit (intx) {returnx& (-x); } + voidADD (LL *c,intXintv) { -      while(x<=Max) { +C[x]+=v; x+=lowbit (x); A     } at } -ll query (ll *c,intx) { -LL res=0; -      while(x>0) { -RES+=C[X]; x-=lowbit (x); -     } in     returnRes; - } to LL NUM[MAXM],SUM[MAXM]; +  - ////read the intRead_int () { *     CharC=GetChar (); $      while(!isdigit (c)) c=GetChar ();Panax Notoginseng     intx=0; -      while(IsDigit (c)) { thex=x*Ten+c-'0'; +C=GetChar (); A     } the     returnx; + }  - intMain () $ { $n=read_int (); -for (I,1, N) { -nodes[i].v=read_int (); thenodes[i].x=read_int (); -max=Max (max,nodes[i].x);Wuyi     } theSort (nodes+1, nodes+n+1); -LL ans=0; Wufor (I,1, N) { -         intx=nodes[i].x,v=nodes[i].v; AboutLL Numl=query (num,x-1); $LL Suml=query (sum,x-1); -LL Numr=query (Num,max)-query (num,x); -LL Sumr=query (Sum,max)-query (sum,x); -Ans + = v* (sumr-x*numr+x*numl-suml); AADD (Num,x,1); + Add (sum,x,x); the     } -cout<<ans<<"\ n"; $     return 0; the}

Luo gu 2657 bow a clan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.