-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestedSegments.java
165 lines (126 loc) · 4.26 KB
/
NestedSegments.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class NestedSegments
{
public static class Segment
{
static long number = 0;
int beg;
int end;
int pos;
long num;
Segment parent;
List<Segment> subsegments;
List<Integer> coords;
public Segment(int s, int e) {
beg = s;
end = e;
pos = 0;
num = number++;
parent = null;
}
public void add(Segment s) {
if (subsegments == null) {
subsegments = new ArrayList<>();
coords = new ArrayList<>();
}
//System.out.println(""+s+" added to "+this);
s.parent = this;
subsegments.add(s);
coords.add(s.beg);
}
public String toString() {
return "["+beg+","+end+"]";
}
public Segment find(int c) {
Segment current = this;
//System.out.println("+find: "+this+ " x="+c);
//System.out.println("coords: "+ Arrays.toString(this.coords.toArray()));
while (current.coords != null) {
int t = upperBound(current.coords, current.pos, current.coords.size() - 1, c);
// System.out.println("upperBound: "+t);
if (t == -1) {
break;
}
else {
Segment found = current.subsegments.get(t);
current.pos = t;
if (found.end < c) {
break;
}
current = found;
//System.out.println("current="+current);
}
}
//System.out.println("3found="+current+" c="+c);
return current.beg <= c && current.end >= c ? current : null;
}
private int upperBound(List<Integer> list, int from, int to, int value) {
//System.out.println(Arrays.toString( list.subList(from, to+1).toArray() ));
if (to < from) {
return -1;
}
else if (to == from) {
return list.get(to) > value ? -1 : to;
}
else if (to == from + 1) {
if (list.get(from) > value) {
return -1;
}
else if (list.get(to) > value) {
return from;
}
else {
return to;
}
}
int m = from + (to - from)/2;
int v = list.get(m);
if (v > value) {
return upperBound(list, from, m-1, value);
}
else {
return upperBound(list, m, to, value);
}
}
}
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int segments = in.nextInt();
Stack<Segment> parentSegments = new Stack<>();
Segment root;
Segment parent = new Segment(Integer.MIN_VALUE, Integer.MAX_VALUE);
root = parent;
while (segments-- > 0) {
int a = in.nextInt();
int b = in.nextInt();
Segment newSeg = new Segment(a,b);
while(a < parent.beg || b > parent.end) {
// System.out.println("pop parent");
parent = parentSegments.pop();
}
// System.out.println("new parent");
parent.add(newSeg);
parentSegments.push(parent);
parent = newSeg;
}
int queries = in.nextInt();
Segment current = root;
while (queries-- > 0) {
int x = in.nextInt();
Segment found = null;
while ( current != null && (found = current.find(x)) == null ) {
current = current.parent;
}
if (found == null || found.num == 0) {
System.out.println("-1");
}
else {
current = found;
System.out.println(found.num);
}
}
}
}