CSES Solutions
#1749Range Queries

List Removals

View on CSES

Solution

1749-List_Removals.cpp
1#include<bits/stdc++.h>
2using namespace std;
3#define int long long
4int n;
5void upd(int *fw,int x,int v){
6  for(++x;x<=n;x+=(x&-x)) fw[x]+=v;
7}
8int sum(int *fw,int x){
9  int res=0;
10  for(++x;x>0;x-=(x&-x)) res+=fw[x];
11  return res;
12}
13signed main(){
14  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
15  cin>>n;
16  int a[n]; for(int i=0;i<n;++i){
17    cin>>a[i];
18  }
19  int fw[n+5]{};
20  for(int i=0;i<n;++i){
21    int x; cin>>x; --x;
22    int l=0,r=n-1,m;
23    while(l<r){
24      m=(l+r)>>1;
25      int cnt=sum(fw,m);
26      if(cnt+x <= m) r=m;
27      else l=m+1;
28    }
29    cout<<a[l]<<" ";
30    upd(fw,l,1);
31  }
32  cout<<'\n';
33  return 0;
34}

Editorial not yet generated for this problem. Run the editorial generation script to add hints and detailed explanations.