Hdu 3874 neck.pdf [simple tree Array application]

Source: Internet
Author: User

Neck.pdf
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 1957 Accepted Submission (s): 702


Problem Description
Mery 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.
 

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 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.
 

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

Source
2011 Multi-University Training Contest 4-Host by SDU
 

Recommend
Lcy
 


Question:


Here is a sequence of N characters,
Then there are M queries, which require the calculation of the continuous sum of each interval
Note: duplicate elements can only appear once in a row or in a row.


Algorithm: Tree array for sequential and offline Processing

 

 


Ideas:


It is a very basic tree pile array of questions, and I can't think of it during the competition. After the competition, I saw other people's questions, and I found a solution only when I got an AC from my schoolmates.

It's really weak. Orz.

 


The key to the question: how to eliminate repeated elements in the sequence.

First, record all the inquiries, and then sort them by the successive query and the right endpoint of the interval from small to large.

You can use map <int, int> to record whether the current element has appeared. The value of the element recorded in the previous int, and the latest subscript of the element marked by the last int

Start initializing map is blank

Add the column array from the first element index = 1

Traverse the sorted query in sequence:

If the current element appears before, delete it in front of the tree array. At the same time, add its current position to the tree pile array.

That is to say, only the last position of the same element is used, and all the previous occurrences are eliminated.

This is equivalent to converting to the sequence sum of a continuous interval, because repeated elements have been eliminated.

This idea is also based on others. For more information, see code implementation.

I can't think of Orz for many problems.

# Include <stdio. h> # include <string. h >#include <algorithm> # include <map> # include <iostream> using namespace std; const int maxn = 50000 + 10; const int maxQ = 200000 + 10; int a [maxn] ;__ int64 c [maxn] ;__ int64 ans [maxQ]; map <int, int> mp; int n, m; struct Que {int left, right; int index;} q [maxQ]; bool cmp (Que a, Que B) {return. right <B. right;} int lowbit (int x) {return x & (-x);} void add (int index, int value) {while (ind Ex <= n) {c [index] + = value; index + = lowbit (index) ;}__ int64 sum (int index) {_ int64 ret = 0; while (index> 0) // subscript starts from 1 {ret + = c [index]; index-= lowbit (index);} return ret; // do not forget} int main () {int T; scanf ("% d", & T); while (T --) {scanf ("% d ", & n); memset (a, 0, sizeof (a); memset (c, 0, sizeof (c); for (int I = 1; I <= n; I ++) scanf ("% d", & a [I]); scanf ("% d", & m); for (int I = 1; I <= m; I ++ ){ Scanf ("% d", & q [I]. left, & q [I]. right); q [I]. index = I;} sort (q + 1, q + 1 + m, cmp); mp. clear (); // clear mark int index = 1; // traverse from 1 for (int I = 1; I <= m; I ++) {while (index <= q [I]. right) // For each query, traverse to the rightmost end of the range {if (mp [a [index]! = 0) // if the current number exists before, delete the add (mp [a [index],-a [index]) effect formed in the previous position. mp [a [index] = index; // record the position where the new element value is a [index] index add (index, a [index]); // re-join the effect formed at the current "last" position, index ++; // traverse the next element} ans [q [I]. index] = sum (q [I]. right)-sum (q [I]. left-1); // returns the continuous sequence and} for (int I = 1; I <= m; I ++) printf ("% I64d \ n ", ans [I]);} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.