社区讨论
10pts求调,玄关
B3656【模板】双端队列 1参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mjf6enya
- 此快照首次捕获于
- 2025/12/21 11:37 2 个月前
- 此快照最后确认于
- 2025/12/23 19:40 2 个月前
CPP
#include <cstdio>
#include <cstring>
#define int long long
using namespace std;
const int maxn=1e6+100;
struct node
{
int dt,lp,rp;
}t[maxn];
struct hFlist
{
int head,tail,size=0;
}ml,lt[maxn];
void init_listo()
{
ml.head=1;
t[1].lp=-1;
for(int i=1;i<=1e6+9;++i)
{
t[i].rp=i+1;
t[i+1].lp=i;
}
t[(int)(1e6+10)].rp=-1;
ml.tail=1e6+10;
}
int get_new_nodeo()
{
int r=ml.head;
t[t[ml.head].rp].lp=-1;
ml.head=t[ml.head].rp;
t[r].rp=-1;
t[r].lp=-1;
return r;
}
void put_old_nodeo(int h)
{
t[ml.tail].rp=h;
t[h].lp=ml.tail;
t[h].rp=-1;
ml.tail=h;
}
void push_backo(int a,int x)
{
int h=get_new_nodeo();
t[h].dt=x;
if(lt[a].size)
{
t[lt[a].tail].rp=h;
t[h].lp=lt[a].tail;
lt[a].tail=h;
}
else
{
lt[a].head=lt[a].tail=h;
}
++lt[a].size;
}
void pop_backo(int a)
{
if(lt[a].size>1)
{
int tt=t[lt[a].tail].lp;
t[tt].rp=-1;
put_old_nodeo(lt[a].tail);
lt[a].tail=tt;
--lt[a].size;
}
else if(lt[a].size==1)
{
put_old_nodeo(lt[a].tail);
lt[a].size=0;
lt[a].head=lt[a].tail=-1;
}
}
void push_fronto(int a,int x)
{
int h=get_new_nodeo();
t[h].dt=x;
if(lt[a].size)
{
t[h].rp=lt[a].head;
t[lt[a].head].lp=h;
lt[a].head=h;
}
else
{
lt[a].head=lt[a].tail=h;
}
++lt[a].size;
}
void pop_fronto(int a)
{
if(lt[a].size>1)
{
int tt=t[lt[a].head].lp;
t[tt].lp=-1;
put_old_nodeo(lt[a].head);
lt[a].head=tt;
--lt[a].size;
}
else if(lt[a].size==1)
{
put_old_nodeo(lt[a].head);
lt[a].size=0;
lt[a].head=lt[a].tail=-1;
}
}
int sizeo(int a)
{
return lt[a].size;
}
int fronto(int a)
{
return t[lt[a].head].dt;
}
int backo(int a)
{
return t[lt[a].tail].dt;
}
int q;
char s[100];
int a,x;
signed main()
{
scanf("%lld",&q);
init_listo();
while(q--)
{
scanf("%s",s);
if(!strcmp(s,"push_back"))
{
scanf("%lld%lld",&a,&x);
push_backo(a,x);
}
else if(!strcmp(s,"pop_back"))
{
scanf("%lld",&a);
pop_backo(a);
}
else if(!strcmp(s,"push_front"))
{
scanf("%lld%lld",&a,&x);
push_fronto(a,x);
}
else if(!strcmp(s,"pop_front"))
{
scanf("%lld",&a);
pop_fronto(a);
}
else if(!strcmp(s,"size"))
{
scanf("%lld",&a);
printf("%lld\n",sizeo(a));
}
else if(!strcmp(s,"front"))
{
scanf("%lld",&a);
if(lt[a].size)printf("%lld\n",fronto(a));
}
else if(!strcmp(s,"back"))
{
scanf("%lld",&a);
if(lt[a].size)printf("%lld\n",backo(a));
}
else
{
printf("MEOW!!!!!\n");
}
}
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...