HDU 3874 neck.pdf (tree array)
Problem DescriptionMery has a beautiful neck.pdf. the neckenders is made up of N magic bils. each ball has a beautiful value. the bils with the same beautiful value look the same, so if two or more bils 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 neckexample is 1 1 1 2 3 1, we have F () = 1, F () = 3, F () = 6.
Now Mery thinks the necktasks is too long. she plans to take some continuous part of the neck.pdf to build a new one. she wants to know each of the beautiful value of M continuous parts of the neck.pdf. she will give you M intervals [L, R] (1 <= L <= R <= N) and you must tell her F (L, R) of them.
InputThe 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 bils. the second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N bils. 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.
OutputFor each query, output a line contains an integer number, representing the result of the query.
Sample Input
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
Sample Output
3714136Question: Here is a string of numbers for you to calculate the sum in the range [l, r]. You must repeat the number only once.
Tree array method: You will surely think of offline, but there is a way to sort offline:
Due to the need to duplicate the query, we consider sorting the query by the right range from small to large. For the query, we remove the preceding one by one.
Duplicate value. Only the current value is retained.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof a) typedef long LL; const int maxn = 50000 + 100; const int maxm = 200000 + 100; LL c [maxn], ans [maxm]; int pre [maxn], hash [1000000 + 10]; struct node {int l, r; int id;} q [maxm]; int num [maxn]; int t, n, m; int lowbit (int x) {return x & (-x);} void update (int x, LL w) {while (x
0) {s + = c [x]; x-= lowbit (x);} return s;} int cmp (node l1, node l2) {return l1.r