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个数,然后问的是区间内的连续数的个数(一个都不连续,就是和自己连续,也算做一组)。
例如,我们有5个数,是{3,1,2,5,4};查询[2,4]中的连续数的个数,就是{1, 2}、{5}一共有两个,问题就是,我们该怎么去转换这个思想?
最早的时候,我想到了是以前求区间互质数的个数,当时按照一定的顺序排序,然后先预处理质因子的每个下一位的出现的位置,做更新,但是这道题有点不实用,因为加入这个数是"4",遇上"3"、"5"就得去进行处理,但是"3"、"5"的位置之后,可能会使得"2"、"6"等等也产生了连续关系……这就不好处理了。
然后,想个办法,如果他的左右出现在查询区间之内的话,是不是就可以递推转移了?试试看查询的左区间升序,如果递推下去的数它的左右元素值是出现过的就给予对应位置上附上+1,然后就这样不断的判断连续…… 那么,右端点的不稳定性就不好处理了,不过也可以就这样递推的,这样子的递推,就得满足每次都要到查询的端点的右区间更新,但是之前的连续关系会断掉,就不好处理了……
所以,我换了种方法,把左区间降序,这样子,出现过的节点就可以附上“+1”表示它们之间的关系是存在的,若是左右区间都存在,就都+1,然后查询的每个区间,就直接用区间长度去减去关系,就可以了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include