2018 “百度之星”程序设计大赛 - 初赛(A)度度熊学队列 list rope

c++ list使用

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <time.h>
  6 #include <string>
  7 #include <set>
  8 #include <map>
  9 #include <list>
 10 #include <ext/rope>
 11 #include <stack>
 12 #include <queue>
 13 #include <vector>
 14 #include <bitset>
 15 #include <algorithm>
 16 #include <iostream>
 17 using namespace std;
 18 #define ll long long
 19 #define minv 1e-6
 20 #define inf 1e9
 21 #define pi 3.1415926536
 22 #define E  2.7182818284
 23 const ll mod=1e9+7;//998244353
 24 const int maxn=150010;
 25 using namespace __gnu_cxx;
 26 
 27 char ch;
 28 
 29 void read(int &x){
 30     ch = getchar();x = 0;
 31     for (; ch < '0' || ch > '9'; ch = getchar());
 32     for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
 33 }
 34 
 35 list<int>f[maxn];
 36 
 37 int main()
 38 {
 39     int n,q,mode,u,w,val,v,i;
 40     while (~scanf("%d%d",&n,&q))
 41     {
 42         for (i=1;i<=n;i++)
 43             f[i].clear();
 44         while (q--)
 45         {
 46             read(mode);
 47             if (mode==1)
 48             {
 49                 read(u),read(w),read(val);
 50                 if (w)
 51                     f[u].push_back(val);
 52                 else
 53                     f[u].push_front(val);
 54             }
 55             //2
 56             else if (mode==2)
 57             {
 58                 read(u),read(w);
 59                 if (f[u].empty())
 60                     printf("-1\n");
 61                 else
 62                 {
 63                     if (w)
 64                     {
 65                         printf("%d\n",f[u].back());
 66                         f[u].pop_back();
 67                     }
 68                     else
 69                     {
 70                         printf("%d\n",f[u].front());
 71                         f[u].pop_front();
 72                     }
 73                 }
 74             }
 75             //3
 76             else
 77             {
 78                 read(u),read(v),read(w);
 79                 if (w)
 80                     reverse(f[v].begin(),f[v].end());
 81                 f[u].splice(f[u].end(),f[v]);
 82 
 83                 //or
 84 
 85 //                if (w)
 86 //                    f[u].insert(f[u].end(),f[v].rbegin(),f[v].rend());
 87 //                else
 88 //                    f[u].insert(f[u].end(),f[v].begin(),f[v].end());
 89                 f[v].clear();
 90             }
 91         }
 92     }
 93     return 0;
 94 }
 95 /*
 96 2 30
 97 1 1 0 123
 98 1 1 0 1234
 99 1 2 1 2333
100 1 2 1 23333
101 1 2 1 233333
102 1 2 1 2333333
103 1 2 1 23333333
104 2 2 0
105 2 2 1
106 3 1 2 1
107 2 1 1
108 2 1 1
109 2 1 1
110 2 1 1
111 2 1 1
112 2 1 1
113 3 1 5 0
114 1 5 1 1
115 2 5 1
116 */

 

用rope超时了

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <string>
#include <set>
#include <map>
#include <list>
#include <ext/rope>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e9
#define pi 3.1415926536
#define E  2.7182818284
const ll mod=1e9+7;//998244353
const int maxn=150010;
using namespace __gnu_cxx;


void read(int &x){
    char ch = getchar();x = 0;
    for (; ch < '0' || ch > '9'; ch = getchar());
    for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}

rope<int>f[maxn],ff[maxn];


int main()
{
    int n,q,mode,u,w,val,v,i;
    while (~scanf("%d%d",&n,&q))
    {
        for (i=1;i<=n;i++)
            f[i].clear(),ff[i].clear();
        while (q--)
        {
            read(mode);
            if (mode==1)
            {
                read(u),read(w),read(val);
                if (w==1)
                {
                    f[u].push_back(val);
                    ff[u].insert(0,val);
                }
                else
                {
                    f[u].insert(0,val);
                    ff[u].push_back(val);
                }
            }
            else if (mode==2)
            {
                read(u),read(w);
                if (f[u].empty())
                    printf("-1\n");
                else
                {
                    if (w==1)
                    {
                        printf("%d\n",f[u].at(f[u].size()-1));
                        f[u].erase(f[u].size()-1,1);
                        ff[u].erase(0,1);
                    }
                    else
                    {
                        printf("%d\n",f[u].at(0));
                        f[u].erase(0,1);
                        ff[u].erase(f[u].size()-1,1);
                    }
                }
            }
            else
            {
                read(u),read(v),read(w);
                if (w==0)
                {
                    f[u].append(f[v]);
                    ff[u]=ff[v]+ff[u];
                    f[v].clear();
                    ff[v].clear();
                }
                else
                {
                    f[u].append(ff[v]);
                    ff[u]=f[v]+ff[u];
                    f[v].clear();
                    ff[v].clear();
                }
            }
        }
    }
    return 0;
}

 

posted @ 2018-08-22 11:31  congmingyige  阅读(212)  评论(0编辑  收藏  举报