HDU 5721 Palace 求最近点对 + 对答案的贡献


Palace

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Problem Description

The last trial Venus imposes on Psyche is a quest to the underworld. She is to take a box and obtain in it a dose of the beauty of Prosperina, queen of the underworld.
There are $ n $ palaces in the underworld, which can be located on a 2-Dimension plane with $ (x, y) $ coordinates (where $ x, y $ are integers). Psyche would like to find the distance of the closest pair of two palaces. It is the password to enter the main palace.
However, the underworld is mysterious and changes all the time. At different times, exactly one of the $ n $ palaces disappears.
Psyche wonders what the distance of the closest pair of two palaces is after some palace has disappeared.
Print the sum of the distance after every single palace has disappeared.
To avoid floating point error, define the distance $ d $ between palace $ (x_1, y_1) $ and $ (x_2, y_2) $ as $ d = (x_1 - x_2) ^ 2 + (y_1 - y_2) ^ 2 $.

Input

The first line of the input contains an integer $ T $ $ (1 \le T \le 5) $, which denotes the number of testcases.
For each testcase, the first line contains an integers $ n $ $ (3 \le n \le 10 ^ 5) $, which denotes the number of temples in this testcase.
The following $ n $ lines contains $ n $ pairs of integers, the $ i $-th pair $ (x, y) $ $ (-10 ^ 5 \le x,y \le 10 ^ 5) $ denotes the position of the $ i $-th palace.

Output

For each testcase, print an integer which denotes the sum of the distance after every single palace has disappeared.

Sample Input

1
3
0 0
1 1
2 2

Sample Output

12

Hint

If palace $ (0,0) $ disappears,$ d = (1-2) ^ 2 + (1 - 2) ^ 2 = 2 $; If palace $ (1,1) $ disappears,$ d = (0-2) ^ 2 + (0 - 2) ^ 2 = 8 $; If palace $ (2,2) $ disappears,$ d = (0-1) ^ 2 + (0-1) ^ 2 = 2 $; Thus the answer is $ 2 + 8 + 2 = 12 $。

Source

 

题意

给出n个点的坐标,假设第i个点消失后,最近点对的距离的平方为d,求d的和。

思路

会发现,这n个点的最近点对的距离对答案的贡献系数为(n-2),因为除了删除这两个最近点对,其它点的删除都不会影响最近点对的距离。后面分别删除这两个点,再求出最近点对的距离求和即可。求最近点对可以用KD-tree或者CDQ分治。
 
KD-tree

 

CDQ分治

 

 

 
 

HDU 5730 Shell Necklace FFT+cdq分治优化DP

http://acm.hdu.edu.cn/showproblem.php?pid=5730

Shell Necklace

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell necklace with n beautiful shells contains the most sincere feeling for my best lover Arrietty, but even that is not enough.
Suppose the shell necklace is a sequence of shells (not a chain end to end). Considering i continuous shells in the shell necklace, I know that there exist different schemes to decorate the i shells together with one declaration of love.
I want to decorate all the shells with some declarations of love and decorate each shell just one time. As a problem, I want to know the total number of schemes.
Input
There are multiple test cases(no more than 20 cases and no more than 1 in extreme case), ended by 0.
For each test cases, the first line contains an integer n , meaning the number of shells in this shell necklace, where 1n105 . Following line is a sequence with n non-negative integer a1,a2,,an, and ai107 meaning the number of schemes to decorate i continuous shells together with a declaration of love.
 Output
For each test case, print one line containing the total number of schemes module 313 (Three hundred and thirteen implies the march 13th, a special and purposeful day).
 Sample Input
3
1 3 7
4
2 2 2 2
0
 Sample Output

14 54

Hint
 

For the first test case in Sample Input, the Figure 1 provides all schemes about it. The total number of schemes is 1 + 3 + 3 + 7 = 14.

 

 Author
HIT
 Source
 题意:装饰一条链上的n个珠子,装饰任意连续i个珠子的方案数是a[i],每个珠子只能被装饰一次,求装饰这n个珠子的方案数的总数。
思路:很容易得出,装饰前i个珠子的方案数为dp[i]=sigma(dp[j]*a[i-j])  (0<=j<i)。很显然是一个卷积,但是dp同时出现在等号前后,需要用CDQ分治加上FFT优化。对于一个区间[l,r],我们先求出[l,mid]的答案,在求出[l,mid[对[mid+1,r]的影响。对于l端点,如果要影响到r端点,就需要卷积a[i]的长度r-mid+1。用dp[l~id]和a[1~r-mid+1]进行卷积(FFT)后取出对应位置加到dp上即可。