[TYVJ] Lou Lan totem (tree-like array)

Source: Internet
Author: User

[TYVJ] Lou Lan Totem

Topic Link: http://www.joyoi.cn/problem/tyvj-1432

Topic description

After completing the assignment, the West 314来 to the west of the old town of Lou. It is said that a long time ago this piece of land (than the old Town of Lou) lived two tribes, a tribal worship knife (' V '), a tribal Worship spade (' ∧ '), they respectively use the shape of V and ∧ to represent their tribal totem.
West 314 in the city under the old Lou, found a huge mural, the murals were marked with n points, measured by the measurement of the n points of the horizontal position and vertical position is 22 different. West 314 think the mural contains information about the relative position of these n points, so you may wish to set coordinates (1,Y1), (2,y2),..., (N,yn), where Y1~yn is an arrangement of 1 to N.
West 314 is going to study the number of totems in the mural, where the V totem is defined as follows (note: The totem form is only related to the relative size of the three ordinate) 1<=i<j<k<=n and yi>yj,yj<yk;

And the totem of the tribe worshipping ∧ is defined as 1<=i<j<k<=n and yi<yj,yj>yk;

West 314 wants to know the number of two tribal totems in these n points. Therefore, you need to write a program to find out the number of V and the number of ∧. Input format

First line one number n
The second row is the number of N, representing the Y1,y2......yn output format, respectively.

Two numbers
The middle is separated by a space
The number of V and the number of ∧ in sequence is explained by sample example

Data range
10% of Data n<=600
40% of Data n<=5000
100% of the data n<=200000, the answer is no more than the Int64 West 314\admin TYVJ the first month of the fourth race sample data Input sample #1 output sample #1

5
1 5 3 2 4

3 4

The puzzle: For each point to find the front than his larger number and later than his number, the product is the number of V, the same as before, the number of smaller than his product is the number of ^, then the tree-like array can be quickly obtained, and finally the product can be.

AC Code:

#include <iostream> #include <cstdio> #include <algorithm> using namespace std;
const int MAXN = 200006; #define _FOR (I,A,B) for (int i=a;i<=b;i++) #define NOFOR (I,A,B) for (int i=a;i>=b;i--) int N,a[maxn],c[maxn],left_
NUM[MAXN],RIGHT_NUM[MAXN];
        int lowbit (int x) {return x=x& (-X);} void Insert (int x,int y) {while (y<=n) {c[y]+=x;
    Y+=lowbit (y);
    } int getsum (int x) {int sum = 0;
        while (x>0) {sum+=c[x];
    X-=lowbit (x);
return sum;
    int main (int argc, char const *argv[]) {scanf ("%d", &n);
    _for (i,1,n) cin>>a[i];
        Nofor (i,n,1) {int k = getsum (A[i]);
        Insert (1,a[i]);
    Right_num[i]=k;
    } _for (I,1,n) c[i]=0;
        _for (i,1,n) {int k = getsum (A[i]);
        Insert (1,a[i]);
    Left_num[i]=k;
    Long long ans1 = 0;
    _for (i,1,n) {ans1+=left_num[i]*right_num[i];
    Long long ans2 = 0; _for (i,1, n) {ans2+= (I-1-left_num[i]) * (N-i-right_num[i]);
    } cout<<ans2<< "" <<ans1<<endl;
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.