ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#1057 | #211. 「CSP-S2019」划分 | lzx | 100 | 3133ms | 928336kb | C++14 | 1.6kb | 2025-07-08 08:26:32 | 2025-07-08 08:26:34 |
answer
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define fi first
#define se second
using namespace std;
const int N=4e7+1;
const int M=1e5+1;
const int inf=0x3f3f3f3f;
const int mod=(1<<30)-1;
/*
首先想到暴力dp
然后考虑说,段数越多显然更小
发现在所有决策点中,最大的一定最优
一定有决策单调性
所以可以用单调队列优化
为了卡空间,可以只记决策点
*/
int n,type;
ll a[N],b[N];
int f[N];
int q[N],l=1,r;
__int128 ans;
void write(__int128 x)
{
if(x>=10) write(x/10);
putchar('0'+x%10);
}
int p[M];
ll L[M],R[M];
signed main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin>>n>>type;
if(type)
{
int x,y,z,m;
cin>>x>>y>>z>>b[1]>>b[2]>>m;
for(int i=1;i<=m;i++) cin>>p[i]>>L[i]>>R[i];
for(int i=1;i<=m;i++)
{
for(int j=p[i-1]+1;j<=p[i];j++)
{
if(j>2) b[j]=(x*b[j-1]+y*b[j-2]+z)&mod;
a[j]=b[j]%(R[i]-L[i]+1)+L[i];
}
}
}
else for(int i=1;i<=n;i++) cin>>a[i];
for(int i=2;i<=n;i++) a[i]+=a[i-1];
q[++r]=0;
for(int i=1;i<=n;i++)
{
while(r-l+1>=2&&a[i]-a[q[l+1]]>=a[q[l+1]]-a[f[q[l+1]]]) l++;
f[i]=q[l];
while(r>=l&&a[i]-a[f[i]]+a[i]<=a[q[r]]-a[f[q[r]]]+a[q[r]]) r--;//如果不如i优,那就弹出
q[++r]=i;
}
int pos=n;
while(pos)
{
ans=ans+(__int128)(a[pos]-a[f[pos]])*(__int128)(a[pos]-a[f[pos]]);
pos=f[pos];
}
write(ans);
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 4
Accepted
time: 1ms
memory: 7400kb
input:
1 0 1
output:
1
result:
ok "1"
Test #2:
score: 4
Accepted
time: 0ms
memory: 7280kb
input:
10 0 5 1 2 1 1 1 1 1 2 3
output:
108
result:
ok "108"
Test #3:
score: 4
Accepted
time: 2ms
memory: 7172kb
input:
10 0 5 1 1 4 3 2 1 6 5 6
output:
254
result:
ok "254"
Test #4:
score: 4
Accepted
time: 0ms
memory: 7284kb
input:
50 0 488 19 20 6 7 8 9 10 11 171 172 173 174 64 64 64 64 64 64 64 64 64 64 64 64 218 219 220 221 222...
output:
4485725
result:
ok "4485725"
Test #5:
score: 4
Accepted
time: 2ms
memory: 7388kb
input:
50 0 657 83 80 81 86 60 84 85 63 64 61 335 13 14 6 7 6 7 6 7 6 7 6 17 17 17 17 17 135 136 130 131 10...
output:
3963358
result:
ok "3963358"
Test #6:
score: 4
Accepted
time: 0ms
memory: 7400kb
input:
50 0 538 3 4 3 4 3 4 3 128 89 90 131 72 79 76 71 84 81 82 73 76 83 74 83 82 71 72 71 74 81 8 3 2 13 ...
output:
3376710
result:
ok "3376710"
Test #7:
score: 4
Accepted
time: 0ms
memory: 7356kb
input:
400 0 3009 356 438 551 606 387 2073 2777 2778 824 404 394 554 248 545 261 262 548 253 561 403 415 27...
output:
3170248737
result:
ok "3170248737"
Test #8:
score: 4
Accepted
time: 0ms
memory: 7384kb
input:
400 0 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633...
output:
6979363199
result:
ok "6979363199"
Test #9:
score: 4
Accepted
time: 1ms
memory: 7444kb
input:
400 0 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444...
output:
3318244868
result:
ok "3318244868"
Test #10:
score: 4
Accepted
time: 1ms
memory: 7380kb
input:
5000 0 26416 26417 26418 26419 26420 26421 26422 26423 26424 26425 26426 26427 26428 26429 26430 264...
output:
33887118980393
result:
ok "33887118980393"
Test #11:
score: 4
Accepted
time: 1ms
memory: 7372kb
input:
5000 0 23207 23208 23209 23210 23211 23212 23213 23214 23215 23216 23217 23218 23219 23220 23221 232...
output:
33248894141865
result:
ok "33248894141865"
Test #12:
score: 4
Accepted
time: 1ms
memory: 7440kb
input:
5000 0 66757 66758 66759 66760 66761 66762 66763 66764 66765 66766 66767 66768 66769 66770 66771 667...
output:
37723392435267
result:
ok "37723392435267"
Test #13:
score: 4
Accepted
time: 2ms
memory: 7416kb
input:
5000 0 27900 27901 27902 27903 27904 27905 27906 27907 27908 27909 27910 27911 27912 27913 27914 279...
output:
32475835026360
result:
ok "32475835026360"
Test #14:
score: 4
Accepted
time: 1ms
memory: 7408kb
input:
5000 0 88538 88539 88540 88541 88542 88543 88544 88545 88546 88547 88548 88549 88550 88551 88552 885...
output:
46614887221321
result:
ok "46614887221321"
Test #15:
score: 4
Accepted
time: 1ms
memory: 7360kb
input:
5000 0 57597 57598 57599 57600 57601 57602 57603 57604 57605 57606 57607 57608 57609 57610 57611 576...
output:
42148252068110
result:
ok "42148252068110"
Test #16:
score: 4
Accepted
time: 1ms
memory: 7328kb
input:
5000 0 32091 32092 32093 32094 32095 32096 32097 32098 32099 32100 32101 32102 32103 32104 32105 321...
output:
36356787333837
result:
ok "36356787333837"
Test #17:
score: 4
Accepted
time: 74ms
memory: 15500kb
input:
500000 0 232520 232521 232522 232523 232524 232525 232526 232527 232528 232529 232530 232531 232532 ...
output:
3404048031740228391
result:
ok "3404048031740228391"
Test #18:
score: 4
Accepted
time: 66ms
memory: 15692kb
input:
500000 0 171097 171098 171099 171100 171101 171102 171103 171104 171105 171106 171107 171108 171109 ...
output:
3274086928325086647
result:
ok "3274086928325086647"
Test #19:
score: 4
Accepted
time: 52ms
memory: 15596kb
input:
500000 0 507482 507483 507484 507485 507486 507487 507488 507489 507490 507491 507492 507493 507494 ...
output:
3313623507696416665
result:
ok "3313623507696416665"
Test #20:
score: 4
Accepted
time: 47ms
memory: 15672kb
input:
500000 0 184967 184968 184969 184970 184971 184972 184973 184974 184975 184976 184977 184978 184979 ...
output:
3136834726987393666
result:
ok "3136834726987393666"
Test #21:
score: 4
Accepted
time: 57ms
memory: 15676kb
input:
500000 0 532977 532978 532979 532980 532981 532982 532983 532984 532985 532986 532987 532988 532989 ...
output:
3194381893489801230
result:
ok "3194381893489801230"
Test #22:
score: 4
Accepted
time: 65ms
memory: 15656kb
input:
500000 0 599257 599258 599259 599260 599261 599262 599263 599264 599265 599266 599267 599268 599269 ...
output:
3354933513897756362
result:
ok "3354933513897756362"
Test #23:
score: 4
Accepted
time: 915ms
memory: 893588kb
input:
40000000 1 825772993 851948608 851948609 23077817 23077818 100000 10000000 446359966 479914397 10000...
output:
3794994452005049854674339
result:
ok "3794994452005049854674339"
Test #24:
score: 4
Accepted
time: 914ms
memory: 910108kb
input:
40000000 1 843670282 1068932343 1068932344 12005507 12005508 100000 20000000 191508704 225063135 200...
output:
2875588265896779695426252
result:
ok "2875588265896779695426252"
Test #25:
score: 4
Accepted
time: 929ms
memory: 928336kb
input:
40000000 1 308437383 27106938 27106939 1520476 1520477 100000 30000000 7283395 40837826 30000152 202...
output:
2049762805232475409502206
result:
ok "2049762805232475409502206"