結果

問題 No.1079 まお
ユーザー IKyoproIKyopro
提出日時 2020-06-12 22:48:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,983 bytes
コンパイル時間 3,832 ms
コンパイル使用メモリ 223,348 KB
実行使用メモリ 16,744 KB
最終ジャッジ日時 2023-09-06 10:56:42
合計ジャッジ時間 8,464 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 79 ms
10,016 KB
testcase_13 AC 98 ms
11,184 KB
testcase_14 AC 126 ms
12,564 KB
testcase_15 AC 136 ms
13,788 KB
testcase_16 AC 144 ms
15,012 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 199 ms
16,700 KB
testcase_23 AC 185 ms
16,648 KB
testcase_24 AC 199 ms
16,648 KB
testcase_25 AC 194 ms
16,648 KB
testcase_26 AC 199 ms
16,692 KB
testcase_27 AC 18 ms
4,384 KB
testcase_28 AC 45 ms
10,404 KB
testcase_29 AC 64 ms
16,744 KB
testcase_30 AC 63 ms
16,708 KB
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

template<class T> class SWAG{
private:
    struct node{
        T val,sum;
        node(const T& val, const T& sum):val(val),sum(sum){}
    };

    using F = function<T(T,T)>;
    stack<node> front_stack,back_stack;
    F op;
public:
    SWAG(const F op):op(op),front_stack(),back_stack(){};

    void push(const T& x){
        if(back_stack.empty()) back_stack.emplace(x,x);
        else{
            T s{op(back_stack.top().sum,x)};
            back_stack.emplace(x,s);
        }
    }

    void pop(){
        if(front_stack.empty()){
            front_stack.emplace(back_stack.top().val,back_stack.top().val);
            back_stack.pop();
            while(!back_stack.empty()){
                T s{op(back_stack.top().val,front_stack.top().sum)};
                front_stack.emplace(back_stack.top().val,s);
                back_stack.pop();
            }
        }
        front_stack.pop();
    }

    T result(){
        if(front_stack.empty()) return back_stack.top().sum;
        else if(back_stack.empty()) return front_stack.top().sum;
        else return op(front_stack.top().sum,back_stack.top().sum);
    }

    bool empty(){
        return front_stack.empty() && back_stack.empty();
    }
};

using P = pair<int,int>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,K;
    cin >> N >> K;
    vec<int> A(N);
    for(auto& x:A) cin >> x;
    auto op = [](int a,int b){return min(a,b);};
    SWAG<int> swag(op);
    map<int,ll> lsum,cnt;
    ll ans = 0;
    int r = 0;
    int l = 0;
    queue<int> Q;
    int inf = 1e9;
    while(l<N){
        while(r<N){
            if(swag.empty() || swag.result()!=A[r]){
                int pre = (swag.empty()? (int) 1e9:swag.result());
                swag.push(A[r]);
                Q.push(r);
                if(pre>swag.result()){
                    while(!Q.empty()){
                        int x = Q.front(); Q.pop();
                        lsum[A[x]] += x-1;
                        cnt[A[x]]++;
                        if(cnt.count(K-A[x])) ans += cnt[K-A[x]]*x-lsum[K-A[x]];
                    }
                }
                r++;
            }else break;
        }
        if(r==N){
            while(!Q.empty()){
                int x = Q.front(); Q.pop();
                lsum[A[x]] += x-1;
                cnt[A[x]]++;
                if(cnt.count(K-A[x])) ans += cnt[K-A[x]]*x-lsum[K-A[x]];
            }
        }
        while(!swag.empty()){
            bool remove = A[l]==swag.result();
            swag.pop();
            lsum[A[l]] -= l-1;
            cnt[A[l]]--;
            if(cnt[A[l]]==0){
                lsum.erase(A[l]);
                cnt.erase(A[l]);
            }
            l++;
            if(remove) break;
        }
    }
    cout << ans << "\n";
}
0