POJ 1990 Moofest "tree-like array"

Source: Internet
Author: User

Test instructions: give n cow, each cow has a hearing v, coordinate x, the energy between two cows for Max (V1,V2) *dist (V1,V2), the total energy value

Once each cow is sorted by the V, the order will find that the coordinates are smaller than the current x, and the coordinates are larger than the current x.

Assuming that the coordinates are smaller than x, there are num

So

The sum of distances = X*num-the sum of the preceding coordinates and +-(n-num-1) * x

And because

The and of the subsequent coordinates = the and of the entire interval coordinates-the and of the preceding coordinates

So with two arrays, one array to count, the other an array to count and

The study of this--http://www.acmtime.com/?p=403

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A  -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=20005; -  -LL A[MAXN];//This is for a number smaller than x . +LL B[MAXN];//This is used to ask for the front and - LL C[MAXN]; + intN; A  at structnode{ -     intx,v; - }Q[MAXN]; -  - intCMP (node N1,node n2) { -     returnN1.V <n2.v; in } -  to intLowbit (intx) {returnX & (-x);} +  -ll sum (ll c[],intx) { theLL ret=0; *      while(X >0){ $RET + = c[x]; x-=lowbit (x);Panax Notoginseng     } -     returnret; the }  +  A voidAdd (LL c[],intXintd) { the      while(X <MAXN) { +c[x]+=d;x+=lowbit (x); -     } $ } $  - intMain () { -scanf"%d",&n); the      for(intI=1; i<=n;i++) scanf ("%d%d",&q[i].v,&q[i].x); -Sort (q+1, q+n+1, CMP);Wuyi      theMemset (A,0,sizeof(a)); -memset (b,0,sizeof(b)); WuLL cnt =0; -      for(intI=1; i<=n;i++){ About         intx = SUM (a,q[i].x);//How many of the statistical coordinates are smaller than x? $LL alltotal = SUM (B,MAXN);//to count all the coordinates of this interval and -LL total = SUM (b,q[i].x);//statistic The front coordinate is smaller than x and -CNT + = Q[I].V * (x * q[i].x-total + alltotal-total-(I-x-1) *q[i].x); -Add (a,q[i].x,1); A Add (b,q[i].x,q[i].x); +     }  theprintf"%i64d\n", CNT); -     return 0; $}
View Code

Come on-------------

Goooooooooooooo---------------

POJ 1990 Moofest "tree-like array"

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.