結果
| 問題 | 
                            No.1079 まお
                             | 
                    
| コンテスト | |
| ユーザー | 
                             IKyopro
                         | 
                    
| 提出日時 | 2020-06-12 22:29:57 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,296 bytes | 
| コンパイル時間 | 3,945 ms | 
| コンパイル使用メモリ | 210,776 KB | 
| 最終ジャッジ日時 | 2025-01-11 02:43:42 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 17 WA * 13 | 
ソースコード
#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;
    while(l<N){
        while(r<N){
            if(swag.empty() || swag.result()!=A[r]){
                swag.push(A[r]);
                lsum[A[r]] += r-1;
                cnt[A[r]]++;
                if(cnt.count(K-A[r])) ans += cnt[K-A[r]]*r-lsum[K-A[r]];
                r++;
            }else break;
        }   
        while(!swag.empty()){
            bool remove = A[l]==swag.result();
            swag.pop();
            lsum[A[l]] -= l-1;
            cnt[A[l]]--;
            l++;
            if(remove) break;
        }
    }
    cout << ans << "\n";
}
            
            
            
        
            
IKyopro