题意:对n个栈,有q次操作。每个操作可能为三种情况中的一种:1.将v插入到s栈的顶端;2.输出s栈的栈顶(若栈为空则输出empty);3.将栈t插入到栈s的栈顶。

开始考虑到指针可能会mle,用数组模拟链表来实现。迷之wa,中间少写一句,若s栈为空,则s栈的栈顶变为t栈的栈顶。

 #include <iostream>
#include <cstring>
#include <cstdio> const int maxn = 3e5+;
struct node{
long long data;
long long pre;
}arr[maxn];
struct list{
long long top;
long long head;
}lst[maxn];
long long used; void insert(long long s, long long v){
arr[used].data = v;
arr[used].pre = lst[s].top;
lst[s].top = used;
if (lst[s].head == -)
lst[s].head = used;
used++;
}
void pop(long long s){
if (lst[s].top == -)
printf("EMPTY\n");
else{
printf("%d\n", arr[lst[s].top].data);
lst[s].top = arr[lst[s].top].pre;
if (lst[s].top == -)
lst[s].head = -;
}
}
void move(long long s, long long t){ //把t放到s上
if (lst[t].top != -){
arr[lst[t].head].pre = lst[s].top;
if (lst[s].top==-) lst[s].head=lst[t].head;
lst[s].top = lst[t].top;
lst[t].top = lst[t].head = -;
}
} int main(){
int T;
scanf("%d", &T); while (T--){
long long n,q;
scanf("%lld%lld", &n, &q);
used = ;
memset(lst, -, sizeof(lst));
long long s,t,mod;
long long v;
while (q--){
scanf("%lld", &mod);
if (mod == ){
scanf("%lld%lld", &s, &v);
insert(s, v);
}
else if (mod == ){
scanf("%lld", &s);
pop(s);
}
else {
scanf("%lld%lld", &s, &t);
move(s, t);
}
}
} return ;
}

zoj4016 Mergeable Stack的更多相关文章

  1. Mergeable Stack(链表实现栈)

    C - Mergeable Stack ZOJ - 4016 一开始用stl中内置的栈来写,其中第三个操作,我先复制到一个数组,再将其倒给另一个栈 这个方法有两个错误的地方: 1.栈在内存很大需要扩容 ...

  2. ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given initially empty stacks, ther ...

  3. ZOJ 4016 Mergeable Stack(栈的数组实现)

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given  initially empty stacks, the ...

  4. ZOJ 4016 Mergeable Stack 链表

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given  initially empty stacks, the ...

  5. Mergeable Stack ZOJ - 4016(list)

    ZOJ - 4016 vector又T又M list是以链表的方式存储的 是一个双向链表 元素转移操作中,不能一个个遍历加入到s中,list独有的splic函数可以在常数时间内实现合并(并删除源lis ...

  6. C Mergeable Stack(list超好用)

    ZOJ  4016 list用法https://www.cnblogs.com/LLLAIH/p/10673068.html 一开始用普通的栈做,超内存,链表模拟栈也没写出来orz.补题发现list超 ...

  7. Mergeable Stack 直接list内置函数。(152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上. 题解:直接用list 的pop_back,push_back,splice 模拟, 坑:用splice ...

  8. ZOJ - 4016 Mergeable Stack 【LIST】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...

  9. The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack

    题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...

随机推荐

  1. PHP-GTK的demo在windows下运行出现的问题

    I am trying to use Firebird 2.5.2.26539 with wamp,When i enable the extensions of firebird in php: - ...

  2. conn not captured

    线程 和 事件中 变量 not captured 把变量定义为 static 或者添加为全局变量(放在main之前)

  3. 《Delphi XE6 android 编程入门教程》推荐

    近5.6年已经没有看见关于delphi的新技术的书出来了(看来在国内delphi的使用量确实很低了), 高勇同学最近出了一本<Delphi XE6 android 编程入门教程>,上周刚拿 ...

  4. 2018.10.02 NOIP模拟 聚会(前缀和)

    传送门 今天的签到题. 直接前缀和处理一下就秒了. 然而考试的时候智障用线段树维护被卡成了30分,交到OJ一测竟然有100? 搞得我都快生无可恋了. 如果用线段树来做可以类比这道题的写法,直接维护区间 ...

  5. 2018.09.17 atcoder Tak and Cards(背包)

    传送门 背包经典题. 直接f[i][j]f[i][j]f[i][j]表示选i张牌和为j的方案数. 最后统计答案就行了. 代码: #include<bits/stdc++.h> #defin ...

  6. hdu-1711(kmp算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 思路:kmp模板,注意用scanf,不然超时. #include<iostream> ...

  7. Django基础(二)

    https://www.cnblogs.com/yuanchenqi/articles/5716193.html

  8. java redis基本操作

    package com.redis; import java.util.ArrayList;import java.util.Iterator;import java.util.List;import ...

  9. 1) Apache Maven 's README.txt

    Apache Maven What is it? ----------- Maven is a software project management and comprehension tool. ...

  10. PBOCIC读芯片卡流程

    https://blog.csdn.net/kxd_ysheng/article/details/21178101?_t=t PBOCIC读芯片卡流程,参考上面的博客,整理了一下PBOCIC卡读流程. ...