UVA 1428-ping Pong (tree-like array)

Source: Internet
Author: User

UVA 1428-ping Pong

Topic links

Test instructions: Given some people, from left to right, each person has a skill value, now to hold the game, must meet the position from left to right 3 people. and the skill value from small to large or from big to small, ask a few forms of organization

Idea: Use a tree-like array to process the number of the left side of each position smaller than it and the number of small on the right. Then the left and right large can also be calculated, then the game played for the left small * right big + left big * right small.

Code:

#include <cstdio> #include <cstring>const int N = 100005;int T, N, A[n];long long bit[n], lx[n], rx[n];inline i NT lowbit (int x) {    return x& (-X);} void Add (int x, Long long Val) {while    (x < N) {Bit[x] + = val;x + lowbit (x);    }} Long long Query (int x) {    long long ans = 0;    while (x > 0) {ans + = bit[x];x-= lowbit (x);    }    return ans;} int main () {    scanf ("%d", &t);    while (t--) {scanf ("%d", &n), memset (bit, 0, sizeof (bit)), for (int i = 0; i < n; i++) {    scanf ("%d", &a[i]); C10/>lx[i] = Query (A[i]);    Add (A[i], 1);} memset (bit, 0, sizeof (bit)), for (int i = n-1; I >= 0; i--) {    Rx[i] = Query (A[i]);    Add (A[i], 1);} Long Long ans = 0;for (int i = 0; i < n; i++)    ans + lx[i] * (N-i-1-rx[i]) + (I-lx[i]) * Rx[i];p rintf ("%ll D\n ", ans);    }    return 0;}


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

UVA 1428-ping Pong (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.