gt22 icon

Untitled

gt22 | PRO | 09/20/18 07:01:56 PM UTC | 0 ⭐ | 541 👁️ | Never ⏰ | []
Java |

2.24 KB

|

None

|

0 👍

/

0 👎

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
 
public class O1987 {
 
    static class Segment {
 
        int id, start, end;
        Queue<Segment> children = new LinkedList<>();
        Segment parent;
 
        Segment(int id, int start, int end, Segment parent) {
            this.id = id;
            this.start = start;
            this.end = end;
            this.parent = parent;
        }
 
        boolean covers(int point) {
            return start <= point && point <= end;
        }
 
        @Override
        public String toString() {
            return start + "#" + end + "(" + children.size() + ")";
        }
    }
 
    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(r.readLine());
        Segment root = new Segment(-1, 0, Integer.MAX_VALUE, null);
        Segment cur = root;
        for (int i = 0; i < n; i++) {
            String[] s = r.readLine().split(" ");
            int start = Integer.parseInt(s[0]), end = Integer.parseInt(s[1]);
            while(start > cur.end) {
                cur = cur.parent;
            }
            cur.children.offer(cur = new Segment(i + 1, start, end, cur));
        }
        int c = Integer.parseInt(r.readLine());
        cur = root;
        for (int i = 0; i < c; i++) {
            int point = Integer.parseInt(r.readLine());
            while(!cur.covers(point)) {
                cur = cur.parent;
                // Current segment can only be leftmost, otherwise it would be removed earlier
                cur.children.remove();
            }
            while(!cur.children.isEmpty()) {
                Segment child = cur.children.element();
                if(!child.covers(point)) {
                    if(child.end < point) {
                        cur.children.remove();
                    } else {
                        break;
                    }
                } else {
                    cur = cur.children.element();
                }
            }
            System.out.println(cur.id);
        }
    }
 
 
}

Comments