Poj 2274 The Race (reverse number + line segment tree)

Source: Internet
Author: User

Poj 2274 The Race (reverse number + line segment tree)
The Race

Time Limit:15000 MS Memory Limit:65536 K
Total Submissions:3237 Accepted:664
Case Time Limit:3000 MS

Description

During the Annual Interstellar Competition for Tuned Spaceships, N spaceships will be competing. each spaceship I is tuned in such a way that it can accelerate in zero time to its maximum speed Vi and remain cruising at that speed. due to past achievements, each spaceship starts at a starting position Xi, specifying how many kilometers the spaceship is away from the starting line.
The race course is infinitely long. because of the high speeds of the spaceships, the race course goes straight all the time. on that straight course, spaceships can pass one another very easily, without interfering with each other.
Lost people in the audience have not realized yet that the outcome of the race can be determined in advance. it is your task to show this to them, by telling them how many times spaceships will pass one another, and by predicting the first 10 000 times that spaceships pass in chronological order.
You may assume that each spaceship starts at a different position. Furthermore, there will never be more than two spaceships at the same position of the course at any time.

Input

The first line of the input specifies the number of spaceshipsN (0 <N <= 250 000) that are competing. each of the next N lines describe the properties of one spaceship. the I + 1th line describes the ith ship with two integers Xi and Vi, representing the starting position and the velocity of the ith spaceship (0 <= Xi <= 1 000, 0 <Vi <100 ). the spaceships are ordered according to the starting position, I. e. x1 <X2 <... <XN. the starting position is the number of kilometers past the starting line where the spaceship starts, and the velocity is given in kilometers per second.

Output

The first line of the output shoshould contain in the number of times that spaceships pass one another during the race modulo 1 000 000. by publishing the number of passes only modulo 1 000 000, you can at the same time prove your knowledge of it and don't spoil the party for the less intelligent people in the audience.
Each of the subsequent lines shocould represent one passing, in chronological order. if there wocould be more than 10 000 passings, only output the first 10 000 passings. if there are less than 10 000 passings, output all passings. each line shoshould consist of two integers I and j, specifying that spaceship I passes spaceship j. if multiple passings occur at the same time, they have to be sorted by their position on the course. this means that passings taking place closer to the starting line must be listed first. the time of a passing is the time when the two spaceships are at the same position.

Sample Input

40 22 13 86 3

Sample Output

23 41 2
 
Question: The starting point (not overlapped) and speed of a spacecraft fly in the same direction. Question 1: The total number of times each spacecraft is exceeded (Model 1000000). Question 2: output The first 10000 superships, sorted by time, and if the time is the same, sorted by the input order of the ships that surpass other ships.
 
Solution:
Question 1: sort by the starting point (the input has already been arranged for us). If the previous speed is higher than the current speed, the previous spacecraft will surely surpass the current speed; otherwise, it will not be possible. Therefore, you only need to calculate the inverse ordinal number and the sum.
 
Question 2: The first super ship must be two adjacent ships.
Counterproof: set the current position as the A-B-C, if the first transcendence is A-> C, it must be A-> B or B-> C, and A-> C for the first transcendence contradiction.
Therefore:
 <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink">
  
VcHJlPgo8cHJlIGNsYXNzPQ = "brush: java;"> in this way, I can ensure that each input is the ship that is currently the first to surpass!
 
After the design of the top layer is complete, the implementation problem occurs. I use the line segment tree for the number of reverse orders. I also used a line segment tree to find a ship that exceeded the ship. Each line segment is treated as a point, and each query is minimal and spof is updated.
 
#include 
  
   #include 
   
    #include #include 
    
     using namespace std;const int maxn1 = 250010;const int maxn2 = 110;const int mod = 1000000;struct tree1{    int l , r , sum;}a1[4*maxn2];struct plane{    int x , v;}P[maxn1];struct tree2{    int l , r , Max;}a2[4*maxn1];struct Node{    int l , r;}node[maxn1];int N;void build1(int l , int r , int k){    a1[k].l = l;    a1[k].r = r;    a1[k].sum = 0;    if(l != r){        int mid = (l+r)/2;        build1(l , mid , 2*k);        build1(mid+1 , r , 2*k+1);    }}void add1(int l , int r , int k){    if(l <= a1[k].l && a1[k].r <= r) a1[k].sum++;    else{        int mid = (a1[k].l+a1[k].r)/2;        if(mid >= r) add1(l , r , 2*k);        else add1(l , r , 2*k+1);        a1[k].sum = (a1[2*k].sum+a1[2*k+1].sum)%mod;    }}int query1(int l , int r , int k){    if(l <= a1[k].l && a1[k].r <= r) return a1[k].sum;    else{        int mid = (a1[k].l+a1[k].r)/2;        if(mid >= r) return query1(l , r , 2*k)%mod;        else if(l > mid) return query1(l , r , 2*k+1)%mod;        else return (query1(l , mid , 2*k)+query1(mid+1 , r , 2*k+1))%mod;    }}void pushup(int k){    if(a2[2*k].Max == -1 && a2[2*k+1].Max == -1) a2[k].Max = -1;    else if(a2[2*k].Max == -1) a2[k].Max = a2[2*k+1].Max;    else if(a2[2*k+1].Max == -1) a2[k].Max = a2[2*k].Max;    else{        int lson = a2[2*k].Max , rson = a2[2*k+1].Max;        int lnode = (P[node[lson].r].v-P[node[lson].l].v)*(P[node[rson].r].x-P[node[rson].l].x);        int rnode = (P[node[rson].r].v-P[node[rson].l].v)*(P[node[lson].r].x-P[node[lson].l].x);        if(lnode <= rnode) a2[k].Max = a2[2*k].Max;        else a2[k].Max = a2[2*k+1].Max;    }}void build2(int l , int r , int k){    a2[k].l = l;    a2[k].r = r;    a2[k].Max = -1;    if(l == r){        if(P[node[l].l].v <= P[node[l].r].v) a2[k].Max = -1;        else a2[k].Max = l;    }else{        int mid = (l+r)/2;        build2(l , mid , 2*k);        build2(mid+1 , r , 2*k+1);        pushup(k);    }}void update(int l , int r , int k){    if(l <= a2[k].l && a2[k].r <= r){        if(P[node[l].l].v <= P[node[l].r].v) a2[k].Max = -1;        else a2[k].Max = l;    }else{        int mid = (a2[k].l+a2[k].r)/2;        if(r <= mid) update(l , r , 2*k);        else update(l , r , 2*k+1);        pushup(k);    }}void computing(){    for(int i = 1; i < N; i++){        node[i].l = i;        node[i].r = i+1;    }    build2(1 , N-1 , 1);    int cnt = 0;    while(cnt < 10000){        if(a2[1].Max == -1) break;        int m = a2[1].Max;        printf("%d %d\n" , node[m].l , node[m].r);        swap(node[m].l , node[m].r);        update(m , m , 1);        if(m-1 >= 1){            node[m-1].r = node[m].l;            update(m-1 , m-1 , 1);        }        if(m+1 < N){            node[m+1].l = node[m].r;            update(m+1 , m+1 , 1);        }        cnt++;    }}void readcase(){    build1(1 , 100 , 1);    int ans = 0;    for(int i = 1; i <= N; i++){        scanf("%d%d" , &P[i].x , &P[i].v);        ans = (ans+query1(P[i].v+1 , 100 , 1))%mod;        add1(P[i].v , P[i].v , 1);    }    printf("%d\n" , ans);    if(ans != 0) computing();}int main(){    while(~scanf("%d" , &N)){        readcase();    }    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.