結果

問題 No.1079 まお
ユーザー tko919tko919
提出日時 2020-05-12 16:28:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 928 ms / 2,000 ms
コード長 2,686 bytes
コンパイル時間 2,553 ms
コンパイル使用メモリ 195,316 KB
実行使用メモリ 26,640 KB
最終ジャッジ日時 2024-04-09 22:12:04
合計ジャッジ時間 18,058 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 4 ms
6,944 KB
testcase_07 AC 9 ms
6,944 KB
testcase_08 AC 10 ms
6,944 KB
testcase_09 AC 10 ms
6,940 KB
testcase_10 AC 10 ms
6,940 KB
testcase_11 AC 10 ms
6,940 KB
testcase_12 AC 419 ms
14,808 KB
testcase_13 AC 502 ms
16,844 KB
testcase_14 AC 612 ms
18,628 KB
testcase_15 AC 686 ms
20,560 KB
testcase_16 AC 806 ms
24,808 KB
testcase_17 AC 852 ms
21,016 KB
testcase_18 AC 853 ms
21,380 KB
testcase_19 AC 860 ms
20,868 KB
testcase_20 AC 868 ms
21,072 KB
testcase_21 AC 859 ms
21,204 KB
testcase_22 AC 907 ms
26,640 KB
testcase_23 AC 912 ms
26,640 KB
testcase_24 AC 915 ms
26,512 KB
testcase_25 AC 923 ms
26,512 KB
testcase_26 AC 928 ms
26,636 KB
testcase_27 AC 275 ms
9,828 KB
testcase_28 AC 580 ms
16,672 KB
testcase_29 AC 774 ms
23,952 KB
testcase_30 AC 769 ms
23,696 KB
testcase_31 AC 295 ms
8,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

typedef pair<int,ll> P;
typedef pair<int,P> T;
const int m=101010;
int n,k,a[m]; ll res=0;
void rec(int lb,int rb){
   if(rb-lb<=2){
      if(rb-lb==1 and a[lb]*2==k)res++;
      else if(rb-lb==2){
         if(a[lb]+a[lb+1]==k and a[lb]!=a[lb+1])res+=2;
         if(a[lb]*2==k)res++;
         if(a[lb+1]*2==k)res++;
      }
      return;
   }
   int mid=(lb+rb)/2; rec(lb,mid); rec(mid+1,rb);
   unordered_map<int,vector<T>> num; unordered_map<int,vector<ll>> mis,rui;
   unordered_map<int,vector<P>> is_one;
   int mi=inf,cnt=1;
   for(int i=mid;i<rb;i++){
      if(mi==a[i])cnt++;
      if(chmin(mi,a[i]))cnt=1;
      if(cnt==1 and a[i]+a[mid]==k)res+=i-mid+1;
      num[a[i]].push_back({mi,{i,cnt}});
   }
   for(auto p:num){
      int val=p.first; vector<T> v=p.second;
      sort(ALL(v));
      mis[val].push_back(-inf);
      rui[val].push_back(0); is_one[val].push_back({0,0});
      for(T t:v){
         mis[val].push_back(t.first);
         rui[val].push_back(t.second.first);
         if(t.second.second==1)is_one[val].push_back({1,t.second.first});
         else is_one[val].push_back({0,0});
      }
      mis[val].push_back(inf);
      rep(i,0,rui[val].size()-1){
         rui[val][i+1]+=rui[val][i];
         is_one[val][i+1].first+=is_one[val][i].first;
         is_one[val][i+1].second+=is_one[val][i].second;
      }
   }
   mi=inf; cnt=1;
   for(int i=mid-1;i>=lb;i--){
      if(mi==a[i])cnt++;
      if(chmin(mi,a[i]))cnt=1;
      if(mis[k-a[i]].empty())continue;
      int cen=upper_bound(ALL(mis[k-a[i]]),mi)-mis[k-a[i]].begin();
      if(cnt==1 and mis[k-a[i]][cen]!=inf){
         res+=(*rui[k-a[i]].rbegin()-rui[k-a[i]][cen-1])-1LL*(i-1)*(rui[k-a[i]].size()-cen);
      }
      cen=lower_bound(ALL(mis[k-a[i]]),mi)-mis[k-a[i]].begin()-1;
      if(cen>=1)res+=is_one[k-a[i]][cen].second-1LL*(i-1)*is_one[k-a[i]][cen].first;
      /*
      for(auto p:num[k-a[i]]){
         int opp=p.first,idx=p.second.first,add=p.second.second;
         if(opp<mi and add==1)res+=idx-i+1;
         else if(opp>mi and cnt==1)res+=idx-i+1;
      }*/
   }
}

int main(){
   //ifstream ifs("test/task_0.txt");
   cin>>n>>k; rep(i,0,n)cin>>a[i];
   //ifs>>n>>k; rep(i,0,n)ifs>>a[i];
   rec(0,n); cout<<res<<endl;
   return 0;
}
0