主题

主题

 
   

Group

原题:http://acm.hdu.edu.cn/showproblem.php?pid=4638

Problem Description

There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group can create K*K value. The value of an interval is sum of these value of groups. The people of same group's id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.

Input

First line is T indicate the case number.

For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.

Then a line have n number indicate the ID of men from left to right.

Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].

Output

For every query output a number indicate there should be how many group so that the sum of value is max.

Sample Input

1

5 2

3 1 2 5 4

1 5

2 4

Sample Output

1

2


题目大意:

给出一个长度为n的排列。给出m次询问,每一次询问一段区间最大的“价值”。

区间的“价值”的定义:把区间分为若干“组”,区间的“价值”为所有“组”的“价值”的平方的和。

“组”的定义:一段区间,且区间里面的数字,排序之后,是一段连续的。例如“4 2 1 3”就可以视为一个组。

“组”的“价值”的定义:组所代表的区间的长度。


个人解法:

首先发现一个小规律,x^2+y^2<(x+y)^2。也就是说,如果可以合并,我们就尽量合并。

一眼过去感觉可以莫队,维护桶。

之后变为查询桶有多少个连续的区间。

可以用线段树来维护。但是复杂度变为O(msqrt(m)logn)。不够理想。

发现我们线段树只需要维护区间左端点与右端点是否有数字,以及这个节点的连续区间的个数。分块同样可以维护。

于是复杂度降为O(m(sqrt(m)+sqrt(n)))。

懂得一点点套路,比网上一些做法还是显然了许多。


稍微卡了一下常数。10个测试点从16s变为了8s。这题实现也还是很紧的来着……

代码如下:

https://code.csdn.net/snippets/2300193


突然回想起昨天晚上和某点聊到搜索。记得一份课件曾经说过,“搜索,万能的解题金钥匙。”我说我不以为然,“你拿搜索做搜索题,就说搜索是万能的解题金钥匙,那我拿贪心做贪心题,是不是贪心也是万能的金钥匙啊?”

我还是觉得分块莫队很厉害。高明在于他不止可以解决考察分块与考察莫队的题。这道题正解是DP+树状数组/线段树。然而不是很懂这道题DP的理论。

于是,分块和莫队巧妙解决问题。让暴力也变得有了一些艺术,

 
 
评论