QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#44513#4585. Greedy KnapsackeyiigjknML 2ms7940kbC++142.3kb2022-08-18 18:52:302022-08-18 18:52:33

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-18 18:52:33]
  • 评测
  • 测评结果:ML
  • 用时:2ms
  • 内存:7940kb
  • [2022-08-18 18:52:30]
  • 提交

answer

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int w[100010],v[100010],stk[4000010],lc[4000010],rc[4000010],sz[4000010],tot=0;
ll len[4000010],sum[4000010],val[4000010],maxn[4000010],addmk[4000010];
bool flag[4000010];
mt19937 rnd(0);
int getid(){return stk[0]?stk[stk[0]--]:++tot;}
int newnode(ll L,ll V)
{
	int cur=getid();
	sz[cur]=1;
	addmk[cur]=lc[cur]=rc[cur]=0;
	sum[cur]=len[cur]=L;
	maxn[cur]=val[cur]=V;
	return cur;
}
int copy(int rt)
{
	int cur=getid();
	tie(lc[cur],rc[cur],sz[cur],len[cur],sum[cur],val[cur],maxn[cur],addmk[cur])=make_tuple(lc[rt],rc[rt],sz[rt],len[rt],sum[rt],val[rt],maxn[rt],addmk[rt]);
	return cur;
}
void pushup(int rt)
{
	sz[rt]=sz[lc[rt]]+sz[rc[rt]]+1;
	sum[rt]=sum[lc[rt]]+sum[rc[rt]]+len[rt];
	maxn[rt]=max(val[rt],max(maxn[lc[rt]],maxn[rc[rt]]));
}
void update(int rt,int x){val[rt]+=x;maxn[rt]+=x;addmk[rt]+=x;}
void pushdown(int rt)
{
	if(addmk[rt])
	{
		if(lc[rt]) update(lc[rt]=copy(lc[rt]),addmk[rt]);
		if(rc[rt]) update(rc[rt]=copy(rc[rt]),addmk[rt]);
		addmk[rt]=0;
	}
}
void split(int rt,ll k,int &x,int &y)
{
	if(!rt) return x=y=0,void();
	pushdown(rt);
	if(k<=sum[lc[rt]]) return y=copy(rt),split(lc[rt],k,x,lc[y]),pushup(y);
	else if(k>=sum[lc[rt]]+len[rt]) return x=copy(rt),split(rc[rt],k-sum[lc[rt]]-len[rt],rc[x],y),pushup(x);
	else
	{
		x=newnode(k-sum[lc[rt]],val[rt]);
		y=newnode(len[rt]-len[x],val[rt]);
		lc[x]=lc[rt];rc[y]=rc[rt];
		pushup(x);pushup(y);
	}
}
int merge(int r1,int r2)
{
	if(!r1 || !r2) return r1+r2;
	pushdown(r1);pushdown(r2);
	int rt;
	if(rnd()%(sz[r1]+sz[r2])<sz[r1]) rt=copy(r1),rc[rt]=merge(rc[r1],r2);
	else rt=copy(r2),lc[rt]=merge(r1,lc[r2]);
	pushup(rt);
	return rt;
}
void dfs(int rt)
{
	if(!rt || flag[rt]) return;
	flag[rt]=1;
	dfs(lc[rt]);dfs(rc[rt]);
}
int main()
{
	int n,rt;
	ll m;
	cin>>n>>m;
	for(int i=1;i<=n;i++) scanf("%d",&w[i]);
	for(int i=1;i<=n;i++) scanf("%d",&v[i]);
	rt=newnode(m+1,0);
	for(int i=n;i;i--)
	{
		int a,b,c,d;
		split(rt,w[i],a,b);
		split(rt,m-w[i]+1,c,d);
		if(c) update(c,v[i]);
		rt=merge(a,c);
		if(i%10000==0)
		{
			dfs(rt);
			stk[0]=0;
			for(int j=1;j<=tot;j++)
				if(flag[j]) flag[j]=0;
				else stk[++stk[0]]=j;
		}
	}
	cout<<maxn[rt]<<endl;
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 7792kb

input:

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

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 2ms
memory: 7740kb

input:

5 10000000000
10 1 2 3 4
30 2 15 7 11

output:

65

result:

ok single line: '65'

Test #3:

score: 0
Accepted
time: 0ms
memory: 7940kb

input:

5 20
4 9 5 1 3
203 175 131 218 304

output:

900

result:

ok single line: '900'

Test #4:

score: 0
Accepted
time: 1ms
memory: 7872kb

input:

1 1
1
1

output:

1

result:

ok single line: '1'

Test #5:

score: -100
Memory Limit Exceeded

input:

100000 200000
100000 99998 99996 99994 99992 99990 99988 99986 99984 99982 99980 99978 99976 99974 99972 99970 99968 99966 99964 99962 99960 99958 99956 99954 99952 99950 99948 99946 99944 99942 99940 99938 99936 99934 99932 99930 99928 99926 99924 99922 99920 99918 99916 99914 99912 99910 99908 999...

output:


result: