1 #include<stdio.h> 2 3 void read(int &x){ 4 char ch = getchar();x = 0; 5 for (; ch < '0' || ch > '9'; ch = getchar()); 6 for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; 7 } 8 struct record 9 { 10 int value; 11 record *next; 12 record *pre; 13 }stack1[500010]; 14 int cnt; 15 16 class listqueue 17 { 18 public: 19 record *head,*tail; 20 void pop() 21 { 22 if(!empty()) 23 { 24 printf("%d\n",tail->value); 25 tail=tail->pre; 26 if(tail==0) 27 head=0; 28 else 29 tail->next=0; 30 } 31 else 32 puts("-1"); 33 } 34 bool empty() 35 { 36 return head==0; 37 } 38 void shift() 39 { 40 if(!empty()) 41 { 42 printf("%d\n",head->value); 43 head=head->next; 44 if(head==0) 45 tail=0; 46 else 47 head->pre=0; 48 } 49 else 50 puts("-1"); 51 } 52 void clear() 53 { 54 head=tail=0; 55 } 56 void unshift(int n) 57 { 58 stack1[cnt].value=n; 59 stack1[cnt].next=head; 60 stack1[cnt].pre=0; 61 if(head) 62 head->pre=&stack1[cnt]; 63 head=&stack1[cnt]; 64 if(tail==0) 65 tail=head; 66 cnt++; 67 } 68 void push(int n) 69 { 70 stack1[cnt].value=n; 71 stack1[cnt].next=0; 72 stack1[cnt].pre=tail; 73 if(tail) 74 tail->next=&stack1[cnt]; 75 tail=&stack1[cnt]; 76 if(head==0) 77 head=tail; 78 cnt++; 79 } 80 void append(listqueue &v) 81 { 82 if(v.head) 83 { 84 if(head==0) 85 head=v.head; 86 else 87 { 88 tail->next=v.head; 89 v.head->pre=tail; 90 } 91 tail=v.tail; 92 v.clear(); 93 } 94 } 95 void reverse() 96 { 97 if(empty()) 98 return; 99 record *temp,*temp1; 100 for(temp=tail;temp!=head;temp=temp->next) 101 { 102 temp1=temp->next; 103 temp->next=temp->pre; 104 temp->pre=temp1; 105 } 106 temp1=temp->next; 107 temp->next=temp->pre; 108 temp->pre=temp1; 109 head=tail; 110 tail=temp; 111 } 112 }rec[150100]; 113 114 void test(int i) 115 { 116 record *temp=rec[i].head; 117 if(temp==0) 118 return; 119 printf("%d: ",i); 120 for(temp=rec[i].head;temp!=rec[i].tail;temp=temp->next) 121 printf("%d->",temp->value); 122 printf("%d\n",temp->value); 123 } 124 int main() 125 { 126 int n,q,u,v,w,val; 127 int type,i; 128 while(~scanf("%d%d",&n,&q)) 129 { 130 cnt=1; 131 for(i=1;i<=n;i++) 132 { 133 rec[i].clear(); 134 } 135 while(q--) 136 { 137 read(type); 138 if(type==1) 139 { 140 read(u); 141 read(w); 142 read(val); 143 if(w==0) 144 rec[u].unshift(val); 145 else 146 rec[u].push(val); 147 } 148 else if(type==2) 149 { 150 read(u); 151 read(w); 152 if(w==0) 153 rec[u].shift(); 154 else 155 rec[u].pop(); 156 } 157 else 158 { 159 read(u); 160 read(v); 161 read(w); 162 if(w==1) 163 rec[v].reverse(); 164 rec[u].append(rec[v]); 165 } 166 167 } 168 } 169 return 0; 170 }
hdu 6375
猜你喜欢
转载自www.cnblogs.com/Taskr212/p/9460752.html
今日推荐
周排行