https://ac.nowcoder.com/acm/contest/19684/H
#include<bits/stdc++.h>
using namespace std;
const int N=300005;
struct tnode
{
long long maxx;
int l,r;
};
int n,m,k,cnt[N];
struct s_Tree
{
tnode t[4*N];
void update (int root)
{
int ch=root<<1;
//结点保存左右子树的最大值(查找子树可以存的数据)
t[root].maxx=max(t[ch].maxx,t[ch+1].maxx);
}
void buildt(int root,int l,int r)
{
t[root].l=l;
t[root].r=r;
if(l!=r)
{
int mid=(l+r)>>1;
int ch=root <<1;
buildt(ch,l,mid);
buildt(ch+1,mid+1,r);
update(root);//建树初始化
}
else
{
t[root].maxx=m;
cnt[l]=0;//直接存树的孙子点
}
}
int gao(int val)
{
int root=1;//从根结点查起
while(t[root].l!=t[root].r)
{ //如果左子树可以
if(t[root<<1].maxx>=val)
{
root<<=1;
}
else //查找右子树
{
root=root*2+1;
}
}
t[root].maxx-=val;//使用了val
++cnt[t[root].l];//该节点使用次数+1
int ans=t[root].l;//该点的位置id
if(cnt[t[root].l]==k)
{
//使用次数达到最高
t[root].maxx=0;
}
root>>=1;//找父节点
while(root)
{
update(root);//更新所有父节点
root>>=1;//更新
}
return ans;
}
};
s_Tree st;
int x,t;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--)
{
cin>>n>>m>>k;
st.buildt(1,1,n);//建树
for(int i=1;i<=n;i++)
{
cin>>x;
if(x>m)
{
cout<<"-1\n";
}
else
{
cout<<st.gao(x)<<"\n";
}
}
}
return 0;
}
666666