Codeforces Round #301 (Div. 2) (tree array + discretization)

Source: Internet
Author: User

Codeforces Round #301 (Div. 2) (tree array + discretization)

 

E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

There is an infinite sequence consisting of all positive integers in the increasing order:P? =? {1 ,? 2 ,? 3 ,?...}. We performed medNSwapoperations with this sequence.Swap(A,?B) Is an operation of swapping the elements of the sequence on positionsAAndB. Your task is to find the number of inversions in the resulting sequence, I. e. the number of such index pairs (I,?J), ThatI? J andPI?>?PJ.

Input

The first line contains a single integerN(1? ≤?N? ≤? 105)-the number of swap operations applied to the sequence.

Each of the nextNLines contains two integersAIAndBI(1? ≤?AI,?BI? ≤? 109,AI? ?BI)-The arguments of the swap operation.

Output

Print a single integer-the number of inversions in the resulting sequence.

Sample test (s) input
24 21 4
Output
4
Input
31 63 42 5
Output
15
Note

In the first sample the sequence is being modified as follows:. It has 4 inversions formed by index pairs (1 ,? 4), (2 ,? 3), (2 ,? 4) and (3 ,? 4 ).


 

 

Question: 1, 2, 3, 4 ,.... each operation is performed on the number of locations a and B for n operations, and the number of pairs in the reverse sequence is asked.

Use map to create the final sequence (just create the number of positions in the question ). Then, perform operations in reverse order based on the tree array. However, discretization is required.

Consider such a sequence as a [l], l + 1, l + 2 ,...., r-1, a [r] where the numbers on l and r are exchanged (not necessarily on l and r), considering the influence of the intermediate section on the number of reverse orders, so w = (r-1)-(l + 1)-1, the number of the right side less than l + 1 is x, then the number of the right side of the range [l + 1, r-1] The Contribution of the number of backward orders is w * x (So ans + = w * Query (Loc-1 )), this Section contributes to each number on the left of l + 1 and greater than the R-1 is w (So Modify (Loc-1, w, tot ))

 

 

 

# Include
 
  
# Define foreach (it, v) for (_ typeof (v). begin () it = (v). begin (); it! = (V ). end () ;++ it) using namespace std; typedef long ll; const int maxn = 2e5 + 100; ll c [maxn]; # define lowbit (x) & (-x) void Modify (int x, ll d, int n) {while (x <= n) {c [x] + = d; x + = lowbit (x) ;}} ll Query (int x) {ll res = 0; while (x> 0) {res ++ = c [x]; x-= lowbit (x);} return res;} int main (int argc, char const * argv []) {int n; while (cin> n) {map
  
   
Q; for (int I = 0; I <n; I ++) {int L, R; cin> L> R; if (Q. find (L) = Q. end () Q [L] = L; if (Q. find (R) = Q. end () Q [R] = R; swap (Q [L], Q [R]);} int tot = 0; vector
   
    
> V; vector
    
     
Sec; foreach (it, Q) {v. push_back (* it); sec. push_back (it-> second); tot ++;} sort (sec. begin (), sec. end (); memset (c, 0, sizeof (c [0]) * (tot + 10); ll ans = 0; for (int I = tot-1; i> = 0; I --) {int Loc = lower_bound (sec. begin (), sec. end (), v [I]. second)-sec. begin () + 1; ans + = Query (Loc-1);/* count right less than v [I]. number of seconds */if (I = 0) break; Modify (Loc, 1, tot); ll w = (v [I]. first-1)-v [I-1]. first;/* a [l], l + 1, l + 2 ,... r-1, number of [l + 1, r-1] range elements in a [r] */if (w <1) continue; Loc = lower_bound (sec. begin (), sec. end (), v [I-1]. first + 1)-sec. begin () + 1;/* sequence v [I-1]. first + 1, v [I-1]. first + 2 ..., v [I]. first-1 position after discretization */ans + = w * Query (Loc-1);/* Statistical sequence v [I-1]. first + 1, v [I-1]. first + 2 ..., v [I]. the right side of first-1 is less than the number of the sequence * // * Why add w to the Loc-1? Because v [I]. the first discrete location is Loc, if it is Loc and v [k]. second = v [I]. if the first k is smaller than I, then the v [k] is counted. when the number of backward orders at the first position is not counted to this continuous interval */Modify (Loc-1, w, tot);} cout <
     

 

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.