-
Notifications
You must be signed in to change notification settings - Fork 0
/
B_K_th_Beautiful_String.cpp
138 lines (130 loc) · 3.85 KB
/
B_K_th_Beautiful_String.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
const long double EPS = 1e-14;
const int maxn = 1000500;
const int mod = 1e9 + 7;
int tree[4 * maxn];
struct Point{
int x, y;
};
struct Segment{
Point l, r;
}seg[maxn];
struct SparseTable{
long long t[maxn],dp[maxn][21], lg[maxn];
SparseTable(){
lg[1] = 0 ;
for(int i = 2 ; i < maxn; ++i)
lg[i] = 1 + lg[i/2];
}
void build(int n){
assert(n < maxn);
memset(dp, -1, sizeof dp);
for(int i = 0 ; i < n ; ++i)
dp[i][0] = t[i];
for(int j = 1 ; j <= 20 ; ++j)
for(int i = 0 ; i < n ; ++i)
if(~dp[i][j - 1] && dp[i][j - 1] != i){
if(dp[dp[i][j - 1]][j - 1] != dp[i][j - 1])
dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
}
}spt;
void update(int node, int lo, int hi, int ql, int qh, int id){
if(ql > hi || qh < lo) return ;
if(ql <= lo && hi <= qh){
tree[node] = id;
return;
}
int mid = (lo + hi) >> 1;
update(2 * node + 1, lo, mid, ql, qh, id);
update(2 * node + 2, mid + 1, hi, ql, qh, id);
}
void get(int node, int lo, int hi, int el, int &id){
if(el < lo || el > hi) return;
id = max(id, tree[node]);
if(lo == hi) return;
int mid = (lo + hi) >> 1;
if(el <= mid) get(2 * node + 1, lo, mid, el, id);
else get(2 * node + 2, mid + 1, hi, el, id);
}
inline bool cmp(const Segment &a, const Segment &b){
if(a.r.y != b.r.y)
return a.r.y > b.r.y;
return a.l.x < b.l.x;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q;
cin >> n >> q;
for(int i = 0 ; i < n ; ++i){
cin >> seg[i].l.x >> seg[i].l.y >> seg[i].r.x >> seg[i].r.y;
if(seg[i].l.y > seg[i].r.y) swap(seg[i].l, seg[i].r);
}
sort(seg, seg + n, cmp);
for(int i = 0 ; i < n ; ++i){
int x = (seg[i].l.y > seg[i].r.y ? seg[i].l.x:seg[i].r.x);
if(seg[i].l.y != seg[i].r.y){
int p = -1;
get(0, 0, maxn - 1, x, p);
if(p <= 0) p = i;
else p--;
spt.t[i] = p;
}else{
spt.t[i] = i;
}
update(0, 0, maxn - 1, min(seg[i].l.x, seg[i].r.x), max(seg[i].l.x, seg[i].r.x), i + 1);
}
spt.build(n);
while(q--){
int x;
cin >> x;
int p = -1;
get(0, 0, maxn - 1, x, p);
if(p <= 0){
cout << x << '\n';
continue;
}
p--;
if(spt.dp[p][0] == p){
if(seg[p].l.y != seg[p].r.y){
cout << (seg[p].l.y > seg[p].r.y ? seg[p].l.x:seg[p].r.x) << '\n';
}else{
cout << (seg[p].l.y > seg[p].r.y ? seg[p].l.x:seg[p].r.x) << ' ' << seg[p].l.y << '\n';
}
continue;
}
int prv = -1;
while(1){
int pr = -1;
bool flag = false;
for(int j = 20 ; j >= 0 ; --j)
if(~spt.dp[p][j]){
pr = spt.dp[p][j];
if(pr == p) break;
if(!j){
prv = p;
p = pr;
break;
}
flag = true;
prv = spt.dp[p][j - 1];
p = pr;
break;
}
if(!flag) break;
}
/*cout << p << " = " << seg[p].l.x << ' ' << seg[p].l.y << ' ' << seg[p].r.x << ' ' << seg[p].r.y << '\n';
cout << prv << '\n';*/
if(seg[p].l.y != seg[p].r.y){
cout << (seg[p].l.y > seg[p].r.y ? seg[p].l.x:seg[p].r.x) << '\n';
}else{
cout << (seg[prv].l.y > seg[prv].r.y ? seg[prv].l.x:seg[prv].r.x)
<< ' ' << seg[p].l.y << '\n';
}
}
return 0;
}