QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#44511 | #4585. Greedy Knapsack | eyiigjkn | TL | 509ms | 55112kb | C++14 | 2.3kb | 2022-08-18 17:37:36 | 2022-08-18 17:37:37 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int w[100010],v[100010],rt[2],lc[5000010],rc[5000010],sz[5000010],cnt=0,tot=0;
ll len[5000010],sum[5000010],val[5000010],addmk[5000010];
pair<ll,ll> a[100010];
mt19937 rnd(0);
int newnode(ll L,ll V)
{
sz[++tot]=1;val[tot]=V;
addmk[tot]=lc[tot]=rc[tot]=0;
sum[tot]=len[tot]=L;
return tot;
}
int copy(int rt)
{
tot++;
tie(lc[tot],rc[tot],sz[tot],len[tot],sum[tot],val[tot],addmk[tot])=make_tuple(lc[rt],rc[rt],sz[rt],len[rt],sum[rt],val[rt],addmk[rt]);
return tot;
}
void pushup(int rt)
{
sz[rt]=sz[lc[rt]]+sz[rc[rt]]+1;
sum[rt]=sum[lc[rt]]+sum[rc[rt]]+len[rt];
}
void update(int rt,int x){val[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) return;
pushdown(rt);
dfs(lc[rt]);
a[++cnt]={len[rt],val[rt]};
dfs(rc[rt]);
}
int build(int l,int r)
{
if(l>r) return 0;
int mid=(l+r)/2,rt=newnode(a[mid].first,a[mid].second);
lc[rt]=build(l,mid-1);
rc[rt]=build(mid+1,r);
pushup(rt);
return rt;
}
int main()
{
int n;
ll m,ans=0;
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[n+1&1]=newnode(m+1,0);
for(int i=n;i;i--)
{
int a,b,c,d;
split(rt[i+1&1],w[i],a,b);
split(rt[i+1&1],m-w[i]+1,c,d);
update(c,v[i]);
rt[i&1]=merge(a,c);
if(i%2000==0)
{
cnt=tot=0;dfs(rt[i&1]);
rt[i&1]=build(1,cnt);
}
}
cnt=0;dfs(rt[1]);
for(int i=1;i<=cnt;i++) ans=max(ans,a[i].second);
cout<<ans<<endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3772kb
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: 3580kb
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: 1ms
memory: 3648kb
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: 3788kb
input:
1 1 1 1
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 509ms
memory: 55112kb
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: -100
Time Limit Exceeded
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...