Description
为了测试小W的物理水平,Mr.X在二维坐标系中放了N面镜子(镜子坐标绝对值不超过M),镜子均与坐标轴成45°角,所以一共有两种类型“/”和“\”。原点不会有镜子,任意一点最多只有一面镜子。
镜子两个面都能反光,而中间不透光,例如,对于一个“/”型镜子,下方向射入的光线会被反射到右方向,左方向射入的光线会被反射到上方向。
现在有一条光线从原点沿X轴正方向射出,求走过T路程后所在位置。
Data Constraint
对于10%的数据:N=1
对于25%的数据:T<=1000000
对于35%的数据:N<=1000
对于60%的数据:M<=1000
对于100%的数据:N<=100,000,M<=1,000,000,000,T<=10^18
Solution
前60分很明显是可以通过模拟来完成的,那么怎样优化时间呢?通过分析可以发现,在镜子与镜子之间的反射过程是最耗时间的,那么我们把每一面镜子,通过神奇的方法连接起来就能够快速完成反射操作。找到每一面镜子通过不同方向的反射到达的下一面镜子,把它们连接起来就好了。
Code
var
a,b,h,z:array[0..100005] of longint;
c:array[0..100005] of char;
f,g,l,ne:array[0..100000,1..4] of longint;
n,m,i,fx,tot,now,start:longint;
t,x,y:int64;
zx:array[1..4] of longint=(1,-1,0,0);
zy:array[1..4] of longint=(0,0,-1,1);
kg:char;
procedure qsort(l,r:longint);
var i,j,mid1,mid2:longint;
begin
i:=l;j:=r;
mid1:=a[(i+j)div 2];
mid2:=b[(i+j)div 2];
repeat
while (a[i]<mid1)or((a[i]=mid1)and(b[i]<mid2)) do inc(i);
while (mid1<a[j])or((mid1=a[j])and(mid2<b[j])) do dec(j);
if i<=j then
begin
a[0]:=a[i];a[i]:=a[j];a[j]:=a[0];
b[0]:=b[i];b[i]:=b[j];b[j]:=b[0];
h[0]:=h[i];h[i]:=h[j];h[j]:=h[0];
inc(i); dec(j);
end;
until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
begin
readln(n,m,t);
for i:=1 to n do
begin
read(a[i],b[i]);read(kg);readln(c[i]);h[i]:=i;
end;
qsort(1,n);
for i:=1 to n-1 do
begin
if a[i]<>a[i+1] then continue;
f[h[i],4]:=a[i+1];
g[h[i],4]:=b[i+1];
ne[h[i],4]:=h[i+1];
l[h[i],4]:=b[i+1]-b[i];
f[h[i+1],3]:=a[i];
g[h[i+1],3]:=b[i];
ne[h[i+1],3]:=h[i];
l[h[i+1],3]:=b[i+1]-b[i];
end;
z:=a;a:=b;b:=z;
qsort(1,n);
z:=a;a:=b;b:=z;
fx:=1;
for i:=1 to n do
if (b[i]=0)and(a[i]>0) then
begin
now:=h[i];
if t>=a[i] then
begin
t:=t-a[i];
x:=a[i];
end
else
begin
x:=x+t;
t:=0;
end;
case c[now] of
'/':
begin
if fx=1 then fx:=4
else if fx=2 then fx:=3
else if fx=3 then fx:=2
else if fx=4 then fx:=1;
end;
'\':
begin
if fx=1 then fx:=3
else if fx=2 then fx:=4
else if fx=3 then fx:=1
else if fx=4 then fx:=2;
end;
end;
break;
end;
for i:=1 to n-1 do
begin
if b[i]<>b[i+1] then continue;
f[h[i],1]:=a[i+1];
g[h[i],1]:=b[i+1];
ne[h[i],1]:=h[i+1];
l[h[i],1]:=a[i+1]-a[i];
f[h[i+1],2]:=a[i];
g[h[i+1],2]:=b[i];
ne[h[i+1],2]:=h[i];
l[h[i+1],2]:=a[i+1]-a[i];
end;
while t>0 do
begin
if (t>=l[now,fx])and(l[now,fx]<>0) then
begin
t:=t-l[now,fx];
tot:=f[now,fx];y:=g[now,fx];x:=tot;
now:=ne[now,fx];
case c[now] of
'/':
begin
if fx=1 then fx:=4
else if fx=2 then fx:=3
else if fx=3 then fx:=2
else if fx=4 then fx:=1;
end;
'\':
begin
if fx=1 then fx:=3
else if fx=2 then fx:=4
else if fx=3 then fx:=1
else if fx=4 then fx:=2;
end;
end;
end
else
begin
x:=x+zx[fx]*t;y:=y+zy[fx]*t;
break;
end;
end;
writeln(x,' ',y);
end.