1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<iostream>
5 #include<algorithm>
6 #include<queue>
7 using namespace std;
8 #define Maxn 15010
9
10 struct node
11 {
12 int id,st,w,rk;
13 friend bool operator < (node x,node y)
14 {
15 return (x.rk==y.rk)?(x.st>y.st):x.rk<y.rk;
16 }
17 };
18
19 priority_queue<node > q;
20
21 int op[Maxn*100],opp[Maxn*100],pl;
22
23 int mymax(int x,int y) {return x>y?x:y;}
24
25 int main()
26 {
27 node nw,now;
28 int t=0;
29 while(!q.empty()) q.pop();
30 pl=0;
31 while(scanf("%d%d%d%d",&nw.id,&nw.st,&nw.w,&nw.rk)!=EOF)
32 {
33 if(q.empty())
34 {
35 q.push(nw);
36 }
37 else
38 {
39 now=q.top();
40 while(mymax(t,now.st)+now.w<=nw.st)
41 {
42 t=mymax(t,now.st)+now.w;
43 op[++pl]=now.id;opp[pl]=t;
44 q.pop();
45 if(q.empty()) break;
46 now=q.top();
47 }
48 if(!q.empty())
49 {
50 q.pop();
51 now.w-=nw.st-mymax(t,now.st);
52 q.push(now);
53 }
54 q.push(nw);
55 }
56 t=mymax(t,nw.st);
57 }
58 while(!q.empty())
59 {
60 node now=q.top();q.pop();
61 t=mymax(t,now.st)+now.w;
62 op[++pl]=now.id;opp[pl]=t;
63 }
64 for(int i=1;i<=pl;i++)
65 {
66 printf("%d %d\n",op[i],opp[i]);
67 }
68 return 0;
69 }