This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define MAXN 300010
using namespace std;
int h[MAXN],m,K,N;
struct bit
{
vector<ll> v;
void Init()
{
v.resize(N+2);
}
void Add(int pos,int val)
{
for (int i=pos;i<=N;i+=i&(-i))
v[i]+=val;
}
int Calc(int pos)
{
int ans=0;
for (int i=pos;i>0;i-=i&(-i))
ans+=v[i];
return ans;
}
int Query(int l,int r)
{
return Calc(r)-Calc(l-1);
}
};
struct seg_tree
{
vector<pair<int,int> > st;
vector<int> lazy;
void Init()
{
m=1;
while (m<N)
m <<= 1;
st.resize(2*m+2);
lazy.resize(2*m+2);
}
pair<int,int> Merge(pair<int,int> x,pair<int,int> y)
{
if (x.first<y.first)
return y;
return x;
}
void Build()
{
for (int i=m;i<m+N;i++)
st[i]={h[i-m+1],i-m+1};
for (int i=m-1;i>0;i--)
st[i]=Merge(st[2*i],st[2*i+1]);
}
void Propagate(int x,int lx,int rx)
{
st[x].first+=lazy[x];
if (lx!=rx)
{
lazy[2*x]+=lazy[x];
lazy[2*x+1]+=lazy[x];
}
lazy[x]=0;
}
void Add(int l,int r,int val,int x,int lx,int rx)
{
Propagate(x,lx,rx);
if (lx>r || rx<l)
return;
if (lx>=l && rx<=r)
{
lazy[x]+=val;
Propagate(x,lx,rx);
return;
}
ll mid=(lx+rx)/2;
Add(l,r,val,2*x,lx,mid);
Add(l,r,val,2*x+1,mid+1,rx);
st[x]=Merge(st[2*x],st[2*x+1]);
}
pair<int,int> Calc(int l,int r,int x,int lx,int rx)
{
Propagate(x,lx,rx);
if (lx>r || rx<l)
return {0,0};
if (lx>=l && rx<=r)
return st[x];
ll mid=(lx+rx)/2;
return Merge(Calc(l,r,2*x,lx,mid),Calc(l,r,2*x+1,mid+1,rx));
}
};
seg_tree S;
bit B;
void inicjuj(int n, int k, int *D)
{
N=n;
K=k;
for (int i=0;i<n;i++)
h[i+1]=D[i];
S.Init();
S.Build();
B.Init();
}
void podlej(int a, int b)
{
S.Add(a+1,b+1,1,1,1,m);
}
int dojrzale(int a, int b)
{
while (S.Calc(1,N,1,1,m).first>=K)
{
pair<int,int> maxx=S.Calc(1,N,1,1,m);
B.Add(maxx.second,1);
S.Add(maxx.second,maxx.second,INT_MIN,1,1,m);
}
return B.Query(a+1,b+1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |