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
题意:给你一串数字,让你求区间[l,r]内的和,要求重复数字只求一次。
树状数组做法:大家肯定会想到离线,但是离线后排序如何排,这里有一个思路:
由于要去重,我们考虑将询问按右区间从小到大排序,对于询问,我们逐个去掉前面
重复的值,只保留当前的。
#include#include #include #include #include #include #include #include #include