传送门
直接用堆模拟不虚。
var
a,b,c,d:array [0..300005] of longint;
n,h,t,time,i,j:longint;
procedure set1(x:longint);
var
t:longint;
begin
while (x>1) do
if (d[x]>d[x div 2]) or ((d[x]=d[x div 2]) and (b[x]<d[x div 2]))then
begin
t:=d[x]; d[x]:=d[x div 2]; d[x div 2]:=t;
t:=c[x]; c[x]:=c[x div 2]; c[x div 2]:=t;
t:=b[x]; b[x]:=b[x div 2]; b[x div 2]:=t;
t:=a[x]; a[x]:=a[x div 2]; a[x div 2]:=t;
x:=x div 2;
end
else break;
end;
procedure set2(x:longint);
var
s,t:longint;
begin
while (x*2<=h) do
begin
if (x*2+1>h) or ((d[x*2]>d[x*2+1]) or
((d[x*2]=d[x*2+1]) and (b[2*x]<b[2*x+1]))) then
t:=x*2 else t:=x*2+1;
if (d[t]>d[x]) or ((d[t]=d[x]) and (b[t]<b[x])) then
begin
s:=d[t]; d[t]:=d[x]; d[x]:=s;
s:=c[t]; c[t]:=c[x]; c[x]:=s;
s:=b[x]; b[x]:=b[t]; b[t]:=s;
s:=a[x]; a[x]:=a[t]; a[t]:=s;
x:=t;
end
else break;
end;
end;
begin
n:=0;
while not(eof) do
begin inc(n);
readln(a[n],b[n],c[n],d[n]);
end;
a[n+1]:=10000000; b[n+1]:=1000000000;
c[n+1]:=1; d[n+1]:=1;
h:=1;
t:=0;
i:=2;
time:=b[1];
while (t<=n) and (i<=n) do
begin
while (time+c[1]<=b[i]) and (h>0) do
begin
time:=time+c[1];
writeln(a[1],' ',time);
a[1]:=a[h]; b[1]:=b[h]; c[1]:=c[h]; d[1]:=d[h];
dec(h); set2(1); inc(t);
end;
if (h>0) then c[1]:=c[1]-(b[i]-time);
time:=b[i]; inc(h);
a[h]:=a[i]; b[h]:=b[i]; c[h]:=c[i]; d[h]:=d[i];
set1(h); inc(i);
end;
for i:=t+1 to n do
begin
time:=time+c[1];
writeln(a[1],' ',time);
c[1]:=c[h]; a[1]:=a[h]; b[1]:=b[h]; d[1]:=d[h];
dec(h); set2(1);
end;
end.