标签:splay cin using val data tom accept 完成后 limit
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1206 Accepted Submission(s): 393
#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include <string.h> #include <new> #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define maxn 150009 #define maxq 400009 using namespace std; struct node_data { node_data *l, *r; int data; }; struct node_que { node_data *head, *tail; }; void addhead(node_que &q, int a) { if (!q.head) { q.head = new node_data; q.head->data = a; q.head->l = q.head->r = NULL; q.tail = q.head; } else { node_data *xin = new node_data; xin->data = a; xin->r = q.head; xin->l = NULL; q.head->l = xin; q.head = xin; } } void addtail(node_que &q, int a) { if (!q.tail) { q.tail = new node_data; q.tail->data = a; q.tail->l = q.tail->r = NULL; q.head = q.tail; } else { node_data *xin = new node_data; xin->data = a; xin->l = q.tail; xin->r = NULL; q.tail->r = xin; q.tail = xin; } } int delehead(node_que &q) { if (!q.head) return -1; int v = q.head->data; if (q.tail == q.head) { node_data *a = q.tail; q.tail = q.head = NULL; free(a); return v; } q.head = q.head->r; free(q.head->l); q.head->l = NULL; return v; } int deletail(node_que &q) { if (!q.tail) return -1; int v = q.tail->data; if (q.tail == q.head) { node_data *a = q.tail; q.tail = q.head = NULL; free(a); return v; } q.tail = q.tail->l; free(q.tail->r); q.tail->r = NULL; return v; } void merge(node_que &u, node_que &v, int w) { if (!v.tail) return; if (w) { node_data *ch = v.head, *ch1; while (ch) { ch1 = ch->l; ch->l = ch->r; ch->r = ch1; ch = ch->l; } ch = v.head; v.head = v.tail; v.tail = ch; } if (u.tail) { u.tail->r = v.head; v.head->l = u.tail; } else { u.head = v.head; } u.tail = v.tail; v.tail = v.head = NULL; } void clearque(node_que &q) { while (q.head != q.tail) { q.head = q.head->r; free(q.head->l); } free(q.head); q.head = q.tail = NULL; } int n, q; int cz, u, v, w, val; node_que qq[maxn]; int main() { while (scanf("%d%d",&n,&q)!=EOF) { for(int i = 1; i <= q; i++){ scanf("%d%d", &cz, &u); if (cz == 1) { scanf("%d%d", &w, &val); if (w) addtail(qq[u], val); else addhead(qq[u], val); } else if (cz == 2) { scanf("%d", &w); int ans; if (w) ans = deletail(qq[u]); else ans = delehead(qq[u]); printf("%d\n", ans); } else { scanf("%d%d", &v, &w); merge(qq[u], qq[v], w); } } for (int i = 1; i <= n; i++) { clearque(qq[i]); } } return 0; }
标签:splay cin using val data tom accept 完成后 limit
原文地址:https://www.cnblogs.com/the-way-of-cas/p/9462119.html