POJ 3928 Ping Pong (tree-like array) _poj

Source: Internet
Author: User

Each person has a unique ranking (number size) with a unique position (from the back of the line), to meet the rank between the two and the position is also between the number of ternary groups

Train of thought: enumerating which numbers in between can only be used in O (n^3) time is too high, but can change the mind. We can convert to each number a, and find each number B that is larger than the current number a. After the number B is larger than the current number of B C, and then for each a corresponding to the number of C in each B of the sum is half the result, of course, compared to the other half of the method is similar. In fact, the tree-like array for the first time to find each B, and then use B to find C, so that two times is good

#include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <vector> #include <string> #include <cstdio> #include <cstring> #include <stdlib.h> #
Include<iostream> #include <algorithm> using namespace std; #define EPS 1E-8/* Note that there may be output -0.000*/#define SGN (x) (x<-eps? -1:x<eps? 0:1)//x to a comparison of two floating-point numbers, note that the integer #define CVS (x) (x &G T 0.0? X+EPS:X-EPS)//floating-point number conversion #define ZERO (x) ((x) >0? (
x):-(x) <eps)//Determine whether equals 0 #define MUL (a,b) (a<<b) #define DIR (a,b) (A&GT;&GT;B) typedef long Long LL;
typedef unsigned long long ull;
const INT inf=1<<28;
Const double Pi=acos (-1.0);
const int mod=1e9+7;
const int max=20010; struct node {int val,pos;}
Num[max];
int n;
ll Bit[max];
        int lowbit (int x) {return x& (-X);} void Add (int x,int y) {while (x<=n) {bit[x]+= (ll) y;
    X+=lowbit (x);
} return;
    ll Sum (int x) {ll sum=0ll; while (x>0) {sUM+=BIT[X];
    X-=lowbit (x);
return sum;
    ll Manx (int n,int *pop,int *ivsn,int *ivsn2,int hh) {ll sum=0ll,manx=0ll;
    memset (bit,0ll,sizeof (bit));
        for (int i=n;i>0;--i) {if (HH) ivsn[i]=sum-sum (Pop[i]);
        else Ivsn[i]=sum (Pop[i]);
        ADD (Pop[i],ivsn2[i]);
        Sum+=ivsn2[i];
    Manx+=ivsn[i];
return Manx;
int Pop[max],ivsn[max],ivsn2[max];
    BOOL CMP (struct node p1,struct node p2) {return p1.val<p2.val;} int main () {int t;
    ll ans;
    scanf ("%d", &t);
        while (t--) {ans=0ll;
        scanf ("%d", &n);
            for (int i=1;i<=n;++i) {scanf ("%d", &num[i].val);
            Num[i].pos=i;
        Ivsn2[i]=1;
        Sort (num+1,num+n+1,cmp); for (int i=1;i<=n;i++) pop[num[i].pos]=i;//discretization Manx (n,pop,ivsn,ivsn2,1);//Find the IVSN of each of the pop arrays after each number larger than the current one
        The sum of 2 array values Ans+=manx (n,pop,ivsn2,ivsn,1); for (int i=1;i<=n;i++) Ivsn2[i]=1;
        Manx (n,pop,ivsn,ivsn2,0)//Find the sum of the IVSN2 array values for each number of pop arrays later than the current small one Ans+=manx (n,pop,ivsn2,ivsn,0);
    printf ("%i64d\n", ans);
return 0;
 }

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.