結果

問題 No.1079 まお
ユーザー tko919tko919
提出日時 2020-05-12 16:16:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,592 bytes
コンパイル時間 2,338 ms
コンパイル使用メモリ 194,356 KB
実行使用メモリ 26,644 KB
最終ジャッジ日時 2024-04-09 22:11:42
合計ジャッジ時間 18,321 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,972 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 15 ms
6,940 KB
testcase_08 AC 14 ms
6,944 KB
testcase_09 AC 14 ms
6,940 KB
testcase_10 AC 13 ms
6,944 KB
testcase_11 AC 13 ms
6,944 KB
testcase_12 AC 397 ms
14,676 KB
testcase_13 AC 496 ms
16,716 KB
testcase_14 AC 592 ms
18,764 KB
testcase_15 AC 691 ms
20,664 KB
testcase_16 AC 795 ms
24,696 KB
testcase_17 AC 843 ms
21,016 KB
testcase_18 AC 863 ms
21,380 KB
testcase_19 AC 843 ms
20,732 KB
testcase_20 AC 837 ms
21,240 KB
testcase_21 AC 852 ms
21,204 KB
testcase_22 AC 919 ms
26,644 KB
testcase_23 AC 911 ms
26,512 KB
testcase_24 AC 917 ms
26,508 KB
testcase_25 AC 911 ms
26,512 KB
testcase_26 AC 914 ms
26,508 KB
testcase_27 TLE -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

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--; while(mis[k-a[i]][cen]==mi)cen--;
      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(){
   cin>>n>>k; rep(i,0,n)cin>>a[i];
   rec(0,n); cout<<res<<endl;
   return 0;
}
0