QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#859433 | #9679. 盒子 | APPAwang | 0 | 29ms | 4736kb | C++14 | 3.6kb | 2025-01-17 19:01:06 | 2025-01-17 19:01:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
char buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?0:*p1++)
int read(){int x=0;bool neg=0;char ch=gc();
while(ch<'0'||ch>'9')neg|=(ch=='-'),ch=gc();
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return neg?-x:x;
}//快读
#define ll long long
int n,m,k,c;
ll s[500005];//前缀和
struct P{//无旋treap
int ls,rs,rd;ll key,G,tag;//key就是区间的值之和,G就是节省点费用
}v[500005];
int tot,rt;
inline void pushdown(int x){//加法标记
if(!v[x].tag)return;
v[v[x].ls].tag+=v[x].tag;
v[v[x].rs].tag+=v[x].tag;
v[x].key+=v[x].tag;
v[x].tag=0;
}
int merge(int x,int y){//合并
if(!x||!y)return x+y;
if(v[x].rd>v[y].rd){
pushdown(x);
v[x].rs=merge(v[x].rs,y);
return x;
}
pushdown(y);
v[y].ls=merge(x,v[y].ls);
return y;
}
void split(int x,int &y,int &z,int mid){//分裂为<mid 和 >=mid
if(!x){y=z=0;return;}
pushdown(x);
if(v[x].key%k<mid){
y=x;
split(v[x].rs,v[x].rs,z,mid);
return;
}
z=x;
split(v[x].ls,y,v[x].ls,mid);
}
ll mx;//存储当区间全部清空时的节省的费用
ll bound_down(int x,ll lim){//暴力将大于上限的部分合并为上限,同时处理全部清空的节省费用
if(!x)return 0;
pushdown(x);
mx=max(mx,v[x].G+v[x].key-(v[x].key/k+1)*c);
v[x].G+=v[x].key/k*(k-c);
v[x].key=lim;//修改为上限
v[x].G=max(v[x].G,bound_down(v[x].ls,lim));//从儿子读取节省费用的最大值
v[x].G=max(v[x].G,bound_down(v[x].rs,lim));
v[x].ls=v[x].rs=0;//合并成一个点,不需要儿子
return v[x].G;
}
void zero_up(int x,ll val){//添加或更新 key == 0 的情况
int y=x;
while(v[y].ls)y=v[y].ls;//一直向左
if(v[y].key%k){//如果没有0,就添加一个
v[++tot]=(P){0,0,rand(),0,val,0};
rt=merge(tot,rt);
}else v[y].G=max(val,v[y].G+v[y].key/k*(k-c)),v[y].key=0;//否则取max
}
void print(int x){//调试语句
if(!x)return;
pushdown(x);
print(v[x].ls);
v[x].G+=v[x].key/k*(k-c);
v[x].key%=k;
printf("(%lld %lld) ",v[x].G,v[x].key);
print(v[x].rs);
}
void output(){//调试语句
print(rt);
printf("\n");
}
int main(){//freopen("in","r",stdin);
int T=read(),i,j;
while(T--){
n=read();m=read();k=read();c=read();
for(i=1;i<=n;i++)s[i]=s[i-1]+read();//读取前缀和
tot=rt=1;
v[1]=(P){0,0,rand(),s[m],0,0};//初始化
for(i=m+1;i<=n;i++){//output();
split(rt,rt,j,s[i-1]-s[i-m]);//取出超过上限的部分
if(j){//如果有超出上限的部分
mx=0;//用来存储当区间全部清空时的节省的费用
bound_down(j,s[i-1]-s[i-m]);//合并超出上限的部分
rt=merge(rt,j);//还原回去
if(mx)zero_up(rt,mx);//更新 key == 0 的情况
}
int a=s[i]-s[i-1];//添加新的数
split(rt,rt,j,k-(a-1)%k-1);
v[rt].tag+=a;//加法标记
v[j].tag+=a;
rt=merge(j,rt);//调换顺序
}//output();
mx=0;
bound_down(rt,0);//发现计算结果恰好跟合并上限的做法一样,就用了同一个函数
printf("%lld\n",s[n]-max(mx,v[rt].G));//输出答案,然后wa了
if(s[n]-max(mx,v[rt].G)==3370){
printf("%d %d %d %d, ",n,m,k,c);
for(i=1;i<=n;i++)printf("%d ",s[i]-s[i-1]);
printf("\n");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 17
Accepted
time: 0ms
memory: 3712kb
input:
3 5 2 4 3 2 2 1 2 2 4 2 4 3 2 4 1 1 10 3 5 1 2 2 2 2 1 1 1 10 2 2
output:
7 7 6
result:
ok 3 number(s): "7 7 6"
Test #2:
score: 17
Accepted
time: 1ms
memory: 3840kb
input:
65 7 1 27 22 70 29 32 15 69 79 84 10 2 2 1 76 63 99 67 75 30 29 45 79 23 9 1 4 3 47 91 10 30 91 29 12 14 53 10 1 5 4 92 22 92 27 30 50 59 6 57 58 5 2 15 15 59 27 70 24 11 5 2 42 42 70 50 42 55 5 6 2 54 46 67 14 52 80 95 3 10 2 89 88 55 14 45 14 90 81 38 40 54 17 5 2 93 86 35 58 76 64 73 6 1 45 43 63...
output:
320 293 287 398 191 222 271 445 285 344 307 270 348 312 370 427 199 184 318 502 344 197 330 233 262 220 454 243 160 280 482 580 330 373 202 293 228 590 268 475 253 494 523 476 186 223 368 323 368 392 507 494 132 209 224 250 297 216 525 557 172 448 433 430 578
result:
ok 65 numbers
Test #3:
score: 0
Wrong Answer
time: 0ms
memory: 3840kb
input:
6 72 4 97 91 33 34 16 21 70 62 12 30 49 27 64 63 82 53 69 14 50 52 59 19 72 79 26 86 55 50 41 85 18 8 97 51 30 7 29 43 12 10 19 13 50 60 57 23 23 11 77 23 58 35 17 47 37 21 47 65 66 49 80 51 67 39 62 80 100 59 12 17 15 71 86 97 59 4 101 92 89 100 35 32 57 26 43 81 44 36 78 47 54 28 96 80 55 82 9 95 ...
output:
3114 3115 2543 4767 3370 78 5 177 163, 9 2 4 94 36 40 59 92 6 82 7 10 5 36 40 2 67 46 20 4 88 28 44 1 62 44 30 59 62 100 96 44 45 37 21 88 43 49 45 75 51 76 53 33 5 74 17 19 46 14 71 1 78 38 78 2 51 85 96 25 34 69 49 68 75 89 86 16 42 95 85 30 45 74 72 48 1 17 5050
result:
wrong answer 5th numbers differ - expected: '3371', found: '3370'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #35:
score: 0
Wrong Answer
time: 29ms
memory: 4736kb
input:
66664 7 2 82188055 1 35930054 4923258 36288509 46890418 53350617 49812938 68015568 10 2 460335201 1 305598063 240803174 36008172 416771728 391050572 270293987 333994588 436573185 216917970 103343453 9 3 119910901 1 35106715 29444257 72409421 49339248 23617992 3266647 38704192 75874356 72979434 10 1 ...
output:
5 8 4 13 8 3 8 13 3 4 6 10 8 5 11 13 9 14 5 7 5 11 11 4 3 9 7 4 6 5 6 4 5 12 5 9 3 5 10 12 6 6 14 15 4 7 14 14 7 5 7 6 9 5 3 10 8 8 7 6 7 5 11 6 6 5 6 7 4 9 9 9 6 4 4 5 7 6 6 13 6 10 12 5 4 10 14 7 3 7 5 4 7 9 8 13 4 4 8 10 6 6 6 15 10 15 11 3 4 6 7 5 11 13 6 16 13 8 7 10 7 14 11 7 6 9 10 10 8 4 5 7...
result:
wrong answer 137th numbers differ - expected: '4', found: '-78913679'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%