結果

問題 No.1496 不思議な数え上げ
ユーザー ぷらぷら
提出日時 2022-12-27 05:16:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 3,000 ms
コード長 2,777 bytes
コンパイル時間 2,528 ms
コンパイル使用メモリ 209,664 KB
実行使用メモリ 42,328 KB
最終ジャッジ日時 2024-05-01 11:32:46
合計ジャッジ時間 11,639 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 112 ms
11,132 KB
testcase_04 AC 113 ms
11,100 KB
testcase_05 AC 115 ms
11,096 KB
testcase_06 AC 122 ms
42,204 KB
testcase_07 AC 123 ms
42,328 KB
testcase_08 AC 121 ms
42,200 KB
testcase_09 AC 97 ms
11,020 KB
testcase_10 AC 99 ms
11,024 KB
testcase_11 AC 98 ms
11,148 KB
testcase_12 AC 119 ms
11,096 KB
testcase_13 AC 97 ms
20,060 KB
testcase_14 AC 101 ms
27,096 KB
testcase_15 AC 95 ms
11,152 KB
testcase_16 AC 93 ms
11,184 KB
testcase_17 AC 115 ms
11,228 KB
testcase_18 AC 115 ms
11,100 KB
testcase_19 AC 115 ms
11,224 KB
testcase_20 AC 112 ms
11,100 KB
testcase_21 AC 114 ms
10,968 KB
testcase_22 AC 116 ms
11,100 KB
testcase_23 AC 117 ms
11,096 KB
testcase_24 AC 115 ms
11,100 KB
testcase_25 AC 113 ms
11,096 KB
testcase_26 AC 113 ms
11,096 KB
testcase_27 AC 88 ms
11,096 KB
testcase_28 AC 89 ms
11,092 KB
testcase_29 AC 88 ms
11,100 KB
testcase_30 AC 87 ms
11,220 KB
testcase_31 AC 88 ms
11,096 KB
testcase_32 AC 103 ms
9,924 KB
testcase_33 AC 57 ms
7,164 KB
testcase_34 AC 50 ms
6,936 KB
testcase_35 AC 3 ms
5,376 KB
testcase_36 AC 104 ms
10,180 KB
testcase_37 AC 58 ms
7,364 KB
testcase_38 AC 53 ms
7,256 KB
testcase_39 AC 77 ms
8,508 KB
testcase_40 AC 43 ms
6,376 KB
testcase_41 AC 77 ms
8,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<int> cartesiantree(vector<int> A) {
    int N = A.size();
    vector<int>p(N,-1);
    stack<int>st;
    for(int i = 0; i < N; i++) {
        int pr = -1;
        while(!st.empty()) {
            int x = st.top();
            if(A[i] < A[x]) {
                st.pop();
                if(pr != -1) {
                    p[pr] = x;
                }
                pr = x;
            }
            else {
                break;
            }
        }
        if(pr != -1) {
            p[pr] = i;
        }
        st.push(i);
    }
    while(st.size() >= 2) {
        int x = st.top();
        st.pop();
        p[x] = st.top();
    }
    return p;  
}

void calc(int L,int R,int now,vector<int>&l,vector<int>&r,vector<long long>&Q,vector<long long>&A,vector<long long>&ans) {
    long long P = Q[now+1]-Q[now]-1;
    if(now-L <= R-now) {
        for(int i = L; i <= now; i++) {
            if(Q[now+1]-Q[i] <= A[P]) {
                int le = now,ri = R+1;
                while(le+1 < ri) {
                    int mid = (le+ri)/2;
                    if(Q[mid+1]-Q[i] <= A[P]) {
                        le = mid;
                    }
                    else {
                        ri = mid;
                    }
                }
                ans[P] += le-now+1;
            }
        }
    }
    else {
        for(int i = now; i <= R; i++) {
            if(Q[i+1]-Q[now] <= A[P]) {
                int le = L-1,ri = now;
                while(le+1 < ri) {
                    int mid = (le+ri)/2;
                    if(Q[i+1]-Q[mid] <= A[P]) {
                        ri = mid;
                    }
                    else {
                        le = mid;
                    }
                }
                ans[P] += now-ri+1;
            }
        }
    }
    if(l[now] != -1) {
        calc(L,now-1,l[now],l,r,Q,A,ans);
    }
    if(r[now] != -1) {
        calc(now+1,R,r[now],l,r,Q,A,ans);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<int>P(N);
    vector<long long>Q(N+1);
    for(int i = 0; i < N; i++) {
        cin >> P[i];
        Q[i+1] = Q[i]+P[i];
    }
    vector<long long>A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    vector<long long>ans(N);
    vector<int>c = cartesiantree(P);
    vector<int>l(N,-1),r(N,-1);
    int root = 0;
    for(int i = 0; i < N; i++) {
        if(c[i] != -1) {
            if(c[i] < i) {
                r[c[i]] = i;
            }
            else {
                l[c[i]] = i;
            }
        }
        else {
            root = i;
        }
    }
    calc(0,N-1,root,l,r,Q,A,ans);
    for(int i = 0; i < N; i++) {
        cout << ans[i] << "\n";
    }
}
0