codeforces 948C
树状数组模拟即可
#include "iostream" #include "iomanip" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm" #include "stdio.h" #include "math.h" #pragma comment(linker, "/STACK:102400000,102400000") #define bug(x) cout<<x<<" "<<"UUUUU"<<endl; #define mem(a,x) memset(a,x,sizeof(a)) #define step(x) fixed<< setprecision(x)<< #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define ll long long #define endl ("\n") #define ft first #define sd second #define lrt (rt<<1) #define rrt (rt<<1|1) using namespace std; const ll mod=1e9+7; const ll INF = 1e18+1LL; const int inf = 1e9+1e8; const double PI=acos(-1.0); const int N=1e5+100; ll n, v[N], t[N], C[N], sum[N], sp[N]; ll Sum(ll x){ ll ret = 0; for(int i=x; i>0; i-=i&(-i)){ ret += C[i]; } return ret; } void add(ll x, ll num){ for(int i=x; i<=n; i+=i&(-i)){ C[i] += num; } } int main(){ scanf("%lld", &n); for(int i=1; i<=n; ++i){ scanf("%lld", &v[i]); } for(int i=1; i<=n; ++i){ scanf("%lld", &t[i]); sum[i] = sum[i-1] + t[i]; //cout<<sum[i]<<" "; } t[n+1] = inf, sum[n+1] = sum[n] + t[n+1]; for(int i=1; i<=n; ++i){ ll goal = v[i] + sum[i-1]; //cout<<goal<<endl; ll l=1, r=n, ans = 0; while(l <= r){ ll mid = l+r>>1; if(sum[mid] <= goal){ ans = mid; l = mid+1; } else r = mid-1; } add(i, 1), add(ans+1, -1); sp[ans+1] += goal - sum[ans]; } for(int i=1; i<=n; ++i){ ll ret = Sum(i)*t[i] + sp[i]; cout<<ret<<" "; } return 0; }