TOJ Open Ural FU Championship 2013 G
G. Nested Segments
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
You are given n segments on a straight line. For each pair of segments it is known that they either have no common points or all points of one segment belong to the second segment.
Then m queries follow. Each query represents a point on the line. For each query, your task is to find the segment of the minimum length, to which this point belongs.
Input
The first line contains an integer n that is the number of segments (1 ≤ n ≤ 105). i’th of the next n lines contains integers ai and bi that are the coordinates of endpoints of the i’th segment (1 ≤ ai < bi ≤ 109). The segments are ordered by non-decreasing ai, and when ai = aj they are ordered by decreasing length. The next line contains an integer m that is the number of queries (1 ≤ m ≤ 105). j’th of the next m lines contains an integer cj that is the coordinate of the point (1 ≤ cj ≤ 109). The queries are ordered by non-decreasing cj.
Output
For each query output the number of the corresponding segment on a single line. If the point does not belong to any segment, output “-1”. The segments are numbered from 1 to n in order they are given in the input.
Sample
| input | output |
|---|---|
3 2 10 2 3 5 7 11 1 2 3 4 5 6 7 8 9 10 11 |
-1 2 2 1 3 3 3 1 1 1 -1 |
Problem Author: Mikhail Rubinchik, idea by Nikita Pervukhin
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 #define maxn 100055 7 #define ll long long 8 #define INF 0x7fffffff 9 //#define ll __int64 10 int n, m; 11 struct T{ 12 int x, cover, id, a, b, c; 13 }; 14 int cmp(T n1, T n2){ 15 if (n1.x != n2.x)return n1.x<n2.x; 16 return n1.c > n2.c; 17 } 18 int g[maxn * 2]; 19 T f[maxn * 4]; 20 T d[maxn * 4]; 21 int p[maxn * 2], ans[maxn * 2]; 22 int main(){ 23 while (~scanf("%d", &n)){ 24 memset(f, 0, sizeof f); 25 memset(p, 0, sizeof p); 26 memset(ans, 0, sizeof ans); 27 memset(d, 0, sizeof d); 28 memset(g, 0, sizeof g); 29 for (int i = 1; i <= n * 2; i += 2){ 30 scanf("%d%d", &d[i].x, &d[i + 1].x); 31 d[i].cover = 1; d[i + 1].cover = -1; 32 d[i].c = d[i + 1].x - d[i].x; 33 d[i].id = (i + 1) / 2; 34 } 35 d[0].x = 0; d[2 * n + 1].x = INF; d[0].id = -1; 36 d[0].cover = 1; d[2 * n + 1].cover = -1; 37 sort(d, d + n + n + 2, cmp); 38 int cov = 0, t = -2, tc, tid = -1; 39 for (int i = 0; i < 2 * n + 1; i++){ 40 if (d[i].cover == -1)f[i].a = d[i].x + 1; 41 else f[i].a = d[i].x; 42 cov += d[i].cover; 43 if (d[i].cover == 1)g[cov] = d[i].id; 44 if (d[i + 1].cover == 1)f[i].b = d[i + 1].x - 1; 45 else f[i].b = d[i + 1].x; 46 f[i].id = g[cov]; 47 } 48 for (int i = 0; i < 2 * n + 1; i++){ 49 p[i] = f[i].a; 50 ans[i] = f[i].id; 51 } 52 sort(f, f + 2 * n + 2, cmp); 53 int x; 54 scanf("%d", &m); 55 for (int j = 1; j <= m; j++){ 56 scanf("%d", &x); 57 printf("%d\n", ans[upper_bound(p, p + 2 * n + 1, x) - p - 1]); 58 } 59 } 60 return 0; 61 }
浙公网安备 33010602011771号