HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)

HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)

Description

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?
 

Input

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). 
 

Output

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

题目大意:有一个互不相等的序列,问有多少个ai、aj、ak满足i<j<k并且ai<aj<ak或者ai>aj>ak。

思路:考虑i<j<k并且ai<aj<ak的,先找出所有的i<j并且ai<aj,令sum[j]为满足i<j并且ai<aj的对数。然后就是枚举k,找出所有比k小的j并把sum[j]加起来。这种合法三元组最多C(2W,3)还是蛮大的,要用64位整数。反面一样。用树状数组就可以解决了。详细看代码吧这个不大会讲。

思路2:其实有个更简单的办法,只是我不久前受到了树状数组求逆序对的影响(已经过了很久了好不好!)。就是对每一个数x,找出左边比它小的数一共x个,右边比它大的数一共y个,那么中间为x的数的逆序对就有xy个,反面一样。这个没有代码我懒得写,DISCUSS有自己去看……

PS:之前做过一条codeforces的61E,也是差不多的题目,不过那题要离散化,这题大概不用也能过(我顺手离散化了……)。我记忆力渣渣以为上次用的是32位这次也用了int结果WA了,原来上次用的也是64位……以后还是算一下别相信记忆力了……

PS2:样例好坑爹啊输出1,害我要自己设计数据验证数得好辛苦啊……

代码(281MS):

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 
 8 const int MAXN = 20010;
 9 
10 struct Node {
11     int val, id;
12     bool operator < (const Node &rhs) const {
13         return val < rhs.val;
14     }
15 };
16 
17 Node a[MAXN];
18 LL tree[MAXN], sum[MAXN];
19 int f[MAXN];
20 int n;
21 
22 inline int lowbit(int x) {
23     return x & -x;
24 }
25 
26 inline LL get_sum(int k) {
27     LL ret = 0;
28     while(k > 0) {
29         ret += tree[k];
30         k -= lowbit(k);
31     }
32     return ret;
33 }
34 
35 inline void modify(int k, LL x) {
36     while(k <= n) {
37         tree[k] += x;
38         k += lowbit(k);
39     }
40 }
41 
42 LL solve() {
43     memset(tree, 0, sizeof(tree));
44     LL ret = 0;
45     for(int i = 1; i <= n; ++i) {
46         sum[i] = get_sum(f[i]);
47         modify(f[i], 1);
48     }
49     memset(tree, 0, sizeof(tree));
50     for(int i = 1; i <= n; ++i) {
51         ret += get_sum(f[i]);
52         modify(f[i], sum[i]);
53     }
54     memset(tree, 0, sizeof(tree));
55     for(int i = n; i > 0; --i) {
56         sum[i] = get_sum(f[i]);
57         modify(f[i], 1);
58     }
59     memset(tree, 0, sizeof(tree));
60     for(int i = n; i > 0; --i) {
61         ret += get_sum(f[i]);
62         modify(f[i], sum[i]);
63     }
64     return ret;
65 }
66 
67 int main() {
68     int T;
69     scanf("%d", &T);
70     while(T--) {
71         scanf("%d", &n);
72         for(int i = 1; i <= n; ++i) scanf("%d", &a[i].val), a[i].id = i;
73         sort(a + 1, a + n + 1);
74         for(int i = 1; i <= n; ++i) f[a[i].id] = i;
75         //printf("%d
", solve());
76         cout<<solve()<<endl;
77     }
78 }
View Code