QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#572589 | #8951. 澡堂 | Kevin5307# | 30 | 293ms | 261356kb | C++23 | 6.0kb | 2024-09-18 15:28:24 | 2024-09-18 15:28:24 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
int n,m,q;
ll T;
ll a[1001000];
ll psum[1001000][5];
ll b[1001000];
ll sum[5];
ll rsum[1001000][5];
ll psum1[1001000][5];
ll pmx[1001000][5];
ll nxt[20][1001000][5];
vector<pii> vq[1001000];
ll Answer[1001000];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q>>T;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=a[i]+T*n-T*(i/m);
}
for(int i=1;i<=n+666;i++)
for(int j=0;j<m;j++)
psum[i][j]=psum[i-1][j]+(i%m==j?b[i]:0);
vector<pair<ll,int>> vec[5];
for(int i=n;i>=1;i--)
{
ll val=b[i];
int p=i%m;
int tot=1;
while(sz(vec[p])&&vec[p].back().first<=val)
{
sum[p]-=vec[p].back().first*vec[p].back().second;
tot+=vec[p].back().second;
vec[p].pop_back();
}
vec[p].pb(val,tot);
sum[p]+=val*tot;
for(int j=0;j<m;j++)
rsum[i][j]=sum[j];
}
ll mx[5];
memset(mx,0,sizeof(mx));
for(int i=1;i<=n;i++)
{
for(int j=0;j<5;j++)
psum1[i][j]=psum1[i-1][j]+(i%m==j?max(0ll,mx[j]-b[i]):0);
mx[i%m]=max(mx[i%m],b[i]);
for(int j=0;j<5;j++)
pmx[i][j]=mx[j];
}
for(int i=1;i<=n;i++)
for(int j=0;j<m;j++)
nxt[0][i][j]=(i%m==j?b[i]:0);
for(int j=0;j<m;j++)
for(int x=1;x<20;x++)
for(int i=1;i<=n;i++)
if(i+(1<<(x-1))>n)
nxt[x][i][j]=nxt[x-1][i][j];
else
nxt[x][i][j]=max(nxt[x-1][i][j],nxt[x-1][i+(1<<x-1)][j]);
for(int ind=1;ind<=q;ind++)
{
int p;
ll v;
cin>>p>>v;
int p2=lower_bound(a+1,a+n+1,v)-a;
if(p==p2||p+1==p2)
{
p2--;
ll ans=0;
for(int i=0;i<m;i++)
if(i!=p%m)
ans+=psum1[n][i];
else
{
ans+=psum1[p-1][i];
ll v2=v+T*n-T*(p/m);
ans+=max(0ll,pmx[p-1][i]-v2);
ll cmx=max(pmx[p-1][i],v2);
int cur=p+1;
for(int j=19;j>=0&&cur<=n;j--)
if(nxt[j][cur][i]<=cmx)
cur+=(1<<j);
cur--;
cur=min(cur,n);
while(cur%m!=i&&cur>=0) cur--;
if(cur>p)
ans+=cmx*(cur-p)/m+rsum[cur+1][i];
else
ans+=rsum[p+1][i];
ans-=psum[n][i]-psum[p][i];
}
Answer[ind]=ans;
}
else if(p<p2)
{
p2--;
ll ans=0;
ll nb=v+T*n-T*(p2/m);
for(int i=0;i<m;i++)
{
ans+=psum1[p-1][i];
int i2=(i+1)%m;
ll cmx=pmx[p-1][i];
if(!i2) cmx-=T;
int cur=p+1;
for(int j=19;j>=0&&cur<=p2;j--)
if(nxt[j][cur][i2]<=cmx)
cur+=(1<<j);
cur--;
cur=min(cur,p2);
while(cur%m!=i2&&cur>=0) cur--;
int nxt=p+1;
while(nxt%m!=i2) nxt++;
if(cur>=nxt)
{
ans+=cmx*(cur-nxt+m)/m;
vq[cur+m].pb(p2,ind);
}
else
vq[nxt].pb(p2,ind);
ans-=psum[p2][i2]-psum[p][i2];
int x=p+1,len=p2-p;
for(int j=0;j<20;j++) if(len>>j&1)
{
cmx=max(cmx,::nxt[j][x][i2]);
x+=(1<<j);
}
if(!i2) cmx+=T;
if(p2%m==i)
{
ans+=max(0ll,cmx-nb);
cmx=max(cmx,nb);
}
cur=p2+1;
for(int j=19;j>=0&&cur<=n;j--)
if(::nxt[j][cur][i]<=cmx)
cur+=(1<<j);
cur--;
cur=min(cur,n);
nxt=p2+1;
while(nxt%m!=i) nxt++;
while(cur%m!=i&&cur>=0) cur--;
if(cur>=nxt)
ans+=cmx*(cur-nxt+m)/m+rsum[cur+1][i];
else
ans+=rsum[nxt][i];
ans-=psum[n][i]-psum[nxt-1][i];
}
Answer[ind]=ans;
}
else
{
ll ans=0;
ll nb=v+T*n-T*(p2/m);
for(int i=0;i<m;i++)
{
ans+=psum1[p2-1][i];
int i2=(i+m-1)%m;
ll cmx=pmx[p2-1][i];
int cur=p2;
if(p2%m==i)
{
ans+=max(0ll,cmx-nb);
cmx=max(cmx,nb);
}
if(!i) cmx+=T;
for(int j=19;j>=0&&cur<p;j--)
if(nxt[j][cur][i2]<=cmx)
cur+=(1<<j);
cur--;
cur=min(cur,p-1);
while(cur%m!=i2&&cur>=0) cur--;
int nxt=p2;
while(nxt%m!=i2) nxt++;
if(cur>=nxt)
{
ans+=cmx*(cur-nxt+m)/m;
vq[cur+m].pb(p-1,ind);
}
else
vq[nxt].pb(p-1,ind);
ans-=psum[p-1][i2]-psum[p2-1][i2];
int x=p2,len=p-p2;
for(int j=0;j<20;j++) if(len>>j&1)
{
cmx=max(cmx,::nxt[j][x][i2]);
x+=(1<<j);
}
if(!i) cmx-=T;
cur=p+1;
for(int j=19;j>=0&&cur<=n;j--)
if(::nxt[j][cur][i]<=cmx)
cur+=(1<<j);
cur--;
cur=min(cur,n);
nxt=p+1;
while(nxt%m!=i) nxt++;
while(cur%m!=i) cur--;
if(cur>=nxt)
ans+=cmx*(cur-nxt+m)/m+rsum[cur+1][i];
else
ans+=rsum[nxt][i];
ans-=psum[n][i]-psum[nxt-1][i];
}
Answer[ind]=ans;
}
}
for(int i=0;i<5;i++)
vec[i].clear();
vector<int> vc[5];
vector<ll> vs[5];
for(int i=0;i<5;i++)
{
vc[i].pb(0);
vs[i].pb(0);
}
for(int i=n;i>=1;i--)
{
ll val=b[i];
int p=i%m;
int tot=1;
while(sz(vec[p])&&vec[p].back().first<=val)
{
sum[p]-=vec[p].back().first*vec[p].back().second;
tot+=vec[p].back().second;
vec[p].pop_back();
vs[p].pop_back();
vc[p].pop_back();
}
vec[p].pb(val,tot);
vc[p].pb(vc[p].back()+tot);
vs[p].pb(vs[p].back()+val*tot);
sum[p]+=val*tot;
for(int j=0;j<m;j++)
rsum[i][j]=sum[j];
for(auto pr:vq[i])
{
if(pr.first<i) continue;
int cnt=(pr.first-i)/m+1;
int p=i%m;
int L=0,R=sz(vec[p]);
while(L<R)
{
int mid=(L+R)/2;
if(vc[p].back()-vc[p][mid]<=cnt)
R=mid;
else
L=mid+1;
}
Answer[pr.second]+=vs[p].back()-vs[p][L];
cnt-=vc[p].back()-vc[p][L];
if(L)
Answer[pr.second]+=vec[p][L-1].first*cnt;
}
}
for(int i=1;i<=q;i++)
cout<<Answer[i]<<'\n';
return 0;
}
详细
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 0ms
memory: 55072kb
input:
1000 5 1000 1969617 59993 133807 154199 240894 438118 483699 540258 892751 922552 1049554 1092961 1153394 1287745 1315664 1372758 1550447 1634967 1817853 1825455 2023239 2064188 2117896 2250036 2453036 2492474 2794776 2917935 3047993 3153958 3163156 3237767 3443614 3664074 3716235 3801008 3822067 40...
output:
145473107761 145400375427 145403718605 145444938654 145438908460 145436948885 145405212826 145454120934 145460664260 145458475414 145433391640 145459823384 145401672860 145361079139 145486629489 145451768180 145426177956 145368877047 145384484360 145392373415 145514759762 145437334277 145471143810 1...
result:
ok 1000 lines
Test #2:
score: 10
Accepted
time: 3ms
memory: 55092kb
input:
1000 5 1000 36167 8215 12748 13176 18886 28740 44382 48915 49343 55053 64907 80549 85082 85510 91220 101074 116716 121249 121677 127387 137241 152883 157416 157844 163554 173408 189050 193583 194011 199721 209575 225217 229750 230178 235888 245742 261384 265917 266345 272055 281909 297551 302084 302...
output:
5383542 4273384 5583092 4487507 4540778 5739420 4991040 4831250 8044764 4747488 4297386 7059595 4985536 7791557 4450596 5708031 4408748 5511682 3977461 5126955 4570217 3960587 6060994 4619639 6617992 5548191 4048923 4200581 5648172 4915001 6247217 3840511 5690071 5159446 3355278 5730086 4592285 4686...
result:
ok 1000 lines
Test #3:
score: 10
Accepted
time: 4ms
memory: 52976kb
input:
1000 5 1000 26455 5755 9335 12488 13480 16017 32210 35790 38943 39935 42472 58665 62245 65398 66390 68927 85120 88700 91853 92845 95382 111575 115155 118308 119300 121837 138030 141610 144763 145755 148292 164485 168065 171218 172210 174747 190940 194520 197673 198665 201202 217395 220975 224128 225...
output:
3759363 2722461 1547992 1048857 2015166 2141100 1365260 3621432 2012670 2816278 953073 910376 959437 3872474 2174732 3202845 2220674 3598113 1366417 1009053 896353 856859 1073992 4270759 2501724 1112987 1322084 1846494 2453774 3339534 1661853 1633223 3031189 4018416 1709729 4499620 1118329 3768062 3...
result:
ok 1000 lines
Test #4:
score: 10
Accepted
time: 3ms
memory: 52924kb
input:
1000 5 1000 16722 3139 3641 7653 10814 14321 19860 20362 24374 27535 31042 36581 37083 41095 44256 47763 53302 53804 57816 60977 64484 70023 70525 74537 77698 81205 86744 87246 91258 94419 97926 103465 103967 107979 111140 114647 120186 120688 124700 127861 131368 136907 137409 141421 144582 148089 ...
output:
3512079 3034475 2734835 1941064 2519290 2440084 3414880 4562356 3334182 4126409 4618632 2050759 3791748 2933951 2726789 4788327 4132574 2963481 3636642 3025088 2699802 2034322 3616015 3451068 3111762 2892910 2940932 3444821 3086673 2603997 2440367 2753422 3618666 2391844 3572522 2602010 3080548 3836...
result:
ok 1000 lines
Test #5:
score: 10
Accepted
time: 3ms
memory: 55248kb
input:
1000 5 1000 60555 14667 19173 23801 42385 55686 75221 79727 84355 102939 116240 135775 140281 144909 163493 176794 196329 200835 205463 224047 237348 256883 261389 266017 284601 297902 317437 321943 326571 345155 358456 377991 382497 387125 405709 419010 438545 443051 447679 466263 479564 499099 503...
output:
10089512 17345531 7817544 13187454 10823659 10325209 13800175 11316499 12145244 12378466 11235075 13238408 18390405 9571104 15996985 7081670 14198216 16953216 10752423 8494916 17169941 7074729 9619075 16441956 14197611 15099449 9910334 12491708 13648701 9675595 10825715 9884233 13371337 15375812 824...
result:
ok 1000 lines
Subtask #2:
score: 0
Memory Limit Exceeded
Test #6:
score: 0
Memory Limit Exceeded
input:
1000000 1 100000 1165 83 197 266 277 299 529 538 601 629 760 792 1081 1208 1211 1387 1726 1873 1932 2198 2437 2448 2584 2715 2813 3113 3169 3207 3386 3934 4013 4032 4057 4137 4213 4396 4666 4754 4786 4856 4917 4966 5054 5124 5151 5196 5505 5548 5583 5730 5763 6031 6161 6169 6372 6446 6517 6712 6744 ...
output:
532526465394304 532526382684731 532526432382169 532526335149396 532526336776485 532526441654485 532526419072018 532526448365904 532526461590885 532526446716107 532526451559614 532526448428416 532526467783803 532526436578785 532526455446447 532526418608776 532526437618228 532526421765463 532526361938...
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #26:
score: 20
Accepted
time: 213ms
memory: 253492kb
input:
200000 5 50000 8448 68 1932 1956 2079 2363 3189 3837 3927 4312 4516 4526 4753 6972 7443 8556 9446 10246 11909 12189 12533 12576 12675 12739 13333 14022 14534 14827 15649 15946 16794 17011 17202 18405 18566 18855 19263 19294 19432 19934 19935 20100 20117 21221 21435 21437 21790 21956 21978 23322 2500...
output:
23828443987602 23828386511525 23828440401453 23828432234782 23828417976119 23828465058207 23828422731188 23828448201551 23828420115069 23828364736773 23828478665752 23828417536212 23828505579302 23828462228237 23828437377836 23828428117390 23828456395176 23828446664885 23828487887278 23828449221903 ...
result:
ok 50000 lines
Test #27:
score: 20
Accepted
time: 215ms
memory: 254440kb
input:
200000 5 50000 8590 103 339 774 821 885 1011 1294 1351 3074 3230 3276 3941 4769 5185 5714 5941 6902 7574 8367 8449 8453 8977 9283 9381 9590 11468 11475 11733 12486 12586 13358 13800 14253 14580 14898 14963 15102 15578 15778 15860 16031 17047 18309 19101 19305 19724 20350 21158 22308 22567 22753 2291...
output:
24370390134431 24370391478289 24370319068102 24370435538692 24370392513002 24370501014902 24370449353937 24370384613440 24370401980945 24370365908891 24370455313241 24370458704500 24370395134637 24370422453218 24370387954135 24370398160446 24370407113413 24370486026795 24370431476353 24370493872347 ...
result:
ok 50000 lines
Test #28:
score: 20
Accepted
time: 272ms
memory: 257260kb
input:
200000 5 50000 300 7 103 134 265 277 307 403 434 565 577 607 703 734 865 877 907 1003 1034 1165 1177 1207 1303 1334 1465 1477 1507 1603 1634 1765 1777 1807 1903 1934 2065 2077 2107 2203 2234 2365 2377 2407 2503 2534 2665 2677 2707 2803 2834 2965 2977 3007 3103 3134 3265 3277 3307 3403 3434 3565 3577...
output:
7618159 6959190 7090433 8125788 6101118 7691773 8851270 8154353 6589016 6115375 4293949 4130509 11585838 8622133 8341444 9619199 5887703 10764402 7441102 7601798 8958738 9467404 7126855 8194190 5727979 7943791 7012656 6333046 7865353 10550702 9765510 8251483 8273144 7402104 6001689 6295155 9026334 1...
result:
ok 50000 lines
Test #29:
score: 20
Accepted
time: 250ms
memory: 260308kb
input:
200000 5 50000 271 80 81 95 98 259 351 352 366 369 530 622 623 637 640 801 893 894 908 911 1072 1164 1165 1179 1182 1343 1435 1436 1450 1453 1614 1706 1707 1721 1724 1885 1977 1978 1992 1995 2156 2248 2249 2263 2266 2427 2519 2520 2534 2537 2698 2790 2791 2805 2808 2969 3061 3062 3076 3079 3240 3332...
output:
6158800 4403917 6594229 8772910 5361663 7443680 6358965 4652800 6034889 8584269 4991034 6987148 5192303 4875986 3465783 3981794 7334766 6526726 7423999 4430269 2839826 7152733 5204361 5525359 4344863 7396518 6017257 8644706 10004197 5672661 4637212 4806956 3928956 4594280 9818309 7682171 4790132 815...
result:
ok 50000 lines
Test #30:
score: 20
Accepted
time: 246ms
memory: 257732kb
input:
200000 5 50000 346 83 164 211 214 345 429 510 557 560 691 775 856 903 906 1037 1121 1202 1249 1252 1383 1467 1548 1595 1598 1729 1813 1894 1941 1944 2075 2159 2240 2287 2290 2421 2505 2586 2633 2636 2767 2851 2932 2979 2982 3113 3197 3278 3325 3328 3459 3543 3624 3671 3674 3805 3889 3970 4017 4020 4...
output:
22269407 12411682 14129630 16488349 10420562 13104102 11436945 19368031 11390721 15559467 12401765 21540932 21946906 18620750 14867132 23177928 16802804 18897764 17960026 20389811 15892554 17289376 12963156 13594568 17134998 14165135 17103054 13008327 14966895 13310379 15162394 16001699 13495521 181...
result:
ok 50000 lines
Test #31:
score: 20
Accepted
time: 222ms
memory: 256500kb
input:
200000 5 50000 131 30 30 33 97 103 161 161 164 228 234 292 292 295 359 365 423 423 426 490 496 554 554 557 621 627 685 685 688 752 758 816 816 819 883 889 947 947 950 1014 1020 1078 1078 1081 1145 1151 1209 1209 1212 1276 1282 1340 1340 1343 1407 1413 1471 1471 1474 1538 1544 1602 1602 1605 1669 167...
output:
2947071 3851685 5122334 4795621 6158109 3455051 3283654 2587446 3502892 6342975 4006931 3851585 3707221 4258767 1877588 4920339 2437732 5214888 3817546 4859471 3473573 3773372 4434501 5306512 4241391 2455209 4950931 2948723 2401972 3863424 2654143 2463789 2750967 3020336 2045101 6465779 2852230 2733...
result:
ok 50000 lines
Test #32:
score: 20
Accepted
time: 195ms
memory: 256500kb
input:
200000 5 50000 25 7 9 9 13 24 31 33 33 37 48 55 57 57 61 72 79 81 81 85 96 103 105 105 109 120 127 129 129 133 144 151 153 153 157 168 175 177 177 181 192 199 201 201 205 216 223 225 225 229 240 247 249 249 253 264 271 273 273 277 288 295 297 297 301 312 319 321 321 325 336 343 345 345 349 360 367 3...
output:
3999514580 3999622073 4000180715 3999735158 4000367268 3999778694 3999336483 3999738178 4000453254 3999920464 3999775478 3999244772 4000033418 3999892647 3999696457 4000048702 4000531119 3999480237 4000129161 3999926833 4000446643 3999772033 4000108207 4000041753 4000108438 4000024618 3999322076 400...
result:
ok 50000 lines
Test #33:
score: 20
Accepted
time: 211ms
memory: 259932kb
input:
200000 5 50000 462 27 196 208 224 427 488 657 669 685 888 949 1118 1130 1146 1349 1410 1579 1591 1607 1810 1871 2040 2052 2068 2271 2332 2501 2513 2529 2732 2793 2962 2974 2990 3193 3254 3423 3435 3451 3654 3715 3884 3896 3912 4115 4176 4345 4357 4373 4576 4637 4806 4818 4834 5037 5098 5267 5279 529...
output:
4002921454 4002761236 4000457885 4003278138 4012506884 3996745327 3994815416 4005200604 4001303989 4001112534 4008830198 4012717394 3998202264 3997931680 4010706331 3994413584 4011612065 4000194673 3991150929 4003394025 3989256774 3995215683 3994198220 4008049849 4002819752 4001522591 4010161812 400...
result:
ok 50000 lines
Test #34:
score: 20
Accepted
time: 210ms
memory: 257200kb
input:
200000 5 50000 423 77 155 225 399 407 499 577 647 821 829 921 999 1069 1243 1251 1343 1421 1491 1665 1673 1765 1843 1913 2087 2095 2187 2265 2335 2509 2517 2609 2687 2757 2931 2939 3031 3109 3179 3353 3361 3453 3531 3601 3775 3783 3875 3953 4023 4197 4205 4297 4375 4445 4619 4627 4719 4797 4867 5041...
output:
4001224153 4001887328 4006402687 3991585867 3997961117 4004174670 3993958904 3993508205 3996766718 3999026514 3993408671 3999158934 3988574697 3993575082 4000979106 3997004833 4009262490 3993662452 4002966670 4001296345 4007506300 3987394875 4011373334 4008024865 4004677527 4003493493 4002573044 400...
result:
ok 50000 lines
Test #35:
score: 20
Accepted
time: 293ms
memory: 261356kb
input:
200000 5 50000 34 19 45 51 58 91 117 143 149 156 189 215 241 247 254 287 313 339 345 352 385 411 437 443 450 483 509 535 541 548 581 607 633 639 646 679 705 731 737 744 777 803 829 835 842 875 901 927 933 940 973 999 1025 1031 1038 1071 1097 1123 1129 1136 1169 1195 1221 1227 1234 1267 1293 1319 132...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 50000 lines
Subtask #5:
score: 0
Memory Limit Exceeded
Test #36:
score: 0
Memory Limit Exceeded
input:
1000000 5 100000 1887 112 168 223 393 535 558 577 631 631 678 682 1043 1099 1268 1337 1396 1437 1472 1510 1587 1804 1984 2013 2290 2294 2392 2474 2547 2694 2717 2742 2841 2880 2906 2985 3030 3047 3064 3108 3275 3375 3440 3451 3488 3950 3957 3963 4047 4086 4335 4378 4448 4490 4544 4622 4864 4875 4897...
output:
138785143191754 138785158412509 138785225283652 138785104800924 138785118976960 138785112934741 138785059838322 138785084952748 138785129256978 138785072292366 138785090484906 138785174307446 138785204546881 138785189045551 138785131284055 138785130779155 138785104455009 138785163776264 138785127154...
result:
Subtask #6:
score: 0
Skipped
Dependency #3:
0%