結果

問題 No.1496 不思議な数え上げ
コンテスト
ユーザー ryoku
提出日時 2026-08-08 05:26:13
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.90.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 389 ms / 3,000 ms
+ 840µs
コード長 31,630 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,638 ms
コンパイル使用メモリ 323,088 KB
実行使用メモリ 115,672 KB
最終ジャッジ日時 2026-08-08 05:26:37
合計ジャッジ時間 22,837 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#line 1 "lib/template.hpp"
#ifdef TEMPLATE
#else
#define TEMPLATE
# pragma GCC optimize("O3")
using namespace std;
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <climits>
#include <cassert>
#include <functional>
#include <iterator>
#include <utility>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <limits>
#include <optional>
#include <variant>
#include <any>

#include <array>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <version>
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using i128=__int128;
using u128=unsigned __int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vvc<vc<T>>;
template<class T>using smpq=priority_queue<T,vector<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define rall(x) x.rbegin(),x.rend()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define is insert
#define bg begin()
#define ed end()
#define all(x) x.begin(),x.end()
void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a;}
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);
template<class T>void print(T a) { cout << a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout<<endl;}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
template<class T,class F>
bool chmin(T &x, F y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template<class T, class F>
bool chmax(T &x, F y){
    if(x<y){
        x=y;
        return true;
    }
    return false;
}
template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}
void YesNo(bool b){
    cout<<(b?"Yes":"No")<<endl;
}
void YESNO(bool b){
    cout<<(b?"YES":"NO")<<endl;
}
void AliceBob(bool b){
    cout<<(b?"Alice":"Bob")<<endl;
}
void TakahashiAoki(bool b){
    cout<<(b?"Takahashi":"Aoki")<<endl;
}
void Yes(){
    cout<<"Yes"<<endl;
}
void No(){
    cout<<"No"<<endl;
}
vc<int>stovi(const string&s,const string&S){
    vc<int>v(s.size());
    rep(i,s.size()){
        auto t=S.find(s[i]);
        assert(t!=string::npos);
        v[i]=t;
    }
    return v;
}
template<class T=ll>
T isqrt(T x){
    T F=sqrtl(x);
    while((F+1)*(F+1)<=x)F++;
    while(F*F>x)F--;
    return F;
}
template<class T>
vvc<T>trans(const vvc<T>&a){
    assert(a.size()&&a[0].size());
    vvc<T>b(a[0].size(),vc<T>(a.size()));
    rep(i,a.size())rep(j,a[0].size()){
        b[j][i]=a[i][j];
    }
    return b;
}
template<class T>
vc<string>trans(const vc<string>&a){
    assert(a.size()&&a[0].size());
    vc<string>b(a[0].size(),string(a.size(),0));
    rep(i,a.size())rep(j,a[0].size()){
        b[j][i]=a[i][j];
    }
    return b;
}
template<class T>
int popcount(T n){
    return __builtin_popcountll(n);
}
template<class T,class L=ll>
L sum(vc<T>&a){
    return accumulate(all(a),L(0));
}
template<class T>
vc<T>subset(T S){
    vc<T>ans;
    for(T x=S;x>0;x=(x-1)&S)ans.pb(x);
    ans.pb(0);
    return ans;
}
template<class T>
T max(vc<T>&a){
    return *max_element(all(a));
}
template<class T>
T min(vc<T>&a){
    return *min_element(all(a));
}
#ifndef COMPRESSER_STRUCT
#define COMPRESSER_STRUCT
template<class T>
struct Compresser{
    vc<T>x;
    Compresser(int n=0){x.reserve(n);}
    Compresser(const vc<T>&xs){
        x=xs;
    }
    void push(T p){built=false;x.pb(p);}
    bool built=false;
    void build(){
        if(!chmax(built,1))return;
        sort(all(x));
        x.erase(unique(all(x)),x.end());
    }
    int find(T v){
        build();
        auto itr=lower_bound(all(x),v)-x.begin();
        if(itr==x.size()||x[itr]!=v)return -1;
        return itr;
    }
    int find_next(T v){
        build();
        return lower_bound(all(x),v)-x.begin();
    }
    int size(){
        build();
        return x.size();
    }
    T operator[](int i)const{
        assert(0<=i&&i<x.size());
        return x[i];
    }
};
#endif
template<class T,class L=ll>
vc<L> presum(vc<T> &a){
    vc<L> ret(a.size()+1);
    rep(i,a.size())ret[i+1]=ret[i]+a[i];
    return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
    for (auto&v:a)v += b;
    return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
    for (auto&v:a)v-=b;
    return a;
}
template<class T, class F>
vc<T> &operator*=(vc<T>&a,F b){
    for (auto&v:a)v*=b;
    return a;
}
template<class T=ll>
constexpr T POW(T a,T b){
    T res=1;
    while(b){
        if(b&1)res*=a;
        a*=a;
        b/=2;
    }
    return res;
}
constexpr ll ten(ll a){
    return POW<ll>(10,a);
}
template<typename T>constexpr T inf=numeric_limits<T>::max()/2-1;
template<class T>
int tbit(T x){
    using U=make_unsigned_t<T>;
    U y=(U)x;
    return y?(int)bit_width(y)-1:-1;
}
template<class T>
int lbit(T x){
    using U=make_unsigned_t<T>;
    U y=(U)x;
    return y?(int)countr_zero(y):-1;
}
template<class T>
int tbit(T x,int p){
    using U=make_unsigned_t<T>;
    constexpr int W=numeric_limits<U>::digits;
    U y=(U)x;
    if(p<0)return -1;
    if(p>=W-1)return tbit(y);
    return tbit(y&((U(1)<<(p+1))-1));
}
template<class T>
int lbit(T x,int p){
    using U=make_unsigned_t<T>;
    constexpr int W=numeric_limits<U>::digits;
    U y=(U)x;
    if(p<0)return lbit(y);
    if(p>=W)return -1;
    return lbit(y&(~U(0)<<p));
}
istream& operator>>(istream&is,i128&x){
    string s;is>>s;
    x=0;
    int i=0,neg=0;
    if(s[0]=='-')neg=1,i=1;
    for(;i<(int)s.size();i++)x=x*10+s[i]-'0';
    if(neg)x=-x;
    return is;
}
ostream& operator<<(ostream&os,i128 x){
    if(x==0)return os<<0;
    if(x<0)os<<"-";
    using u128=__uint128_t;
    u128 y=x<0?-(u128)x:(u128)x;
    string s;
    while(y)s.pb('0'+y%10),y/=10;
    reverse(all(s));
    return os<<s;
}
#define dbg(...) 1111

