JZOJ 5032.C

做法简述:扫描线预处理,倍增回答询问。
脑子:好的我会了。
手:不你不会(并敲出了近 7k 的 WA 0pts 代码)。

用扫描线预处理出每个箭头和每个人接下来到哪个箭头。
注意仅仅保证箭头不相交,但是可以相对撞上,并且人可以一开始就在箭头上(这是若干次 WA 0pts 换来的经验

然后处理出 \(f_{i,j},g_{i,j},h_{i,j}\) 分别表示从第 \(i\) 个箭头切换 \(2^j\) 个箭头到哪个箭头,一共走了多少单位距离(不算第一步,因为其实并不能确定从哪里出发),走到了哪个位置。
查询的时候 xjb 倍增一下就行了(

代码:

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ls (p << 1)
#define rs (ls | 1)
using namespace std;

const int BUFF_SIZE = 1 << 20;
char BUFF[BUFF_SIZE],*BB,*BE;
#define gc() (BB == BE ? (BE = (BB = BUFF) + fread(BUFF,1,BUFF_SIZE,stdin),BB == BE ? EOF : *BB++) : *BB++)
template<class T>
inline void read(T &x)
{
x = 0;
char ch = 0,w = 0;
for(;ch < '0' || ch > '9';w |= ch == '-',ch = gc());
for(;ch >= '0' && ch <= '9';x = (x << 3) + (x << 1) + (ch ^ '0'),ch = gc());
w && (x = -x);
}
inline char getc()
{
char ch = 0;
for(;ch < 'A' || ch > 'Z';ch = gc());
return ch;
}

const int N = 1e5;
const int LG = 20;
int n,m,q;
int nxt[N + 5];
struct s_query
{
int x,y,dir;
long long T;
} qry[N + 5];
int f[N + 5][LG + 5],h[N + 5][LG + 5][2];
long long g[N + 5][LG + 5];
struct Arrow
{
int x1,y1,x2,y2,dir,id;
} a[N * 2 + 5],ori[N + 5];
inline bool cmp0(const Arrow &a,const Arrow &b)
{
return a.y1 > b.y1 || (a.y1 == b.y1 && a.id > b.id);
}
inline bool cmp1(const Arrow &a,const Arrow &b)
{
return a.y1 < b.y1 || (a.y1 == b.y1 && a.id > b.id);
}
inline bool cmp2(const Arrow &a,const Arrow &b)
{
return a.x1 < b.x1 || (a.x1 == b.x1 && a.id > b.id);
}
inline bool cmp3(const Arrow &a,const Arrow &b)
{
return a.x1 > b.x1 || (a.x1 == b.x1 && a.id > b.id);
}
int seg[(N << 3) + 5];
inline void push(int p)
{
if(seg[p])
seg[ls] = seg[rs] = seg[p],seg[p] = 0;
}
void update(int l,int r,int k,int p,int tl,int tr)
{
if(l <= tl && tr <= r)
{
seg[p] = k;
return ;
}
push(p);
int mid = tl + tr >> 1;
l <= mid && (update(l,r,k,ls,tl,mid),1);
r > mid && (update(l,r,k,rs,mid + 1,tr),1);
}
int query(int x,int p,int tl,int tr)
{
if(tl == tr)
return seg[p];
push(p);
int mid = tl + tr >> 1;
return x <= mid ? query(x,ls,tl,mid) : query(x,rs,mid + 1,tr);
}
int main()
{
freopen("c.in","r",stdin),freopen("c.out","w",stdout);
read(n),read(m);
for(register int i = 1;i <= n;++i)
read(ori[i].x1),read(ori[i].y1),read(ori[i].x2),read(ori[i].y2),ori[i].dir = ori[i].y1 < ori[i].y2 ? 0 : ori[i].y1 > ori[i].y2 ? 1 : (ori[i].x1 < ori[i].x2) + 2,a[i] = ori[i],a[i].id = i;
read(q);
char dir;
for(register int i = 1;i <= q;++i)
read(qry[i].x),a[n + i].x1 = qry[i].x,read(qry[i].y),a[n + i].y1 = qry[i].y,dir = getc(),a[n + i].dir = qry[i].dir = dir == 'U' ? 0 : dir == 'D' ? 1 : (dir == 'R') + 2,a[n + i].id = -i,read(qry[i].T);
sort(a + 1,a + n + q + 1,cmp0);
for(register int i = 1;i <= n + q;++i)
{
if(!a[i].dir)
(a[i].id < 0 ? nxt[-a[i].id] : f[a[i].id][0]) = query(a[i].x1,1,0,m);
if(a[i].id > 0)
update(min(a[i].x1,a[i].x2),max(a[i].x1,a[i].x2),a[i].id,1,0,m);
}
sort(a + 1,a + n + q + 1,cmp1),memset(seg,0,sizeof seg);
for(register int i = 1;i <= n + q;++i)
{
if(a[i].dir == 1)
(a[i].id < 0 ? nxt[-a[i].id] : f[a[i].id][0]) = query(a[i].x1,1,0,m);
if(a[i].id > 0)
update(min(a[i].x1,a[i].x2),max(a[i].x1,a[i].x2),a[i].id,1,0,m);
}
sort(a + 1,a + n + q + 1,cmp2),memset(seg,0,sizeof seg);
for(register int i = 1;i <= n + q;++i)
{
if(a[i].dir == 2)
(a[i].id < 0 ? nxt[-a[i].id] : f[a[i].id][0]) = query(a[i].y1,1,0,m);
if(a[i].id > 0)
update(min(a[i].y1,a[i].y2),max(a[i].y1,a[i].y2),a[i].id,1,0,m);
}
sort(a + 1,a + n + q + 1,cmp3),memset(seg,0,sizeof seg);
for(register int i = 1;i <= n + q;++i)
{
if(a[i].dir == 3)
(a[i].id < 0 ? nxt[-a[i].id] : f[a[i].id][0]) = query(a[i].y1,1,0,m);
if(a[i].id > 0)
update(min(a[i].y1,a[i].y2),max(a[i].y1,a[i].y2),a[i].id,1,0,m);
}
for(register int i = 1;i <= n;++i)
if(ori[i].dir < 2)
{
h[i][0][0] = ori[i].x1;
if(ori[f[i][0]].dir < 2)
h[i][0][1] = ori[i].dir == ori[f[i][0]].dir ? ori[f[i][0]].y1 : ori[f[i][0]].y2;
else
h[i][0][1] = ori[f[i][0]].y1;
}
else
{
h[i][0][1] = ori[i].y1;
if(ori[f[i][0]].dir > 1)
h[i][0][0] = ori[i].dir == ori[f[i][0]].dir ? ori[f[i][0]].x1 : ori[f[i][0]].x2;
else
h[i][0][0] = ori[f[i][0]].x1;
}
for(register int i = 1;i <= LG;++i)
for(register int j = 1;j <= n;++j)
if(f[j][i - 1] && f[f[j][i - 1]][i - 1])
f[j][i] = f[f[j][i - 1]][i - 1],
g[j][i] = g[j][i - 1] + g[f[j][i - 1]][i - 1] + abs(h[j][i - 1][0] - h[f[j][i - 1]][0][0]) + abs(h[j][i - 1][1] - h[f[j][i - 1]][0][1]),
h[j][i][0] = h[f[j][i - 1]][i - 1][0],h[j][i][1] = h[f[j][i - 1]][i - 1][1];
for(register int i = 1,dis,cur,curx,cury;i <= q;++i)
{
long long T = qry[i].T;
cur = nxt[i];
if(!qry[i].dir)
curx = qry[i].x,cury = max(qry[i].y,ori[nxt[i]].dir == 1 ? ori[nxt[i]].y2 : ori[nxt[i]].y1);
else if(qry[i].dir == 1)
curx = qry[i].x,cury = min(qry[i].y,ori[nxt[i]].dir == 0 ? ori[nxt[i]].y2 : ori[nxt[i]].y1);
else if(qry[i].dir == 2)
curx = min(qry[i].x,ori[nxt[i]].dir == 3 ? ori[nxt[i]].x2 : ori[nxt[i]].x1),cury = qry[i].y;
else
curx = max(qry[i].x,ori[nxt[i]].dir == 2 ? ori[nxt[i]].x2 : ori[nxt[i]].x1),cury = qry[i].y;
dis = abs(qry[i].x - curx) + abs(qry[i].y - cury);
if(!nxt[i] || T <= dis)
{
if(!qry[i].dir)
printf("%d %d\n",qry[i].x,(int)min(qry[i].y + T,(long long)m));
else if(qry[i].dir == 1)
printf("%d %d\n",qry[i].x,(int)max(qry[i].y - T,0LL));
else if(qry[i].dir == 2)
printf("%d %d\n",(int)max(qry[i].x - T,0LL),qry[i].y);
else
printf("%d %d\n",(int)min(qry[i].x + T,(long long)m),qry[i].y);
}
else
{
T -= dis;
for(register int j = LG;~j;--j)
if(f[cur][j] && T >= g[cur][j] + abs(curx - h[cur][0][0] + abs(cury - h[cur][0][1])))
T -= g[cur][j] + abs(curx - h[cur][0][0] + abs(cury - h[cur][0][1])),curx = h[cur][j][0],cury = h[cur][j][1],cur = f[cur][j];
if(!ori[cur].dir)
printf("%d %d\n",curx,(int)min(cury + T,(long long)m));
else if(ori[cur].dir == 1)
printf("%d %d\n",curx,(int)max(cury - T,0LL));
else if(ori[cur].dir == 2)
printf("%d %d\n",(int)max(curx - T,0LL),cury);
else
printf("%d %d\n",(int)min(curx + T,(long long)m),cury);
}
}
}