Poj3928 ping pong tree Array

Source: Internet
Author: User

There are only N people in the question. Each person has an ID and a skill value. Two contestants and a referee are required for a game, only when both the referee's ID and skill value are between two contestants can a match be held. Now, I want to know how many matches can be organized.

This is a simple tree array statistics problem. First, we need to sort the skill values from small to large, and then maintain the tree array with the ID of each contestant.

Query sum (ID) and sum (N)-sum (ID) before each maintenance of an ID)

For example, we currently maintain the ID: I, then sum (ID) indicates that all the persons whose IDs are less than I and whose capacity value is less than I (after sorting, before I maintenance, all people's capacity values are less than or equal to I). Similarly, sum (N)-sum (ID) is the person whose ID is greater than I but whose capacity value is less than I, record the two values as L and R respectively.

Then when I was a referee, I could hold L * (N-I-r) + R * (i-1-l ), Finding the possibility for everyone to be a referee is the answer to our statistics.

Ping pong
Time limit:1000 ms   Memory limit:65536 K
Total submissions:1352   Accepted:509


N (3 <= n <= 20000) ping pong players live along a West-East Street (consider the street as a line segment ). each player has a unique skill rank. to improve their skill rank, they often compete with each other. if two players want to compete, they must choose a referee
Among other ping pong players and hold the game in the referee's house. for some reason, the contestants can't choose a referee whose skill rank is higher or lower than both of theirs. the contestants have to walk to the referee's house, and because they are
Lazy, they want to make their total walking distance no more than the distance between their houses. of course all players live in different houses and the position of their houses are all different. if the referee or any of the two contestants is different,
We call two games different. Now is the problem: how many different games can be held in this Ping Pong Street?


The first line of the input contains an integer T (1 <= T <= 20), indicating the number of test cases, followed by T lines each of which describes a test case.
Every test case consists of N + 1 integers. the first integer is N, the number of players. then n distinct integers a1, a2... an follow, indicating the skill rank of each player, in the order of west to east. (1 <= AI <= 100000, I = 1... n ).


For each test case, output a single line contains an integer, the total number of different games.

Sample Input

1 3 1 2 3

Sample output


# Include <iostream> # include <cstring> # include <algorithm> # include <string> # include <cstdio> using namespace STD; # define maxn 22000int N; int C [maxn]; int lowbit (INT t) {return T & (-T);} int sum (int I) {int S = 0; while (I> 0) {S + = C [I]; I-= lowbit (I);} return s;} void modify (int I, int X) {While (I <= N) {c [I] + = x; I + = lowbit (I) ;}} struct node {int ID, level;} data [maxn]; long ans; bool CMP (node A, Node B) {return. level <B. level;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); for (INT I = 1; I <= N; I ++) {scanf ("% d", & Data [I]. level); Data [I]. id = I;} Sort (Data + 1, data + 1 + N, CMP); memset (C, 0, sizeof (c); ans = 0; long l, r; For (INT I = 1; I <= N; I ++) {L = sum (data [I]. ID); r = sum (N)-L; ans + = (L * (N-data [I]. ID-R) + R * (data [I]. id-1-l); Modify (data [I]. ID, 1) ;}cout <ans <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.