// created by Dinh Manh Hung
// onepunchac168
// THPT CHUYEN HA TINH
// HA TINH, VIET NAM
#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;
//#pragma GCC optimize("O3,unroll-loops,no-stack-protector")
//#pragma GCC target("sse4,avx2,fma")
#define fi first
#define se second
#define pb push_back
typedef int ll;
typedef pair <ll,ll> ii;
typedef pair <ll,ii> iii;
typedef pair <ii,ii> iiii;
const char nl = '\n';
/* END OF TEMPLATE*/
// ================= Solution =================//
int n,l;
const int BLOCK=501;
ll x[150005];
struct Siuuu{
ii dp[505*2];
ll a[505*2];
ll sz;
void build(vector <ll> opt)
{
sz=opt.size();
ll id=sz;
for (int i=sz;i>=1;i--)
{
a[i]=opt[i-1];
while (a[id]-a[i]>l)
{
id--;
}
if (id==sz)
{
dp[i]={1,a[id]-a[i]};
}
else
{
dp[i]={dp[id+1].fi+1,dp[id+1].se};
}
}
}
void insert(ll val)
{
ll id=sz+1;
for (int i=1;i<=sz;i++)
{
if (a[i]<=val)
{
}
else
{
id=i;
break;
}
}
sz++;
for (int i=sz;i>id;i--)
{
a[i]=a[i-1];
}
a[id]=val;
id=sz;
for (int i=sz;i>=1;i--)
{
while (a[id]-a[i]>l)
{
id--;
}
if (id==sz)
{
dp[i]={1,a[id]-a[i]};
}
else
{
dp[i]={dp[id+1].fi+1,dp[id+1].se};
}
}
}
void erase(ll val)
{
ll id;
for (int i=1;i<=sz;i++)
{
if (a[i]<val)
{
}
else
{
id=i;
break;
}
}
sz--;
for (int i=id;i<=sz;i++)
{
a[i]=a[i+1];
}
id=sz;
for (int i=sz;i>=1;i--)
{
while (a[id]-a[i]>l)
{
id--;
}
if (id==sz)
{
dp[i]={1,a[id]-a[i]};
}
else
{
dp[i]={dp[id+1].fi+1,dp[id+1].se};
}
}
}
} b[305];
void optmushnpr()
{
ll id=0;
for (int i=0;i<=(n-1)/BLOCK;i++)
{
for (int j=1;j<=b[i].sz;j++)
{
x[id]=b[i].a[j];
id++;
}
}
for (int i=0;i<=(n-1)/BLOCK;i++)
{
ll a1=BLOCK*i;
ll a2=BLOCK*(i+1)-1;
a2=min(a2,n-1);
vector <ll> vt;
for (int j=a1;j<=a2;j++)
{
vt.pb(x[j]);
}
b[i].build(vt);
}
}
void init(int N, int L, int X[])
{
n=N;
l=L;
for (int i=0;i<n;i++)
{
x[i]=X[i];
}
for (int i=0;i<=(n-1)/BLOCK;i++)
{
ll a1=BLOCK*i;
ll a2=BLOCK*(i+1)-1;
a2=min(a2,n-1);
vector <ll> vt;
for (int j=a1;j<=a2;j++)
{
vt.pb(x[j]);
}
b[i].build(vt);
}
}
ll dem=0;
int update(int aa, int y)
{
if (dem==400)
{
optmushnpr();
dem=0;
}
dem++;
ll res=0;
ll bl1;
ll bl2;
for (int i=(n-1)/BLOCK;i>=0;i--)
{
if (b[i].sz>0)
{
if (b[i].a[1]<=x[aa])
{
bl1=i;
break;
}
}
}
for (int i=(n-1)/BLOCK;i>=0;i--)
{
if (b[i].sz>0)
{
if (b[i].a[1]<=y)
{
bl2=i;
break;
}
}
}
b[bl1].erase(x[aa]);
b[bl2].insert(y);
x[aa]=y;
ll ok=l;
ll gg;
ll dd=0;
for (int i=0;i<=(n-1)/BLOCK;i++)
{
if (b[i].sz==0)
{
continue;
}
dd++;
if (dd==1)
{
res=b[i].dp[1].fi;
ok=b[i].dp[1].se;
gg=b[i].a[b[i].sz];
}
else
{
if (b[i].a[b[i].sz]-gg+ok<=l)
{
ok+=b[i].a[b[i].sz]-gg;
gg=b[i].a[b[i].sz];
}
else
{
int left=1;
int right=b[i].sz;
ll ans=0;
while (left<=right)
{
int mid=(left+right)/2;
if (b[i].a[mid]-gg+ok<=l)
{
ans=mid;
left=mid+1;
}
else right=mid-1;
}
res+=b[i].dp[ans+1].fi;
ok=b[i].dp[ans+1].se;
gg=b[i].a[b[i].sz];
}
}
}
return res;
}
Compilation message
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:93:25: warning: 'bl1' may be used uninitialized in this function [-Wmaybe-uninitialized]
93 | for (int i=1;i<=sz;i++)
| ^~
elephants.cpp:184:8: note: 'bl1' was declared here
184 | ll bl1;
| ^~~
elephants.cpp:242:36: warning: 'gg' may be used uninitialized in this function [-Wmaybe-uninitialized]
242 | if (b[i].a[mid]-gg+ok<=l)
| ~~~~~~~~~~~^~~
elephants.cpp:92:12: warning: 'id' may be used uninitialized in this function [-Wmaybe-uninitialized]
92 | ll id;
| ^~
elephants.cpp:72:14: warning: 'bl2' may be used uninitialized in this function [-Wmaybe-uninitialized]
72 | a[id]=val;
| ~~~~~^~~~
elephants.cpp:185:8: note: 'bl2' was declared here
185 | ll bl2;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3924 KB |
Output is correct |
2 |
Runtime error |
6 ms |
7764 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3924 KB |
Output is correct |
2 |
Runtime error |
6 ms |
7764 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3924 KB |
Output is correct |
2 |
Runtime error |
6 ms |
7764 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3924 KB |
Output is correct |
2 |
Runtime error |
6 ms |
7764 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3924 KB |
Output is correct |
2 |
Runtime error |
6 ms |
7764 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |