Codeforces 500E. New Year Domino Multiplier/line segment tree + offline

Source: Internet
Author: User
Tags cmath

Codeforces 500E. New Year Domino Multiplier/line segment tree + offline

 

 

 

E. New Year Domino time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Celebrating the new year, drawing people post videos of falling dominoes; Here's a list of them: https://www.youtube.com/results? Search_query = New + Years + Dominos

User ainta, who lives in a 2D world, is going to post a video as well.

There areNDominoes on a 2D Cartesian plane.I-Th domino (1? ≤?I? ≤?N) Can be represented as a line segment which is parallel toY-Axis and whose length isLI. The lower point of the domino is onX-Axis. Let's denoteX-Coordinate ofI-Th dominoPI. Dominoes are placed one after another, soP1? P2? PN? -? 1? PNHolds.

User ainta wants to take a video of falling dominoes. to make dominoes fall, he can push a single domino to the right. then, the domino will fall down drawing a circle-shaped orbit until the line segment totally overlaps with the x-axis.

Also, ifS-Th domino touchesT-Th domino while falling down,T-Th domino will also fall down towards the right, following the same procedure above. DominoSTouches dominoTIf and only if the segment representingSAndTIntersects.

See the picture above. if he pushes the leftmost domino to the right, it falls down, touching dominoes (A), (B) and (C ). as a result, dominoes (A), (B), (C) will also fall towards the right. however, domino (D) won't be affected by pushing the leftmost domino, but eventually it will fall because it is touched by domino (C) for the first time.

The picture above is an example of falling dominoes. Each red circle denotes a touch of two dominoes.

User ainta hasQPlans of posting the video.J-Th of them starts with pushingXJ-Th domino, and lasts untilYJ-Th domino falls. but sometimes, it cocould be impossible to achieve such plan, so he has to lengthen some dominoes. it costs one dollar to increase the length of a single domino by 1. user ainta wants to know, for each plan, the minimum cost needed to achieve it. plans are processed independently, I. e. if domino's length is increased in some plan, it doesn't affect its length in other plans. set of dominos that will fall should tXJ-Th domino andYJ-Th domino doesn' t matter, but the initial push shoshould be on dominoXJ.

Input

The first line contains an integerN(2? ≤?N? ≤? 2? ×? 105)-the number of dominoes.

NextNLines describe the dominoes.I-Th line (1? ≤?I? ≤?N) Contains two space-separated integersPI,LI(1? ≤?PI,?LI? ≤? (109)-X-Coordinate and the length ofI-Th domino. It is guaranteed thatP1? P2? PN? -? 1? PN.

The next line contains an integerQ(1? ≤?Q? ≤? 2? ×? 105)-the number of plans.

NextQLines describe the plans.J-Th line (1? ≤?J? ≤?Q) Contains two space-separated integersXJ,YJ(1? ≤?XJ? YJ? ≤?N). It meansJ-Th plan is, to pushXJ-Th domino, and shoot a video untilYJ-Th domino falls.

Output

For each plan, print a line containing the minimum cost needed to achieve it. If no cost is needed, print 0.

Sample test (s) input
61 53 34 49 210 112 141 22 42 52 6
Output
0112
Note

Consider the example. The dominoes are set like the picture below.

Let's take a look at the 4th plan. to make the 6th domino fall by pushing the 2nd domino, the length of the 3rd domino (whose x-coordinate is 4) shocould be increased by 1, and the 5th domino (whose x-coordinate is 9) shocould be increased by 1 (other option is to increase 4th domino instead of 5th also by 1 ). then, the dominoes will fall like in the picture below. each cross denotes a touch between two dominoes.


Method 1: Calculate the length of a segment that is not covered. You can use the line segment tree to Solve the Problem offline.

 

 

/*************************************** * ******** Author: CKbossCreated Time: Monday, January 1, March 23, 2015 27 seconds File Name: CF500_2.cpp *************************************** * *********/# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          Using namespace std; const int maxn = 400100; int n, q; struct Gun {int h, l, r;} gn [maxn]; struct QUE {int l, r, ans, id;} que [maxn]; bool cmp1 (que a, que B) {if (. l! = B. l) return A. l> B. l; return A. r> B. r;} bool cmp2 (que a, que B) {return A. id
           
             M) insert (L, R, rson); push_up (rt);} int query (int L, int R, int l, int r, int rt) {if (L <= l & r <= R) {return tree [rt];} push_down (l, r, rt); int m = (l + r) /2; int ret = 0; if (L <= m) ret + = query (L, R, lson); if (R> m) ret + = query (L, r, rson); return ret;} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt ", "w", stdout); scanf ("% d", & n); an ++; for (int I = 0; I
            
              = Que [I]. l-1) {// add nx to column int L = lower_bound (arr + 1, arr + an, gn [nx]. l)-arr; int R = lower_bound (arr + 1, arr + an, gn [nx]. r)-arr-1; insert (L, R, 1, an-2, 1); nx --;} int a = que [I]. l-1, B = que [I]. r-1; int L = lower_bound (arr + 1, arr + an, gn [a]. l)-arr; int R = lower_bound (arr + 1, arr + an, gn [B]. l)-arr-1; que [I]. ans = gn [B]. l-gn [a]. l-query (L, R, 1, an-2, 1);} sort (que, que + q, cmp2); for (int I = 0; I
             
              

 

 

Method 2: The multiplication method is used to pre-process a line segment with the forward direction of 2 ^ j.

 

 

/*************************************** * ******** Author: CKbossCreated Time:, Monday, January 1, March 23, 2015 File Name: CF500E_3.cpp // The multiplication method ********************************** * **************/# include
               
                
# Include
                
                 
# Include
                 
                  
# Include
                  
                   
# Include
                   
                    
# Include
                    
                      # Include
                     
                       # Include
                      
                        # Include
                       
                         # Include
                        Using namespace std; const int maxn = 200200; const int INF = 2100000000; int n; int a [maxn], B [maxn]; int d [maxn], dn; int p [maxn] [20]; int q [maxn] [20]; int main () {// freopen ("in.txt", "r", stdin ); // freopen ("out.txt", "w", stdout); scanf ("% d", & n); for (int I = 0; I
                         
                           = 0; I --) {while (a [d [dn-1] + B [d [dn-1] <= a [I] + B [I]) dn --; p [I] [0] = d [dn-1]; if (a [I] + B [I]> = a [d [dn-1]) q [I] [0] = 0; else q [I] [0] = a [d [dn-1]-a [I]-B [I]; d [dn ++] = I ;}for (int j = 1; j <20; j ++) {for (int I = n; I> = 0; I --) {p [I] [j] = p [p [I] [J-1] [J-1]; q [I] [j] = q [I] [J-1] + q [p [I] [J-1] [J-1] ;}} int T_T; scanf ("% d", & T_T); while (T_T --) {int l, r, ans = 0; scanf ("% d", & l, & r); l --; r --; for (int I = 19; I> = 0; I --) {if (p [l] [I] <= r) {ans + = q [l] [I]; l = p [l] [I] ;}} printf ("% d \ n", ans );} return 0 ;}
                         
                       
                      
                     
                    
                   
                  
                 
                
               


 

 

 

 

Related Article

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.