Reverse Order (line segment tree)

Source: Internet
Author: User
Tags cmath

Reverse Order (line segment tree)

A sequence of a1, a2, a3... aN is used to obtain the number of satisfied: ai> aj and I <j.

One of the easiest ways to think about it is to enumerate all the I and check whether j is satisfied. It is obviously the complexity of O (n ^ 2. Not good enough.

You can consider this by opening an array to save the location of the n number and the corresponding number of times. This array should be open to the MAX, or hash, of the maximum number in array, there are no elements in the initial status array, and each number corresponds to 0.

If you consider the number of I, find the number of all numbers greater than it, and the search range is ai + 1 ~ MAX, this is the sum of the reverse order pairs at the position I. Then, add the number of a [I] to the array, that is, the number of a [I] positions plus 1. Until the end of n, the reverse order is obtained.

In this way, the complexity is still O (n ^ 2), but the search and addition operations can be solved by the line segment tree, which reduces the complexity to O (nlogn ).

Another problem is that if a [I] can reach 10 ^ 9 or even bigger, the array cannot be opened, and even the opening time cannot be tolerated, discretization is required, map n numbers to 1 ~ Within the range of n, This sorting operation can be easily solved by adding two points. All numbers are controlled within the n range, and the line segment tree solution is ideal.


Take POJ2299 as an example: the question is that a reverse order is required. For details, see the code:


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include # define lson o <1, l, m # define rson o <1 | 1, m + 1, rusing namespace std; typedef long LL; const int maxn = 500500; const int MAX = 0x3f3f3f3f; int n, a, B, in [maxn], tt [maxn], fu [maxn], f [maxn]; LL num [maxn <2]; int bs (int v, int x, int y) {while (x <y) {int m = (x + y)> 1; if (fu [m]> = v) y = m; else x = m + 1;} return x;} void up (int o) {num [o] = num [o <1] + num [o <1 | 1];} void build (int o, I Nt l, int r) {num [o] = 0; if (l = r) return; int m = (l + r)> 1; build (lson ); build (rson);} void update (int o, int l, int r) {if (l = r) {num [o] ++; return ;} int m = (l + r)> 1; if (a <= m) update (lson); else update (rson); up (o );} LL query (int o, int l, int r) {if (a <= l & r <= B) return num [o]; int m = (l + r)> 1; LL ans = 0; if (a <= m) ans + = query (lson); if (m <B) ans + = query (rson); retur N ans;} int main () {while (cin> n, n) {for (int I = 0; I <n; I ++) {scanf ("% d", & in [I]); tt [I] = in [I]; // tt records the original sequence} sort (in, in + n); int k = 0; fu [k ++] = in [0]; // fu is the auxiliary array for (int I = 1; I <n; I ++) if (in [I]! = In [I-1]) fu [k ++] = in [I]; B = 0; for (int I = 0; I <n; I ++) {// discrete process, binary f [I] = bs (tt [I], 0, k-1); B = max (B, f [I]);} LL ans = 0; build (1, 0, B); for (int I = 0; I <n; I ++) {a = f [I] + 1; // query f [I] + 1 ~ The number of n. The number is f [I]. The current reverse order is the total number of ans + = query (1, 0, B); a = f [I]; // Add f [I] to the array for update (1, 0, B);} cout <ans <endl;} 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.