QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#44517#4585. Greedy KnapsackeyiigjknTL 860ms227184kbC++142.3kb2022-08-18 19:09:002022-08-18 19:09:03

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 19:09:03]
  • 评测
  • 测评结果:TL
  • 用时:860ms
  • 内存:227184kb
  • [2022-08-18 19:09:00]
  • 提交

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],cnt=0,tot=0;
ll len[4000010],sum[4000010],val[4000010],maxn[4000010],addmk[4000010];
bool flag[4000010];
mt19937 rnd(0);
int getid(){cnt++;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(cnt>3999000)
		{
			dfs(rt);
			cnt=stk[0]=0;
			for(int j=1;j<=tot;j++)
				if(flag[j]) flag[j]=0,cnt++;
				else stk[++stk[0]]=j;
		}
	}
	cout<<maxn[rt]<<endl;
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 5828kb

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: 5816kb

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: 5748kb

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: 2ms
memory: 5832kb

input:

1 1
1
1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 860ms
memory: 227184kb

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:

100002

result:

ok single line: '100002'

Test #6:

score: 0
Accepted
time: 626ms
memory: 224068kb

input:

92455 883403359
82193 96168 42737 25557 5699 8922 41136 82789 26249 74241 57030 29989 41743 78491 37281 60598 23165 51802 13911 88911 55220 25398 60154 2879 14519 61138 16806 15952 83710 44076 43551 41425 11055 3321 59105 34722 60133 13735 36785 73444 92250 3613 35787 10798 35612 9564 42531 49012 83...

output:

881743221

result:

ok single line: '881743221'

Test #7:

score: 0
Accepted
time: 601ms
memory: 225968kb

input:

95787 282807627
71790 93372 34507 85540 25871 70820 42496 67633 50879 43192 861 57228 54094 91534 94590 24032 52665 82236 52010 45017 15045 76804 39159 72380 28162 89590 31125 21390 37395 34002 26585 2985 90847 12060 63128 73061 34524 61847 69734 53329 39554 90867 13357 18202 63266 44796 64191 40066...

output:

287223570

result:

ok single line: '287223570'

Test #8:

score: 0
Accepted
time: 663ms
memory: 227080kb

input:

100000 100001
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:

100001

result:

ok single line: '100001'

Test #9:

score: -100
Time 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: