QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#550980#9252. Penguins in Refrigeratorucup-team3510#WA 62ms39060kbC++203.4kb2024-09-07 14:57:162024-09-07 14:57:17

Judging History

你现在查看的是最新测评结果

  • [2024-09-07 14:57:17]
  • 评测
  • 测评结果:WA
  • 用时:62ms
  • 内存:39060kb
  • [2024-09-07 14:57:16]
  • 提交

answer

#include <bits/stdc++.h>
#define N 1000011
#define uint unsigned int
#define pii pair<int,int>
#define s1 first
#define s2 second
using namespace std;
const int p=1e9+7;
int n,W;
struct pen{int p,w,id;}a[N];
mt19937 rnd(801);
int v[N],lc[N],rc[N],siz[N],mx[N],sz;uint R[N];
void pushup(int x)
{
	siz[x]=siz[lc[x]]+siz[rc[x]]+1;
	mx[x]=max({mx[lc[x]],mx[rc[x]],v[x]});
}
void split(int x,int V,int &a,int &b)
{
	if(!x){a=b=0;return;}
	if(mx[lc[x]]>V||v[x]>V)b=x,split(lc[x],V,a,lc[x]);
	else a=x,split(rc[x],V,rc[x],b);pushup(x);
}
int merge(int a,int b)
{
	if(!a||!b)return a^b;
	if(R[a]<R[b]){rc[a]=merge(rc[a],b);pushup(a);return a;}
	lc[b]=merge(a,lc[b]);pushup(b);return b;
}
int make(int V){++sz;v[sz]=mx[sz]=V;R[sz]=rnd();lc[sz]=rc[sz]=0;siz[sz]=1;return sz;}
int fac[N],ifac[N];
namespace sgt
{
	pii mn[N*4],mx[N*4];
	void pushup(int x){mn[x]=min(mn[x<<1],mn[x<<1|1]);mx[x]=max(mx[x<<1],mx[x<<1|1]);}
	void build(int L,int R,int x)
	{
		if(L==R)
		{
			mn[x]=pii(a[L].w,L);
			mx[x]=pii(a[L].w,L);
			return;
		}
		build(L,L+R>>1,x<<1);build((L+R>>1)+1,R,x<<1|1);pushup(x);
	}
	pii qmax(int l,int r,int L,int R,int x)
	{
		if(l>r)return pii(-1e9,-1e9);
		if(l<=L&&R<=r)return mx[x];pii ans(-1e9,-1e9);
		if(l<=L+R>>1)ans=max(ans,qmax(l,r,L,L+R>>1,x<<1));if(r>L+R>>1)ans=max(ans,qmax(l,r,(L+R>>1)+1,R,x<<1|1));return ans;
	}
	pii qmin(int l,int r,int L,int R,int x)
	{
		if(l>r)return pii(1e9,1e9);
		if(l<=L&&R<=r)return mn[x];pii ans(1e9,1e9);
		if(l<=L+R>>1)ans=min(ans,qmin(l,r,L,L+R>>1,x<<1));if(r>L+R>>1)ans=min(ans,qmin(l,r,(L+R>>1)+1,R,x<<1|1));return ans;
	}
	void modi(int k,int L,int R,int x)
	{
		if(L==R){mn[x]=pii(1e9,1e9);mx[x]=pii(-1e9,-1e9);return;}
		if(k<=L+R>>1)modi(k,L,L+R>>1,x<<1);else modi(k,(L+R>>1)+1,R,x<<1|1);pushup(x);
	}
};
void ins(int &x,int p)
{
	int P,Q;
	split(x,p,P,Q);
	x=merge(P,merge(make(p),Q));
}
pair<int,int> solve(int L,int R)
{
	auto tt=sgt::qmax(L,R,1,n,1);
	if(tt.s1<0)return pii(1,0);
	int mxi=tt.s2,mni;
	int fr=0;bool all=0;
	vector<int> vp;
	// printf("==================proc[%d,%d]\n",L,R);
	while(1)
	{
		int mni=sgt::qmin(L,R,1,n,1).s2;
		if(mni==mxi){++fr;vp.push_back(a[mni].p);all=1;break;}
		if(a[mni].w+a[mxi].w>W)break;
		// printf("mark %d as free\n",mni);
		sgt::modi(mni,1,n,1);
		++fr;vp.push_back(a[mni].p);
	}
	if(all)
	{
		pii ans;
		ans.s1=fac[fr];
		sort(vp.begin(),vp.end());
		for(int p:vp)ans.s2=merge(ans.s2,make(p));
		return ans;
	}
	else
	{
		pii lc=solve(L,mxi-1),rc=solve(mxi+1,R);
		// printf("[%d,%d] mxi:%d\n",L,R,mxi);
		// printf("lc:%d rc:%d\n",lc.s1,rc.s1);
		pii ans(1,0);
		ans.s1=1ll*lc.s1*rc.s1%p;
		int tot=siz[lc.s2]+siz[rc.s2]+1+vp.size();
		ans.s1=1ll*ans.s1*fac[tot]%p*ifac[tot-vp.size()]%p;
		ans.s2=merge(lc.s2,merge(make(a[mxi].p),rc.s2));
		for(int p:vp)ins(ans.s2,p);
		return ans;
	}
}
void dfs(int u)
{
	if(!u)return;
	dfs(lc[u]);
	printf("%d ",v[u]);
	dfs(rc[u]);
}
inline int qpow(int bs,int ex){int ans=1;while(ex){if(ex&1)ans=1ll*ans*bs%p;ex>>=1;bs=1ll*bs*bs%p;}return ans;}
int main()
{
	fac[0]=1;for(int i=1;i<N;++i)fac[i]=1ll*fac[i-1]*i%p;
	ifac[N-1]=qpow(fac[N-1],p-2);for(int i=N-2;~i;--i)ifac[i]=1ll*ifac[i+1]*(i+1)%p;
	scanf("%d%d",&n,&W);
	for(int i=1;i<=n;++i)scanf("%d",&a[n-i+1].p);
	for(int i=1;i<=n;++i)scanf("%d",&a[n-i+1].w);
	sgt::build(1,n,1);
	pii ans=solve(1,n);
	printf("%d\n",ans.s1);
	dfs(ans.s2);putchar(10);
	fclose(stdin);fclose(stdout);return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 8ms
memory: 26588kb

input:

5 10
1 2 3 4 5
6 5 3 9 2

output:

3
5 4 2 1 3 

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 9ms
memory: 24288kb

input:

5 10
1 2 3 4 5
2 4 3 3 8

output:

30
1 5 2 3 4 

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 6ms
memory: 26400kb

input:

5 10
1 2 3 4 5
2 3 4 5 1

output:

120
1 2 3 4 5 

result:

ok 2 lines

Test #4:

score: 0
Accepted
time: 8ms
memory: 26596kb

input:

5 10
1 2 3 4 5
2 3 4 5 6

output:

60
1 2 3 5 4 

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 57ms
memory: 32736kb

input:

100000 96
1996 78922 45321 68844 32404 82013 66552 81163 17216 48170 35495 56660 13480 43118 23173 47257 50168 87069 26167 67231 31758 25694 61063 56642 8923 7727 54528 96554 38964 7604 6822 16256 45300 58869 31359 48638 87645 14779 81505 59585 89293 9291 7002 31810 84701 77648 78295 42595 11394 479...

output:

457992974
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

result:

ok 2 lines

Test #6:

score: -100
Wrong Answer
time: 62ms
memory: 39060kb

input:

100000 84
93330 3894 94859 22134 49668 30606 26739 82976 76701 56323 75537 7626 87226 20857 98177 21811 70827 75898 8111 48223 26186 64222 63002 79024 19126 41638 1048 43857 25379 19764 60207 27675 77665 66327 6274 34861 30287 13449 64505 51490 5804 65843 49014 85795 12365 31565 34411 71697 66568 28...

output:

778158997
1146 2481 2800 8588 14089 15421 17476 20123 27661 31659 32947 33622 35785 36013 43919 59690 56945 4491 18825 43842 39523 62369 74728 78078 82792 88414 89122 28520 41413 24857 87721 94788 98143 80586 69064 29028 30999 34715 36175 54502 75343 88502 26125 1723 22878 70160 71878 76546 77142 91...

result:

wrong answer 1st lines differ - expected: '524727018', found: '778158997'