結果
| 問題 |
No.1079 まお
|
| コンテスト | |
| ユーザー |
IKyopro
|
| 提出日時 | 2020-06-12 22:50:10 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,185 bytes |
| コンパイル時間 | 3,488 ms |
| コンパイル使用メモリ | 218,220 KB |
| 最終ジャッジ日時 | 2025-01-11 02:49:24 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 16 WA * 14 |
ソースコード
#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 = 2e9;
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;
}
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]];
}
}
cout << ans << "\n";
}
IKyopro