結果

問題 No.1496 不思議な数え上げ
ユーザー penguinmanpenguinman
提出日時 2021-04-07 01:01:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 330 ms / 3,000 ms
コード長 1,668 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 209,044 KB
実行使用メモリ 17,296 KB
最終ジャッジ日時 2023-09-23 03:24:31
合計ジャッジ時間 14,396 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 206 ms
17,060 KB
testcase_04 AC 210 ms
17,076 KB
testcase_05 AC 207 ms
17,216 KB
testcase_06 AC 185 ms
17,064 KB
testcase_07 AC 184 ms
16,980 KB
testcase_08 AC 185 ms
16,980 KB
testcase_09 AC 187 ms
16,960 KB
testcase_10 AC 186 ms
16,960 KB
testcase_11 AC 188 ms
17,040 KB
testcase_12 AC 188 ms
16,996 KB
testcase_13 AC 186 ms
17,020 KB
testcase_14 AC 179 ms
16,980 KB
testcase_15 AC 187 ms
16,992 KB
testcase_16 AC 190 ms
17,048 KB
testcase_17 AC 290 ms
16,960 KB
testcase_18 AC 287 ms
16,980 KB
testcase_19 AC 287 ms
17,008 KB
testcase_20 AC 283 ms
17,020 KB
testcase_21 AC 296 ms
17,296 KB
testcase_22 AC 296 ms
16,996 KB
testcase_23 AC 295 ms
16,960 KB
testcase_24 AC 297 ms
16,992 KB
testcase_25 AC 247 ms
17,116 KB
testcase_26 AC 258 ms
17,036 KB
testcase_27 AC 330 ms
17,056 KB
testcase_28 AC 326 ms
17,012 KB
testcase_29 AC 326 ms
17,004 KB
testcase_30 AC 330 ms
16,976 KB
testcase_31 AC 324 ms
17,084 KB
testcase_32 AC 247 ms
15,504 KB
testcase_33 AC 118 ms
9,888 KB
testcase_34 AC 109 ms
9,412 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 254 ms
15,644 KB
testcase_37 AC 127 ms
10,392 KB
testcase_38 AC 116 ms
9,868 KB
testcase_39 AC 170 ms
12,512 KB
testcase_40 AC 91 ms
8,532 KB
testcase_41 AC 177 ms
12,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using ll = long long;
#define rep(i,j,k) for(int i=int(j); i<int(k); i++)
#define REP(i,j,k) for(int i=int(j); i<=int(k); i++)

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    ll N; cin >> N;
    vector<ll> P(N);
    rep(i,0,N) cin >> P[i];
    vector<ll> sum(N+1), rev(N);
    rep(i,0,N){
        sum[i+1] = sum[i]+P[i];
        rev[--P[i]] = i;
    }
    std::set<ll> set;
    set.insert(-1);
    set.insert(N);
    rep(i,0,N){
        ll A; cin >> A;
        auto itr = set.lower_bound(rev[i]);
        ll right = *itr;
        itr--;
        ll left = *itr;
        ll ans = 0;
        if(right-rev[i] < rev[i]-left){
            rep(r,rev[i],right){
                //右端固定
                ll l = std::lower_bound(sum.begin(),sum.end(),sum[r+1]-A)-sum.begin();
                //left < l ≤ rev[i] を満たす必要がある
                l = std::max(l, left+1);
                l = std::min(l, rev[i]+1);
                ans += rev[i]-l+1;
            }
        }
        else{
            REP(l,left+1,rev[i]){
                //左端固定
                ll r = std::upper_bound(sum.begin(),sum.end(),sum[l]+A)-sum.begin()-2;
                //rev[i] ≤ r < right を満たす必要がある
                r = std::min(r, right-1);
                r = std::max(r, rev[i]-1);
                ans += r-rev[i]+1;
            }
        }
        cout << ans << "\n";
        set.insert(rev[i]);
    }
}
0