|
NecklaceTime Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4003 Accepted Submission(s): 1330 Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful value of some interval [x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6. Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them. Input The first line is T(T<=10), representing the number of test cases. For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query. Output For each query, output a line contains an integer number, representing the result of the query. Sample Input 2 6 1 2 3 4 3 5 3 1 2 3 5 2 6 6 1 1 1 2 3 5 3 1 1 2 4 3 5 Sample Output 3 7 14 1 3 6 对于这道题。大家能够看我博客里提供的离线处理教程后,基本能够理解离线处理的机制了,然后我们就要对比理解题目。不能出现反复的数字,所以能够离线最右边的值,将前面的值一一删除就能够得到正确答案,当然,大家还需注意的是数值取值范围为long long ,我就是在这个上面看了接近一个小时,query()函数的返回值应该也是long long 。坑爹的地方就是这里了,其它的,大家看代码基本能够秒懂的 #include |