Title Link: https://vjudge.net/contest/252597#problem/C
1. Super memory, how to deal with it
#include<stdio.h>
#include<string>
#include<iostream>
#include<stack>
using namespace std;
typedef long long LL;
const int maxn = 300005;
stack<LL> nstack[maxn];
stack<LL> ss;
LL n, q,v,s;
int t,op;
void init()
{
for (LL i = 0; i <= n; i++)
while (!nstack[i].empty())
nstack[i].pop();
}
int main()
{
scanf("%d", &t);
while (t--)
{
init();
scanf("%lld%lld", &n, &q);
while (q--)
{
scanf("%d", &op);
if (op == 1)
{
scanf("%lld%lld", &s, &v);
nstack[s].push(v);
}
else if (op == 2)
{
scanf("%lld", &s);
if (nstack[s].size())
{
LL tmp = nstack[s].top();
nstack[s].pop();
printf("%lld\n", tmp);
}
else puts("EMPTY");
}
else
{
while (!ss.empty())ss.pop();
scanf("%lld%lld", &s, &v);
while(!nstack[v].empty())
{
ss.push(nstack[v].top());
nstack[v].pop();
}
while (!ss.empty())
{
nstack[s].push(ss.top());
ss.pop();
}
}
}
}
return 0;
}
Then I really can't optimize the memory. I'm dying to change some LL to int. although I know it's definitely not possible, I still want to hand in one. It's mle (but I know int can save 10 ^ 5)
Can't we use the stack of stl?
And then I went to see the underlying implementation
emmm I changed all the original code to int (int can also include 10 ^ 9 ah), and reported an error that never occurred: segment error
Is online God said to be a memory problem or compiler problem?
#include<string.h>
#include<stdio.h>
#include<string>
#include<iostream>
#include<stack>
using namespace std;
const int maxn = 1000005;
stack<int> nstack[maxn];
stack<int> ss;
int v, n, q, s;
int t, op;
void init()
{
for (int i = 0; i <= n; i++)
while (!nstack[i].empty()) nstack[i].pop();
}
int main()
{
scanf("%d", &t);
while (t--)
{
init();
scanf("%d%d", &n, &q);
while (q--)
{
scanf("%d", &op);
if (op == 1)
{
scanf("%d%d", &s, &v);
nstack[s].push(v);
}
else if (op == 2)
{
scanf("%d", &s);
if (nstack[s].size())
{
printf("%d\n", nstack[s].top());
nstack[s].pop();
}
else puts("EMPTY");
}
else
{
while (!ss.empty())ss.pop();
scanf("%d%d", &s, &v);
while (!nstack[v].empty())
{
ss.push(nstack[v].top());
nstack[v].pop();
}
while (!ss.empty())
{
nstack[s].push(ss.top());
ss.pop();
}
}
}
}
return 0;
}
I tried vector, still mle
#include<stdio.h>
#include<string>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 1000005;
vector<int> nstack[maxn];
int n, q, s,v;
int t, op;
void init()
{
for (int i = 0; i <= n; i++)
nstack[i].clear();
}
int main()
{
scanf("%d", &t);
while (t--)
{
init();
scanf("%d%d", &n, &q);
while (q--)
{
scanf("%d", &op);
if (op == 1)
{
scanf("%d%d", &s, &v);
nstack[s].push_back(v);
}
else if (op == 2)
{
scanf("%d", &s);
if (nstack[s].size())
{
printf("%d\n", nstack[s][nstack[s].size() - 1]);
nstack[s].pop_back();
}
else puts("EMPTY");
}
else
{
scanf("%d%d", &s, &v);
nstack[s].insert(nstack[s].end(),nstack[v].begin(), nstack[v].end());
nstack[v].clear();
}
}
}
return 0;
}
I'll change the list. It's over
(there's a magic function called splice)
#include<stdio.h>
#include<string>
#include<iostream>
#include<list>
using namespace std;
const int maxn = 1000005;
list<int> nstack[maxn];
int v, n, q, s;
int t, op;
void init()
{
for (int i = 0; i <= n; i++)
nstack[i].clear();
}
int main()
{
scanf("%d", &t);
while (t--)
{
init();
scanf("%d%d", &n, &q);
while (q--)
{
scanf("%d", &op);
if (op == 1)
{
scanf("%d%d", &s, &v);
nstack[s].push_back(v);
}
else if (op == 2)
{
scanf("%d", &s);
if (!nstack[s].empty())
{
printf("%d\n", nstack[s].back());
nstack[s].pop_back();
}
else puts("EMPTY");
}
else
{
scanf("%d%d", &s, &v);
nstack[s].splice(nstack[s].end(), nstack[v]);
//I've tried it. I'll timeout if I don't use the built-in function
/*while (!nstack[v].empty())
{
nstack[s].push_back(nstack[v].front());
nstack[v].pop_front();
}*/
}
}
}
return 0;
}
Explanation from the great gods
Why can't I use stack
Forget to add the way that the big bulls use arrays: put all the numbers in an array a, and record the numbers of the top, bottom and top of the stack in array a with three arrays top, bottom and next