Submission #1867923
Source Code Expand
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 300010
#define LL long long
using namespace std;
int n,m;
LL a[MAXN],s1[MAXN],s2[MAXN];
int pre[MAXN];
LL f1[MAXN],f2[MAXN];
LL ans[MAXN],temp[MAXN];
namespace Convex{
LL xp[MAXN],yp[MAXN];
int q[MAXN],head,tail;
void init(){
head=0;
tail=-1;
}
void addPoint(int id,LL x,LL y){
xp[id]=x;
yp[id]=y;
while(head<tail && (yp[q[tail]]-yp[q[tail-1]])*(xp[id]-xp[q[tail]])
<=
(yp[id]-yp[q[tail]])*(xp[q[tail]]-xp[q[tail-1]])) tail--;
q[++tail]=id;
}
int query(int i){
while(head<tail && (yp[q[tail]]-yp[q[tail-1]])<=(xp[q[tail]]-xp[q[tail-1]])*i) tail--;
return q[tail];
}
}
void init(){
for(int i=1;i<=n;i++) s1[i]=a[i]+s1[i-1];
for(int i=1;i<=n;i++) s2[i]=a[n-i+1]+s2[i-1];
Convex::init();
Convex::addPoint(0,0,0);
for(int i=1;i<=n;i++){
int j=Convex::query(i);
f1[i]=max(f1[i-1],f1[j]+(LL)(i-j)*(i-j+1)/2-(s1[i]-s1[j]));
pre[i]=j;
if(f1[i]==f1[i-1]) pre[i]=i;
Convex::addPoint(i,i,f1[i]+((LL)i*i-i)/2+s1[i]);
}
Convex::init();
Convex::addPoint(0,0,0);
for(int i=1;i<=n;i++){
int j=Convex::query(i);
f2[i]=max(f2[i-1],f2[j]+(LL)(i-j)*(i-j+1)/2-(s2[i]-s2[j]));
Convex::addPoint(i,i,f2[i]+((LL)i*i-i)/2+s2[i]);
}
for(int i=1;i<=n;i++) if(i<n-i+1) swap(f2[i],f2[n-i+1]);
for(int i=1;i<=n;i++) ans[i]=-(1LL<<60);
}
void gao(int l,int r){
if(l==r){
ans[l]=max(ans[l],f1[l-1]+f2[l+1]+1-a[l]);
return;
}
int mid=(l+r)>>1;
Convex::init();
for(int i=l-1;i<mid;i++)
Convex::addPoint(i,i,f1[i]+((LL)i*i-i)/2+s1[i]);
for(int i=mid+1;i<=r;i++){
int j=Convex::query(i);
temp[i]=f2[i+1]+f1[j]+(LL)(i-j)*(i-j+1)/2-(s1[i]-s1[j]);
}
LL tmax=-(1LL<<60);
for(int i=r;i>mid;i--){
if(temp[i]>tmax) tmax=temp[i];
if(tmax>ans[i]) ans[i]=tmax;
}
Convex::init();
for(int i=r+1;i>mid+1;i--){
int i2=r+1-i;
Convex::addPoint(i,i2,f2[i]+((LL)i2*i2-i2)/2+s2[n-i+1]);
}
for(int i=mid;i>=l;i--){
int j=Convex::query(r+1-i);
temp[i]=f1[i-1]+f2[j]+(LL)(j-i)*(j-i+1)/2-(s1[j-1]-s1[i-1]);
}
tmax=-(1LL<<60);
for(int i=l;i<=mid;i++){
if(temp[i]>tmax) tmax=temp[i];
if(tmax>ans[i]) ans[i]=tmax;
}
gao(l,mid);
gao(mid+1,r);
}
int main(){
#ifdef DEBUG
freopen("F.in","r",stdin);
#endif
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",a+i);
init();
gao(1,n);
scanf("%d",&m);
while(m--){
int x,y;
scanf("%d%d",&x,&y);
LL ans1=f1[x-1]+f2[x+1];
LL ans2=ans[x]-y+a[x];
printf("%lld\n",max(ans1,ans2));
}
}
Submission Info
Submission Time |
|
Task |
F - Contest with Drinks Hard |
User |
ez_zjt |
Language |
C++14 (GCC 5.4.1) |
Score |
1400 |
Code Size |
2589 Byte |
Status |
AC |
Exec Time |
259 ms |
Memory |
24832 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:101:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
./Main.cpp:102:41: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%lld",a+i);
^
./Main.cpp:105:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&m);
^
./Main.cpp:108:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&y);
^
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1400 / 1400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample_01.txt, sample_02.txt |
All |
sample_01.txt, sample_02.txt, subtask_1_high_equaly_01.txt, subtask_1_high_equaly_02.txt, subtask_1_high_equaly_03.txt, subtask_1_high_equaly_04.txt, subtask_1_high_rand_01.txt, subtask_1_high_rand_02.txt, subtask_1_high_rand_03.txt, subtask_1_high_rand_04.txt, subtask_1_killer_01.txt, subtask_1_killer_02.txt, subtask_1_low_equaly_01.txt, subtask_1_low_equaly_02.txt, subtask_1_low_equaly_03.txt, subtask_1_low_equaly_04.txt, subtask_1_low_rand_01.txt, subtask_1_low_rand_02.txt, subtask_1_low_rand_03.txt, subtask_1_low_rand_04.txt, subtask_1_max_high_equaly_01.txt, subtask_1_max_high_equaly_02.txt, subtask_1_max_high_equaly_03.txt, subtask_1_max_high_equaly_04.txt, subtask_1_max_high_rand_01.txt, subtask_1_max_high_rand_02.txt, subtask_1_max_high_rand_03.txt, subtask_1_max_high_rand_04.txt, subtask_1_max_low_equaly_01.txt, subtask_1_max_low_equaly_02.txt, subtask_1_max_low_equaly_03.txt, subtask_1_max_low_equaly_04.txt, subtask_1_max_low_rand_01.txt, subtask_1_max_low_rand_02.txt, subtask_1_max_low_rand_03.txt, subtask_1_max_low_rand_04.txt, subtask_1_max_rand_equaly_01.txt, subtask_1_max_rand_equaly_02.txt, subtask_1_max_rand_equaly_03.txt, subtask_1_max_rand_equaly_04.txt, subtask_1_max_rand_rand_01.txt, subtask_1_max_rand_rand_02.txt, subtask_1_max_rand_rand_03.txt, subtask_1_max_rand_rand_04.txt, subtask_1_min_rand_rand_01.txt, subtask_1_min_rand_rand_02.txt, subtask_1_rand_equaly_01.txt, subtask_1_rand_equaly_02.txt, subtask_1_rand_equaly_03.txt, subtask_1_rand_equaly_04.txt, subtask_1_rand_rand_01.txt, subtask_1_rand_rand_02.txt, subtask_1_rand_rand_03.txt, subtask_1_rand_rand_04.txt |
Case Name |
Status |
Exec Time |
Memory |
sample_01.txt |
AC |
5 ms |
18560 KB |
sample_02.txt |
AC |
5 ms |
18560 KB |
subtask_1_high_equaly_01.txt |
AC |
109 ms |
21760 KB |
subtask_1_high_equaly_02.txt |
AC |
79 ms |
21504 KB |
subtask_1_high_equaly_03.txt |
AC |
24 ms |
18688 KB |
subtask_1_high_equaly_04.txt |
AC |
160 ms |
22272 KB |
subtask_1_high_rand_01.txt |
AC |
108 ms |
21760 KB |
subtask_1_high_rand_02.txt |
AC |
78 ms |
21760 KB |
subtask_1_high_rand_03.txt |
AC |
114 ms |
21760 KB |
subtask_1_high_rand_04.txt |
AC |
145 ms |
22016 KB |
subtask_1_killer_01.txt |
AC |
5 ms |
16512 KB |
subtask_1_killer_02.txt |
AC |
5 ms |
16512 KB |
subtask_1_low_equaly_01.txt |
AC |
171 ms |
23424 KB |
subtask_1_low_equaly_02.txt |
AC |
109 ms |
22144 KB |
subtask_1_low_equaly_03.txt |
AC |
124 ms |
22528 KB |
subtask_1_low_equaly_04.txt |
AC |
61 ms |
21632 KB |
subtask_1_low_rand_01.txt |
AC |
133 ms |
23296 KB |
subtask_1_low_rand_02.txt |
AC |
101 ms |
22272 KB |
subtask_1_low_rand_03.txt |
AC |
87 ms |
22272 KB |
subtask_1_low_rand_04.txt |
AC |
166 ms |
23296 KB |
subtask_1_max_high_equaly_01.txt |
AC |
251 ms |
23552 KB |
subtask_1_max_high_equaly_02.txt |
AC |
253 ms |
23936 KB |
subtask_1_max_high_equaly_03.txt |
AC |
254 ms |
24192 KB |
subtask_1_max_high_equaly_04.txt |
AC |
252 ms |
23296 KB |
subtask_1_max_high_rand_01.txt |
AC |
255 ms |
24192 KB |
subtask_1_max_high_rand_02.txt |
AC |
257 ms |
23552 KB |
subtask_1_max_high_rand_03.txt |
AC |
256 ms |
23680 KB |
subtask_1_max_high_rand_04.txt |
AC |
259 ms |
23936 KB |
subtask_1_max_low_equaly_01.txt |
AC |
231 ms |
24448 KB |
subtask_1_max_low_equaly_02.txt |
AC |
225 ms |
24832 KB |
subtask_1_max_low_equaly_03.txt |
AC |
233 ms |
24448 KB |
subtask_1_max_low_equaly_04.txt |
AC |
228 ms |
24832 KB |
subtask_1_max_low_rand_01.txt |
AC |
227 ms |
24832 KB |
subtask_1_max_low_rand_02.txt |
AC |
235 ms |
24576 KB |
subtask_1_max_low_rand_03.txt |
AC |
229 ms |
24704 KB |
subtask_1_max_low_rand_04.txt |
AC |
224 ms |
24832 KB |
subtask_1_max_rand_equaly_01.txt |
AC |
249 ms |
23040 KB |
subtask_1_max_rand_equaly_02.txt |
AC |
250 ms |
23040 KB |
subtask_1_max_rand_equaly_03.txt |
AC |
252 ms |
23040 KB |
subtask_1_max_rand_equaly_04.txt |
AC |
251 ms |
23040 KB |
subtask_1_max_rand_rand_01.txt |
AC |
250 ms |
23040 KB |
subtask_1_max_rand_rand_02.txt |
AC |
251 ms |
23040 KB |
subtask_1_max_rand_rand_03.txt |
AC |
252 ms |
23040 KB |
subtask_1_max_rand_rand_04.txt |
AC |
258 ms |
23040 KB |
subtask_1_min_rand_rand_01.txt |
AC |
5 ms |
16512 KB |
subtask_1_min_rand_rand_02.txt |
AC |
5 ms |
16512 KB |
subtask_1_rand_equaly_01.txt |
AC |
14 ms |
18688 KB |
subtask_1_rand_equaly_02.txt |
AC |
149 ms |
21888 KB |
subtask_1_rand_equaly_03.txt |
AC |
70 ms |
21376 KB |
subtask_1_rand_equaly_04.txt |
AC |
117 ms |
21760 KB |
subtask_1_rand_rand_01.txt |
AC |
173 ms |
22400 KB |
subtask_1_rand_rand_02.txt |
AC |
105 ms |
21760 KB |
subtask_1_rand_rand_03.txt |
AC |
157 ms |
22144 KB |
subtask_1_rand_rand_04.txt |
AC |
76 ms |
21376 KB |