#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define psf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(),x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar_unlocked();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
#define maxn 500005
struct node{
int s,e,m,v,lz,mn;
node *l,*r;
node(int _s,int _e){
s=_s,e=_e,m=(s+e)>>1,mn=INF,v=0,lz=0;
if(s!=e)l=new node(s,m),r=new node(m+1,e);
}
void ppo(){
v+=lz;mn+=lz;
if(s!=e&&lz)l->lz+=lz,r->lz+=lz;
lz=0;
}
void up(int x,int y,int nv){
if(s==x&&e==y){lz+=nv;return;}
if(y<=m)l->up(x,y,nv);
else if(x>m)r->up(x,y,nv);
else l->up(x,m,nv),r->up(m+1,y,nv);
l->ppo(),r->ppo();
mn=min(l->mn,r->mn);
}
void set(int x){
if(s==x&&e==x){mn=v;return;}
if(x<=m)l->set(x);
else r->set(x);
l->ppo(),r->ppo();
mn=min(l->mn,r->mn);
}
int qmn(int x,int y){
ppo();
if(s==x&&e==y)return mn;
if(y<=m)return l->qmn(x,y);
if(x>m)return r->qmn(x,y);
return min(l->qmn(x,m),r->qmn(m+1,y));
}
int qry(int x){
ppo();
if(s==x&&e==x)return v;
if(x<=m)return l->qry(x);
else return r->qry(x);
}
}*rt;
int n,a[maxn],d[maxn],mx[maxn],bad[maxn],pv[maxn];
set<ii> s;
ll ans;
vi play,shop;
int main(){
sf("%d",&n);
rt=new node(0,n-1);
for(int i=0;i<n;++i){
sf("%d",&a[i]);
if(a[i]==-1){
play.pb(i);
rt->up(i,i,INF);
}
else{
shop.pb(i);
ans+=a[i];
rt->up(i,i,i);
}
}
int ptr=-1;
memset(mx,-1,sizeof mx);
for(int i=0;i<sz(shop);++i){
while(ptr+1<sz(play)&&play[ptr+1]<shop[i])++ptr;
int x=shop[i];
pv[x]=ptr;
if(a[x]==0)continue;
d[x]=INF;
if(ptr!=-1)d[x]=min(d[x],shop[i]-play[ptr]);
if(ptr+1<sz(play))d[x]=min(d[x],play[ptr+1]-shop[i]);
if(mx[ptr+1]==-1||d[x]>d[mx[ptr+1]])mx[ptr+1]=x;
s.insert({d[x],x});
}
for(int i=-1;i<sz(play)-1;++i){
if(mx[i+1]==-1)continue;
int x=mx[i+1];
if(a[x]==0)continue;
rt->set(x);
--ans;--a[x];
bad[x]=1;
//pf("take %d\n",x);
if(a[x]==0)s.erase(s.find({d[x],x}));
}
while(!s.empty()){
auto[_,i]=*s.begin();
s.erase(s.begin());
//printf("now: %d\n",i);
int cur=rt->qry(i);
if(cur<0)continue;
//pf("pv %d, mx %d, %d\n",pv[i],mx[pv[i]+1],pv[i]==play.size()-1||i<mx[pv[i]+1]);
int num=min(a[i],cur/(2*d[i])+(pv[i]==play.size()-1||i<mx[pv[i]+1]));
if(i+1<n){
int mn=rt->qmn(i+1,n-1);
num=min(num,mn/(2*d[i]));
}
//printf("d=%d cur=%d, num=%d\n",d[i],cur,num);
ans-=num;
rt->up(i,n-1,-num*d[i]*2);
//for(int i=0;i<n;++i)printf("%d ",rt->qry(i));
//printf("\n");
}
pf("%lld\n",ans);
}
Compilation message
tortoise.cpp: In function 'int main()':
tortoise.cpp:156:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | int num=min(a[i],cur/(2*d[i])+(pv[i]==play.size()-1||i<mx[pv[i]+1]));
| ~~~~~^~~~~~~~~~~~~~~
tortoise.cpp:112:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | sf("%d",&n);
| ^
tortoise.cpp:115:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | sf("%d",&a[i]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |