POJ 3928 Ping pong(樹狀陣列)
裸的樹狀陣列大白書P197有詳細的解釋,注意每個點找過之後要更新,就行了啊。
Ping pong
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1842 | Accepted: 721 |
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).
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.
Sample Input
1 3 1 2 3
Sample Output
1
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
//#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 100100;
using namespace std;
struct node
{
int lsum1, rsum1;
int lsum2, rsum2;
} f[maxn];
int num[maxn];
int sum[maxn];
int n;
int low_bit(int x)
{
return x&(-x);
}
void add(int x, int d)
{
while(x < maxn)
{
sum[x] += d;
x += low_bit(x);
}
}
int sum1(int x)
{
int ans = 0;
while(x > 0)
{
ans += sum[x];
x -= low_bit(x);
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = 1; i <= n; i++)
scanf("%d",&num[i]);
memset(sum, 0, sizeof(sum));
for(int i = 1; i <= n; i++)
{
int x = sum1(num[i]);
f[i].lsum1 = x;
f[i].rsum1 = i-x-1;
add(num[i], 1);
}
memset(sum, 0, sizeof(sum));
for(int i = n; i >= 1; i--)
{
int x = sum1(num[i]);
f[i].lsum2 = x;
f[i].rsum2 = n-i-x;
add(num[i], 1);
}
LL ans = 0;
for(int i = 1; i <= n; i++)
ans += (LL)(f[i].lsum1*f[i].rsum2+f[i].lsum2*f[i].rsum1);
printf("%I64d\n",ans);
}
return 0;
}
相關文章
- POJ 3928-Ping pong(樹狀陣列+加/乘法原理)陣列
- poj 2481 樹狀陣列陣列
- POJ-2352 Stars(樹狀陣列)陣列
- 【二維樹狀陣列】poj 2155 Matrix陣列
- 二維樹狀陣列-poj2155陣列
- poj 1195 二維樹狀陣列陣列
- POJ 2352-Stars(樹狀陣列-星系)陣列
- POJ 3321-Apple Tree(樹狀陣列)APP陣列
- POJ 2352 Stars(簡單樹狀陣列)陣列
- 二維樹狀陣列--poj1195陣列
- HDU 1541 & POJ 2352 Stars (樹狀陣列)陣列
- POJ 3067-Japan(樹狀陣列-逆序數)陣列
- POJ 1195 Mobile phones(二維樹狀陣列)陣列
- POJ 3321 Apple Tree(dfs+樹狀陣列)APP陣列
- 樹狀陣列陣列
- POJ3321 Apple Tree(DFS序 + 樹狀陣列)APP陣列
- POJ 2299-Ultra-QuickSort(樹狀陣列求逆序數)UI陣列
- 解析樹狀陣列陣列
- POJ3468 A Simple Problem with Integers---樹狀陣列(區間問題)陣列
- POJ 2352(順路講解一下樹狀陣列)陣列
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- 樹狀陣列詳解陣列
- 樹狀陣列基礎陣列
- hdu 3874 樹狀陣列陣列
- 二維樹狀陣列陣列
- 【樹狀陣列 求比其小的個數】poj 2353 Stars陣列
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- 樹狀陣列和逆序對陣列
- hdu 5147 樹狀陣列陣列
- 樹狀陣列快速入門陣列
- POJ 2155-Matrix(二維樹狀陣列-區間修改 單點查詢)陣列
- 樹狀陣列模板+習題集陣列
- 樹狀陣列3種基本操作陣列
- 學習筆記----樹狀陣列筆記陣列
- 樹狀陣列upc1976陣列
- CSU 4441 Necklace (樹狀陣列/LIS)陣列
- 樹狀陣列(我是真小白)陣列
- 線段樹+差分——【模板】樹狀陣列2陣列