#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;
int no_operation=-1;
void Init()
{
m=1;
while (m<N)
m <<= 1;
st.resize(2*m+2);
lazy.resize(2*m+2,no_operation);
}
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)
{
if (lazy[x]==no_operation)
return;
st[x].first+=lazy[x];
if (lx!=rx)
{
lazy[2*x]=lazy[x];
lazy[2*x+1]=lazy[x];
}
lazy[x]=no_operation;
}
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 |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
2836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
3704 KB |
Output is correct |
2 |
Incorrect |
82 ms |
3572 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
92 ms |
6660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
165 ms |
8152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
245 ms |
11296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
310 ms |
14268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
380 ms |
26512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
349 ms |
22548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |