#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define pp pair<ll,int>
#define ub(x,i) upper_bound(all(x),i)-x.begin()
using namespace std;
const int mxn=5e5+5;
const ll inf=1e16;
pii a[mxn];
ll fw[mxn]{0};
void add(int i,ll amt){
for(;i<mxn;i+=i&-i)fw[i]+=amt;
}
ll sum(int i,ll rs=0){
for(;i;i-=i&-i)rs+=fw[i];
return rs;
}
int id[mxn]{0};
vector<pii>x;
ll t[4*mxn]{0};
ll lz[4*mxn]{0};
void build(int i,int l,int r){
if(l==r)return void(t[i]=inf);
int m=(l+r)>>1;
build(2*i,l,m);build(2*i+1,m+1,r);
t[i]=min(t[2*i],t[2*i+1]);
}
void push(int i,int l,int r){
t[i]+=lz[i];
if(l<r)lz[2*i]+=lz[i],lz[2*i+1]+=lz[i];
lz[i]=0;
}
void update(int i,int l,int r,int tl,int tr,ll v){
push(i,l,r);
if(r<tl||l>tr)return;
if(r<=tr&&l>=tl){
lz[i]+=v;push(i,l,r);return;
}int m=(l+r)>>1;
update(2*i,l,m,tl,tr,v);update(2*i+1,m+1,r,tl,tr,v);
t[i]=min(t[2*i],t[2*i+1]);
}
int qr(int i,int l,int r,ll x){
push(i,l,r);
if(l==r)return l;int m=(l+r)>>1;
push(2*i,l,m),push(2*i+1,m+1,r);
if(t[2*i+1]<=x)return qr(2*i+1,m+1,r,x);
return qr(2*i,l,m,x);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].f;
for(int i=1;i<=n;i++)cin>>a[i].s;
sort(a+1,a+n+1);for(int i=1;i<=n;i++)x.pb({a[i].s+a[i].f,i});
sort(all(x));for(int i=0;i<n;i++)id[x[i].s]=i+1;
build(1,1,n);
ll cur=0;int ans=0;
for(int i=1;i<=n;i++){
if(cur<=a[i].s){
ans++;cur+=a[i].f;
update(1,1,n,id[i],id[i],a[i].s-inf);
update(1,1,n,1,id[i],a[i].f);
add(id[i],a[i].f);
}
else {
int tl=qr(1,1,n,cur+a[i].f-1);
ll sm=sum(n)-sum(tl);
if(cur-sm>a[i].s)continue;
ans++;cur+=a[i].f;
update(1,1,n,id[i],id[i],a[i].s-inf);
update(1,1,n,1,id[i],a[i].f);
add(id[i],a[i].f);
}
}cout<<ans;
}
# | 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... |