結果

問題 No.1496 不思議な数え上げ
ユーザー penguinmanpenguinman
提出日時 2021-04-07 00:58:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 3,000 ms
コード長 1,980 bytes
コンパイル時間 2,594 ms
コンパイル使用メモリ 210,824 KB
実行使用メモリ 15,560 KB
最終ジャッジ日時 2023-09-23 03:24:14
合計ジャッジ時間 13,833 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 136 ms
15,460 KB
testcase_04 AC 137 ms
15,528 KB
testcase_05 AC 135 ms
15,452 KB
testcase_06 AC 182 ms
15,464 KB
testcase_07 AC 179 ms
15,460 KB
testcase_08 AC 178 ms
15,456 KB
testcase_09 AC 181 ms
15,416 KB
testcase_10 AC 181 ms
15,412 KB
testcase_11 AC 189 ms
15,488 KB
testcase_12 AC 122 ms
15,420 KB
testcase_13 AC 173 ms
15,496 KB
testcase_14 AC 179 ms
15,560 KB
testcase_15 AC 173 ms
15,500 KB
testcase_16 AC 173 ms
15,428 KB
testcase_17 AC 261 ms
15,528 KB
testcase_18 AC 270 ms
15,520 KB
testcase_19 AC 257 ms
15,456 KB
testcase_20 AC 248 ms
15,460 KB
testcase_21 AC 253 ms
15,424 KB
testcase_22 AC 256 ms
15,452 KB
testcase_23 AC 267 ms
15,520 KB
testcase_24 AC 252 ms
15,416 KB
testcase_25 AC 224 ms
15,432 KB
testcase_26 AC 222 ms
15,448 KB
testcase_27 AC 264 ms
15,460 KB
testcase_28 AC 271 ms
15,432 KB
testcase_29 AC 270 ms
15,476 KB
testcase_30 AC 267 ms
15,420 KB
testcase_31 AC 271 ms
15,492 KB
testcase_32 AC 220 ms
13,868 KB
testcase_33 AC 107 ms
9,192 KB
testcase_34 AC 90 ms
8,928 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 224 ms
14,108 KB
testcase_37 AC 110 ms
9,648 KB
testcase_38 AC 97 ms
9,116 KB
testcase_39 AC 147 ms
11,536 KB
testcase_40 AC 75 ms
7,796 KB
testcase_41 AC 158 ms
11,728 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++)
#define per(i,j,k) for(int i=int(j); int(k)<=i; i--)

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