#ifdef LOCAL
#undef dbg

template<class T,class U>
ostream& operator<<(ostream&os,const pair<T,U>&p){
    return os<<"("<<p.fi<<", "<<p.se<<")";
}
template<class T,size_t N>
ostream& operator<<(ostream&os,const array<T,N>&a){
    os<<"[";
    rep(i,N){
        if(i)os<<", ";
        os<<a[i];
    }
    return os<<"]";
}
template<class T>
ostream& operator<<(ostream&os,const vc<T>&a){
    os<<"[";
    rep(i,a.size()){
        if(i)os<<", ";
        os<<a[i];
    }
    return os<<"]";
}
template<class T>
ostream& operator<<(ostream&os,const deque<T>&a){
    os<<"[";
    rep(i,a.size()){
        if(i)os<<", ";
        os<<a[i];
    }
    return os<<"]";
}
template<class T>
ostream& operator<<(ostream&os,const set<T>&s){
    os<<"{";
    bool f=0;
    for(auto&x:s){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T>
ostream& operator<<(ostream&os,const multiset<T>&s){
    os<<"{";
    bool f=0;
    for(auto&x:s){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T>
ostream& operator<<(ostream&os,const unordered_set<T>&s){
    os<<"{";
    bool f=0;
    for(auto&x:s){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T,class U>
ostream& operator<<(ostream&os,const map<T,U>&m){
    os<<"{";
    bool f=0;
    for(auto&x:m){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T,class U>
ostream& operator<<(ostream&os,const unordered_map<T,U>&m){
    os<<"{";
    bool f=0;
    for(auto&x:m){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T>
ostream& operator<<(ostream&os,queue<T>q){
    vc<T>a;
    while(q.size())a.pb(q.front()),q.pop();
    return os<<a;
}
template<class T>
ostream& operator<<(ostream&os,stack<T>s){
    vc<T>a;
    while(s.size())a.pb(s.top()),s.pop();
    return os<<a;
}
template<class T,class C,class F>
ostream& operator<<(ostream&os,priority_queue<T,C,F>q){
    vc<T>a;
    while(q.size())a.pb(q.top()),q.pop();
    return os<<a;
}

void debug_out(){cout<<endl;}
template<class T,class... Ts>
void debug_out(const T&x,const Ts&...xs){
    cout<<x;
    if constexpr(sizeof...(xs))cout<<" ",debug_out(xs...);
    else cout<<endl;
}
#define dbg(...) cout<<"["<<#__VA_ARGS__<<"] = ",debug_out(__VA_ARGS__)
#endif

struct TemplateSetup{
    TemplateSetup(){
        #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        
        #endif
        cin.tie(0)->sync_with_stdio(0);
        #ifdef LOCAL
        cout<<fixed<<setprecision(6);
        dbg("==============="s);
        #else
        cout<<fixed<<setprecision(20);
        #endif
    }
};
inline TemplateSetup template_setup;

#endif
#line 2 "lib/ds/segtree.hpp"
/*
struct X{
    using value_type=pair<mint,mint>;
    static value_type op(value_type a,value_type b){
        return {a.first*b.first,b.first*a.second+b.second};
    }
    static value_type e(){
        return {1,0};
    }
};*/
template<typename Info>
struct Segtree{
    using value_type=typename Info::value_type;
    vc<value_type>node;
    int N;
    int n;
    int lg;
    void update(int i){
        assert(0<=i&&i<n);
        node[i]=Info::op(node[i*2],node[i*2+1]);
    }
    Segtree(int N_=0,value_type leaf=Info::e()){
        assert(N_>=0);
        N=N_;
        n=1;
        lg=0;
        while(n<N)n*=2,++lg;
        node.resize(n*2,Info::e());
        REP(i,n,n+N)node[i]=leaf;
        DREP(i,n-1,1)dbg(i),update(i);
    }
    template<class F>
    Segtree(int N_,F f){
        assert(N_>=0);
        N=N_;
        n=1;
        lg=0;
        while(n<N)n*=2,++lg;
        node.resize(n*2,Info::e());
        for(int i=n;i<n+N;i++){
            node[i]=f(i-n);
        }
        DREP(i,n-1,1)update(i);
    }
    void push_back(value_type val){
        if(n==N){
            vc<value_type>vs(n+1);rep(i,n)vs[i]=node[i+n];
            vs[n]=val;
            (*this)=Segtree(N+1,[&](auto i){
                if(i<n+1)return vs[i];
                return Info::e();
            });
        }else{
            node[N+n]=val;
            int i=N+n;
            while(i>>=1)update(i);N++;
        }
    }
    void set(int i,value_type val){
        assert(0<=i&&i<N);
        node[i+=n]=val;
        while(i>>=1)update(i);
    }
    void apply(int i,value_type val){
        assert(0<=i&&i<N);
        i+=n;
        node[i]=Info::op(node[i],val);
        while(i>>=1)update(i);
    }
    value_type prod(int l,int r){
        assert(0<=l&&l<=r&&r<=N);
        l+=n,r+=n;
        value_type sml=Info::e(),smr=Info::e();
        while(l<r){
            if(l&1)sml=Info::op(sml,node[l++]);
            if(r&1)smr=Info::op(node[--r],smr);
            l/=2,r/=2;
        }
        return Info::op(sml,smr);
    }
    value_type all_prod(){
        return node[1];
    }
    template<class F>
    int max_right(int L,F f){
        assert(0<=L&&L<=N);
        int l=n+L,w=1;
        value_type ansL=Info::e();
        for(;L+w<=N;l>>=1,w<<=1)if(l&1){
            if(!f(Info::op(ansL,node[l])))break;
            ansL=Info::op(ansL,node[l++]);
            L+=w;
        }
        while(l<<=1,w>>=1){
            if(L+w<=N&&f(Info::op(ansL,node[l]))){
                ansL=Info::op(ansL,node[l++]);
                L+=w;
            }
        }
        return L;
    }
    template<class F>
    int min_left(int R,F f){
        assert(0<=R&&R<=N);
        int r=n+R,w=1;
        value_type ansR=Info::e();
        for(;R-w>=0;r>>=1,w<<=1)if(r&1){
            if(!f(Info::op(node[r-1],ansR)))break;
            ansR=Info::op(node[--r],ansR);
            R-=w;
        }
        while(r<<=1,w>>=1){
            if(R-w>=0&&f(Info::op(node[r-1],ansR))){
                ansR=Info::op(node[r-1],ansR);
                R-=w;
                r--;
            }
        }
        return R;
    }
};
#line 2 "lib/ds/famous.hpp"
template<class T,class=void>
struct FamousHasCommute{
    static constexpr bool value=false;
};
template<class T>
struct FamousHasCommute<T,decltype((void)T::commute,void())>{
    static constexpr bool value=T::commute;
};
template<class Value_type,Value_type inf>
struct Min{
    using value_type=Value_type;
    static constexpr bool commute=true;
    static value_type op(value_type a,value_type b){
        return min(a,b);
    }
    static value_type e(){
        return inf;
    }
    static value_type get(auto a){
        return inf;
    }
};
template<class Value_type,Value_type neg_inf>
struct Max{
    using value_type=Value_type;
    static constexpr bool commute=true;
    static value_type op(value_type a,value_type b){
        return max(a,b);
    }
    static value_type e(){
        return neg_inf;
    }
};
template<class Value_type>
struct Sum{
    using value_type=Value_type;
    static constexpr bool commute=true;
    static value_type op(value_type a,value_type b){
        return a+b;
    }
    static value_type e(){
        return 0;
    }
};
template<class Value_type>
struct Prod{
    using value_type=Value_type;
    static constexpr bool commute=true;
    static value_type op(value_type a,value_type b){
        return a*b;
    }
    static value_type e(){
        return 1;
    }
};
template<class... Infos>
struct Merger{
    using value_type=tuple<typename Infos::value_type...>;
    static constexpr bool commute=(FamousHasCommute<Infos>::value&&...);
    template<size_t... I>
    static value_type op_impl(value_type a,value_type b,index_sequence<I...>){
        return value_type{Infos::op(get<I>(a),get<I>(b))...};
    }
    static value_type op(value_type a,value_type b){
        return op_impl(a,b,index_sequence_for<Infos...>{});
    }
    static value_type e(){
        return value_type{Infos::e()...};
    }
};
template<class Value_type>
struct Affine{
    using value_type=pair<Value_type,Value_type>;
    static value_type op(value_type a,value_type b){
        return {a.first*b.first,a.second*b.first+b.second};
    }
    static value_type e(){
        return {1,0};
    }
};
template<class Value_type>
struct AddMin{
    struct Info{
        using value_type=Value_type;
        static constexpr bool commute=true;
        static value_type op(value_type a,value_type b){
            return min(a,b);
        }
        static value_type e(){
            return numeric_limits<Value_type>::max();
        }
    };
    struct Tag{
        using lazy_type=Value_type;
        static constexpr bool commute=true;
        static lazy_type Merge(lazy_type old_tag,lazy_type new_tag){
            return old_tag+new_tag;
        }
        static lazy_type id(){
            return 0;
        }
        static typename Info::value_type Apply(typename Info::value_type node,lazy_type lz){
            return node+lz;
        }
    };
};
template<class Value_type>
struct AddMax{
    struct Info{
        using value_type=Value_type;
        static constexpr bool commute=true;
        static value_type op(value_type a,value_type b){
            return max(a,b);
        }
        static value_type e(){
            return numeric_limits<Value_type>::lowest();
        }
    };
    struct Tag{
        using lazy_type=Value_type;
        static constexpr bool commute=true;
        static lazy_type Merge(lazy_type old_tag,lazy_type new_tag){
            return old_tag+new_tag;
        }
        static lazy_type id(){
            return 0;
        }
        static typename Info::value_type Apply(typename Info::value_type node,lazy_type lz){
            return node+lz;
        }
    };
};
template<class Value_type>
struct AddSum{
    struct Info{
        using value_type=pair<Value_type,Value_type>;
        static constexpr bool commute=true;
        static value_type op(value_type a,value_type b){
            return {a.first+b.first,a.second+b.second};
        }
        static value_type e(){
            return {0,0};
        }
    };
    struct Tag{
        using lazy_type=Value_type;
        static constexpr bool commute=true;
        static lazy_type Merge(lazy_type old_tag,lazy_type new_tag){
            return old_tag+new_tag;
        }
        static lazy_type id(){
            return 0;
        }
        static typename Info::value_type Apply(typename Info::value_type node,lazy_type lz){
            return {node.first+node.second*lz,node.second};
        }
    };
};
template<class Value_type>
struct AssignMin{
    struct Info{
        using value_type=Value_type;
        static constexpr bool commute=true;
        static value_type op(value_type a,value_type b){
            return min(a,b);
        }
        static value_type e(){
            return numeric_limits<Value_type>::max();
        }
    };
    struct Tag{
        using lazy_type=pair<bool,Value_type>;
        static lazy_type Merge(lazy_type old_tag,lazy_type new_tag){
            if(new_tag.first)return new_tag;
            return old_tag;
        }
        static lazy_type id(){
            return {false,0};
        }
        static typename Info::value_type Apply(typename Info::value_type node,lazy_type lz){
            if(lz.first)return lz.second;
            return node;
        }
    };
};
template<class Value_type>
struct AssignMax{
    struct Info{
        using value_type=Value_type;
        static constexpr bool commute=true;
        static value_type op(value_type a,value_type b){
            return max(a,b);
        }
        static value_type e(){
            return numeric_limits<Value_type>::lowest();
        }
    };
    struct Tag{
        using lazy_type=pair<bool,Value_type>;
        static lazy_type Merge(lazy_type old_tag,lazy_type new_tag){
            if(new_tag.first)return new_tag;
            return old_tag;
        }
        static lazy_type id(){
            return {false,0};
        }
        static typename Info::value_type Apply(typename Info::value_type node,lazy_type lz){
            if(lz.first)return lz.second;
            return node;
        }
    };
};
template<class Value_type>
struct AssignSum{
    struct Info{
        using value_type=pair<Value_type,Value_type>;
        static constexpr bool commute=true;
        static value_type op(value_type a,value_type b){
            return {a.first+b.first,a.second+b.second};
        }
        static value_type e(){
            return {0,0};
        }
    };
    struct Tag{
        using lazy_type=pair<bool,Value_type>;
        static lazy_type Merge(lazy_type old_tag,lazy_type new_tag){
            if(new_tag.first)return new_tag;
            return old_tag;
        }
        static lazy_type id(){
            return {false,0};
        }
        static typename Info::value_type Apply(typename Info::value_type node,lazy_type lz){
            if(lz.first)return {node.second*lz.second,node.second};
            return node;
        }
    };
};
template<class Value_type>
struct AffineSum{
    struct Info{
        using value_type=pair<Value_type,Value_type>;
        static constexpr bool commute=true;
        static value_type op(value_type a,value_type b){
            return {a.first+b.first,a.second+b.second};
        }
        static value_type e(){
            return {0,0};
        }
    };
    struct Tag{
        using lazy_type=pair<Value_type,Value_type>;
        static lazy_type Merge(lazy_type old_tag,lazy_type new_tag){
            return {new_tag.first*old_tag.first,new_tag.first*old_tag.second+new_tag.second};
        }
        static lazy_type id(){
            return {1,0};
        }
        static typename Info::value_type Apply(typename Info::value_type node,lazy_type lz){
            return {node.first*lz.first+node.second*lz.second,node.second};
        }
    };
};
template<class Info>
struct Reversed{
    using value_type=typename Info::value_type;
    static constexpr bool commute=FamousHasCommute<Info>::value;
    static value_type op(value_type a,value_type b){
        return Info::op(b,a);
    }
    static value_type e(){
        return Info::e();
    }
};


template<class Key,class Val,int K,Val neg_inf>
struct MaxK{
    using DATA=MaxK<Key,Val,K,neg_inf>;
    struct T{
        Val val;
        Key key;
        T&operator+=(Val v){val+=v;return *this;}
        T&operator-=(Val v){val-=v;return *this;}
        T operator+(Val v)const{return {val+v,key};}
        T operator-(Val v)const{return {val-v,key};}
        friend T operator+(Val v,const T&a){return a+v;}
    };
    array<T,K> d;
    MaxK(){d.fill({neg_inf,Key{}});}
    T&operator[](int i){return d[i];}
    const T&operator[](int i)const{return d[i];}
    bool has(Key key,Val val)const{
        rep(i,K)if(d[i].val>neg_inf&&d[i].key==key&&d[i].val==val)return true;
        return false;
    }
    int add_element(Key key,Val val){
        if(val<=d[K-1].val)return 0;
        int pos=0;
        while(pos<K&&d[pos].val>=val){
            if(d[pos].key==key)return 0;
            pos++;
        }
        int end=K-1;
        REP(i,pos,K)if(d[i].key==key){end=i;break;}
        DREP(i,end,pos+1)d[i]=d[i-1];
        d[pos]={val,key};
        return 1;
    }
    DATA&merge_data(const DATA&x){
        rep(i,K)add_element(x.d[i].key,x.d[i].val);
        return *this;
    }
    DATA&operator+=(Val v){
        rep(i,K)if(d[i].val>neg_inf)d[i].val+=v;
        return *this;
    }
    DATA&operator-=(Val v){
        rep(i,K)if(d[i].val>neg_inf)d[i].val-=v;
        return *this;
    }
    DATA operator+(Val v)const{return DATA(*this)+=v;}
    DATA operator-(Val v)const{return DATA(*this)-=v;}
    friend DATA operator+(Val v,const DATA&a){return a+v;}
};
template<class Key,class Val,Val neg_inf>
struct MaxK<Key,Val,2,neg_inf>{
    using DATA=MaxK<Key,Val,2,neg_inf>;
    struct T{
        Val val;
        Key key;
        T&operator+=(Val v){val+=v;return *this;}
        T&operator-=(Val v){val-=v;return *this;}
        T operator+(Val v)const{return {val+v,key};}
        T operator-(Val v)const{return {val-v,key};}
        friend T operator+(Val v,const T&a){return a+v;}
    };
    array<T,2> d;
    MaxK(){d.fill({neg_inf,Key{}});}
    T&operator[](int i){return d[i];}
    const T&operator[](int i)const{return d[i];}
    bool has(Key key,Val val)const{
        return (d[0].val>neg_inf&&d[0].key==key&&d[0].val==val)||(d[1].val>neg_inf&&d[1].key==key&&d[1].val==val);
    }
    int add_element(Key key,Val val){
        if(val<=d[1].val)return 0;
        if(key==d[0].key){
            if(val>d[0].val){
                d[0].val=val;
                return 1;
            }
            return 0;
        }
        if(val>d[0].val){
            d[1]=d[0];
            d[0]={val,key};
        }else{
            d[1]={val,key};
        }
        return 1;
    }
    DATA&merge_data(const DATA&x){
        add_element(x.d[0].key,x.d[0].val);
        add_element(x.d[1].key,x.d[1].val);
        return *this;
    }
    DATA&operator+=(Val v){
        if(d[0].val>neg_inf)d[0].val+=v;
        if(d[1].val>neg_inf)d[1].val+=v;
        return *this;
    }
    DATA&operator-=(Val v){
        if(d[0].val>neg_inf)d[0].val-=v;
        if(d[1].val>neg_inf)d[1].val-=v;
        return *this;
    }
    DATA operator+(Val v)const{return DATA(*this)+=v;}
    DATA operator-(Val v)const{return DATA(*this)-=v;}
    friend DATA operator+(Val v,const DATA&a){return a+v;}
};
template<class Key,class Val,int K,Val neg_inf>
struct MaxKInfo{
    using value_type=MaxK<Key,Val,K,neg_inf>;
    static value_type op(const value_type&a,const value_type&b){
        value_type res(a);
        return res.merge_data(b);
    }
    static value_type e(){return value_type();}
};
template<class Key,class Val,int K,Val inf>
struct MinK{
    using DATA=MinK<Key,Val,K,inf>;
    struct T{
        Val val;
        Key key;
        T&operator+=(Val v){val+=v;return *this;}
        T&operator-=(Val v){val-=v;return *this;}
        T operator+(Val v)const{return {val+v,key};}
        T operator-(Val v)const{return {val-v,key};}
        friend T operator+(Val v,const T&a){return a+v;}
    };
    array<T,K> d;
    MinK(){d.fill({inf,Key{}});}
    T&operator[](int i){return d[i];}
    const T&operator[](int i)const{return d[i];}
    bool has(Key key,Val val)const{
        rep(i,K)if(d[i].val<inf&&d[i].key==key&&d[i].val==val)return true;
        return false;
    }
    int add_element(Key key,Val val){
        if(val>=d[K-1].val)return 0;
        int pos=0;
        while(pos<K&&d[pos].val<=val){
            if(d[pos].key==key)return 0;
            pos++;
        }
        int end=K-1;
        REP(i,pos,K)if(d[i].key==key){end=i;break;}
        DREP(i,end,pos+1)d[i]=d[i-1];
        d[pos]={val,key};
        return 1;
    }
    DATA&merge_data(const DATA&x){
        rep(i,K)add_element(x.d[i].key,x.d[i].val);
        return *this;
    }
    DATA&operator+=(Val v){
        rep(i,K)if(d[i].val<inf)d[i].val+=v;
        return *this;
    }
    DATA&operator-=(Val v){
        rep(i,K)if(d[i].val<inf)d[i].val-=v;
        return *this;
    }
    DATA operator+(Val v)const{return DATA(*this)+=v;}
    DATA operator-(Val v)const{return DATA(*this)-=v;}
    friend DATA operator+(Val v,const DATA&a){return a+v;}
};
template<class Key,class Val,Val inf>
struct MinK<Key,Val,2,inf>{
    using DATA=MinK<Key,Val,2,inf>;
    struct T{
        Val val;
        Key key;
        T&operator+=(Val v){val+=v;return *this;}
        T&operator-=(Val v){val-=v;return *this;}
        T operator+(Val v)const{return {val+v,key};}
        T operator-(Val v)const{return {val-v,key};}
        friend T operator+(Val v,const T&a){return a+v;}
    };
    array<T,2> d;
    MinK(){d.fill({inf,Key{}});}
    T&operator[](int i){return d[i];}
    const T&operator[](int i)const{return d[i];}
    bool has(Key key,Val val)const{
        return (d[0].val<inf&&d[0].key==key&&d[0].val==val)||(d[1].val<inf&&d[1].key==key&&d[1].val==val);
    }
    int add_element(Key key,Val val){
        if(val>=d[1].val)return 0;
        if(key==d[0].key){
            if(val<d[0].val){
                d[0].val=val;
                return 1;
            }
            return 0;
        }
        if(val<d[0].val){
            d[1]=d[0];
            d[0]={val,key};
        }else{
            d[1]={val,key};
        }
        return 1;
    }
    DATA&merge_data(const DATA&x){
        add_element(x.d[0].key,x.d[0].val);
        add_element(x.d[1].key,x.d[1].val);
        return *this;
    }
    DATA&operator+=(Val v){
        if(d[0].val<inf)d[0].val+=v;
        if(d[1].val<inf)d[1].val+=v;
        return *this;
    }
    DATA&operator-=(Val v){
        if(d[0].val<inf)d[0].val-=v;
        if(d[1].val<inf)d[1].val-=v;
        return *this;
    }
    DATA operator+(Val v)const{return DATA(*this)+=v;}
    DATA operator-(Val v)const{return DATA(*this)-=v;}
    friend DATA operator+(Val v,const DATA&a){return a+v;}
};
template<class Key,class Val,int K,Val inf>
struct MinKInfo{
    using value_type=MinK<Key,Val,K,inf>;
    static value_type op(const value_type&a,const value_type&b){
        value_type res(a);
        return res.merge_data(b);
    }
    static value_type e(){return value_type();}
};
#line 4 "A/main.cpp"
struct SegTree{
    Segtree<Sum<ll>>seg;
    int L,R;
    int N;
    //[L,R] を占有
    SegTree(ll x):L(1),R(1),N(3),seg(3){
        seg.set(1,x);
    }
    void extend(){
        ll n=R-L+1;
        vc<ll>vs(R-L+1);REP(i,L,R+1)vs[i-L]=(seg.node[i+seg.n]);
        seg=Segtree<Sum<ll>>(3*n,[&](auto i){
            if(i<n)return 0ll;
            if(i>=n*2)return 0ll;
            return vs[i-n];
        });
        N=3*n;
        L=n,R=n*2-1;
    }
    void push_back(ll x){
        if(R+1==N){
            extend();
        }
        seg.set(++R,x);
    }
    void push_front(ll x){
        if(L==0){
            extend();
        }
        seg.set(--L,x);
    }
    //x を超えないような [0,i) -> max i
    ll leftlower(ll x){
        if(x<=0)return 0;
        return min(seg.max_right(L,[&](auto v){
            return v<x;
        }),R+1)-L+1;
    }
    ll rightlower(ll x){
        if(x<=0)return 0;
        
        return R+1-max(seg.min_left(R+1,[&](auto v){
            return v<x;
        }),L)+1;
    }
    ll size(){
        return R-L+1;
    }
    vc<ll>getvs(){
        vc<ll>res(R-L+1);rep(i,R-L+1)res[i]=seg.node[L+i+seg.n];
        return res;
    }
};
void solve(){
    LL(n);
    VC(ll,p,n);
    VC(ll,a,n);
    Segtree<Min<pii,{inf<int>,inf<int>}>>minseg(n,[&](auto i){
        return pii{p[i],i};
    });
    vc<ll>ans(n);
    vc<SegTree>seg(n+1,0);
    auto dfs=[&](auto&dfs,int l,int r)->int{
        if(l==r){
            return n;
        }
        if(l+1==r){
            ans[p[l]-1]=(a[p[l]-1]>=p[l]);
            seg[l]=(p[l]);
            return l;
        }
        int mid=minseg.prod(l,r).second;
        int r0=dfs(dfs,l,mid);
        int r1=dfs(dfs,mid+1,r);
        a[p[mid]-1]-=p[mid];
        if(r0==n||seg[r0].size()<seg[r1].size()){
            
            ans[p[mid]-1]+=seg[r1].leftlower(a[p[mid]-1]+1);
            if(r0!=n){

                auto vr0=seg[r0].getvs();
                drep(i,vr0.size()){
                    a[p[mid]-1]-=vr0[i];
                    ans[p[mid]-1]+=seg[r1].leftlower(a[p[mid]-1]+1);
                }
                seg[r1].push_front(p[mid]);
                reverse(all(vr0));
                for(auto&x:vr0)seg[r1].push_front(x);
            }else seg[r1].push_front(p[mid]);
            seg[mid]=move(seg[r1]);
        }else{
            ans[p[mid]-1]+=seg[r0].rightlower(a[p[mid]-1]+1);
            if(r1!=n){

                auto vr1=seg[r1].getvs();
                rep(i,vr1.size()){
                    a[p[mid]-1]-=vr1[i];
                    ans[p[mid]-1]+=seg[r0].rightlower(a[p[mid]-1]+1);
                }
                seg[r0].push_back(p[mid]);
                for(auto&x:vr1)seg[r0].push_back(x);
            }else seg[r0].push_back(p[mid]);
            seg[mid]=move(seg[r0]);
        }
        return mid;
    };
    dfs(dfs,0,n);
    rep(i,n)PRT(ans[i]);
}
signed main(){
    int t=1;
    // cin >> t;
    while(t--)solve();
}
0