Description
Input
Output
每组数据输出一行表示答案。
Solution
Explanation
比赛的时候没注意到
S
e
e
d
Seed
Seed的数据类型,然后调了好久😓
这道题观察到如果是扔出去的茶,肯定有两个性质:
1.已经被买过了
2.一定比它后面扔出去的茶先捡回来
我们先考虑只买茶的情况
对于这种情况,我们发现答案肯定是不下降的,所以我们用标记数组维护一下就好了
然后就是扔出去的茶
根据性质2,如果后面扔出来的茶比前面的茶要小,那前面的茶对后面的答案肯定是没贡献了
所以我们可以用单调队列维护
对于捡回来的情况,用个普通队列维护就行了
注意细节
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MO 998244353
#define M 3000001
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
using namespace std;
int t,m,a,b,c,d,i,ma,l,head,tail,ans,tot,ls,p[M],pl[M],qu[M];
bool bz[M],bj[M];
unsigned int seed;
unsigned int randnum()
{
seed=seed^(seed<<13);
seed=seed^(seed>>17);
seed=seed^(seed<<5);
return seed;
}
int main()
{
open("sage");
scanf("%d",&t);
for (;t;t--)
{
scanf("%d%d%d%d%d%d",&m,&seed,&a,&b,&c,&d);
for (i=1;i<=m;i++)
{
if (randnum()%c==0) p[i]=-1;else p[i]=randnum()%b;
}
for (i=0;i<=a;i++)
bz[i]=1;
ma=a+1;l=1;head=1;tail=0;tot=0;
for (i=1;i<=m;i++)
{
if (p[i]==-1)
{
if (d || l>tot) continue;
bj[qu[l]]=0;
if (pl[head]==qu[l] && head<=tail) head++;
l++;
}else
if (!bz[p[i]])
{
bz[p[i]]=1;
while (bz[ma]) ma++;
if (p[i]==pl[head] && head<=tail) head++;
}else
if (!bj[p[i]])
{
if (d==1) continue;
bj[p[i]]=1;
qu[++tot]=p[i];
while (head<=tail && (pl[tail]>=p[i] || !bj[pl[tail]])) tail--;
pl[++tail]=p[i];
}else
{
if (d || l>tot) continue;
bj[qu[l]]=0;
if (pl[head]==qu[l] && head<=tail) head++;
l++;
}
if (head>tail) ls=2147483647;else ls=pl[head];
ans=ans^(1ll*min(ls,ma)*(1ll*(7+i)*i%MO)%MO);
}
printf("%d\n",ans);
ans=0;
memset(bz,0,sizeof(bz));
memset(bj,0,sizeof(bj));
}
return 0;
}