結果

問題 No.1079 まお
ユーザー ChanyuhChanyuh
提出日時 2020-07-22 00:10:00
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,954 bytes
コンパイル時間 2,416 ms
コンパイル使用メモリ 129,748 KB
実行使用メモリ 40,536 KB
最終ジャッジ日時 2023-08-30 06:32:15
合計ジャッジ時間 6,950 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 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 103 ms
18,100 KB
testcase_13 AC 42 ms
10,628 KB
testcase_14 AC 193 ms
27,392 KB
testcase_15 AC 108 ms
19,904 KB
testcase_16 AC 72 ms
15,460 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 198 ms
28,284 KB
testcase_23 AC 184 ms
27,076 KB
testcase_24 AC 252 ms
32,572 KB
testcase_25 AC 332 ms
38,484 KB
testcase_26 AC 353 ms
40,536 KB
testcase_27 AC 164 ms
7,468 KB
testcase_28 AC 235 ms
23,980 KB
testcase_29 AC 266 ms
32,944 KB
testcase_30 AC 255 ms
32,884 KB
testcase_31 AC 164 ms
7,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;//二項演算
  T ti;//単位元
  vector<T> dat;

  SegmentTree(){}
  SegmentTree(F f,T ti):f(f),ti(ti){}

  void init(int n_){//sizeがn_のsegtreeを作る
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }

  void build(const vector<T> &v){//vによってsegtreeをbuildする
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }

  void set_val(int k,T x){//k番目をxにする
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }

  T query(int a,int b){//区間[a,b)に対しFを適応した値を返す
    if(a>=b) return ti;
    T vl=ti,vr=ti;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }

  template<typename C>
  int find(int st,C &check,T &acc,int k,int l,int r){//
    if(l+1==r){
      acc=f(acc,dat[k]);
      return check(acc)?k-n:-1;
    }
    int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l&&!check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }

  template<typename C>
  int find(int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }

  T &operator [] (int i) {return dat[i+n];};
};

int n,k;
int A[100010];
set<int> se;
map<int,vector<int>> ma;
map<int,vector<ll>> ma2;

ll f(int t,int id,SegmentTree<P> &seg){
  if(ma[t].empty()) return 0;
  //cout << id << endl;
  int sta=lower_bound(ma[t].begin(),ma[t].end(),id)-ma[t].begin(),r=ma[t].size();
  int l=sta;
  //cout << sta << endl;
  if(r<=l) return 0;
  while(r-l>1){
    //cout << l << " " << r << endl;
    int mid=(l+r)/2;
    if(seg.query(id,ma[t][mid]+1).second<=1) l=mid;
    else r=mid;
  }
  //cout << l << endl;
  //cout << l << " " <<  ma2[t][l+1]-ma2[t][sta]-(id-1)*(l-sta+1) << endl;
  if(seg.query(id,ma[t][l]+1).second>1) return 0; 
  return ma2[t][l+1]-ma2[t][sta]-(id-1)*(l-sta+1);
}


void solve(){
  cin >> n >> k;
  rep(i,n){
    cin >> A[i];
    if(k>=A[i]){
      ma[k-A[i]].push_back(i);
      se.insert(k-A[i]);
    }
  }
  for(int s:se){
    //cout << s << endl;
    ma2[s].push_back(0);
    for(int t:ma[s]){
      //cout << t << " ";
      ma2[s].push_back(ma2[s].back()+t);
    }
    //cout << "" << endl;
  }
  auto g=[](P a,P b){
    if(a.first<b.first) return a;
    if(a.first==b.first) return P(a.first,a.second+b.second);
    if(a.first>b.first) return b;
    return P(mod,0);
  };
  SegmentTree<P> seg(g,P(mod,0));
  vector<P> v(n);
  rep(i,n) v[i]=P(A[i],1);
  seg.build(v);
  ll ans=0;
  rep(i,n){
    ans+=f(A[i],i,seg);
  }
  cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0