結果

問題 No.1079 まお
ユーザー LayCurseLayCurse
提出日時 2020-06-12 23:04:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 392 ms / 2,000 ms
コード長 5,558 bytes
コンパイル時間 3,012 ms
コンパイル使用メモリ 230,180 KB
実行使用メモリ 28,168 KB
最終ジャッジ日時 2023-09-06 11:11:19
合計ジャッジ時間 9,141 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,476 KB
testcase_01 AC 6 ms
11,536 KB
testcase_02 AC 5 ms
11,516 KB
testcase_03 AC 5 ms
11,536 KB
testcase_04 AC 5 ms
11,484 KB
testcase_05 AC 6 ms
11,584 KB
testcase_06 AC 6 ms
11,532 KB
testcase_07 AC 7 ms
11,732 KB
testcase_08 AC 7 ms
11,732 KB
testcase_09 AC 7 ms
11,696 KB
testcase_10 AC 8 ms
11,800 KB
testcase_11 AC 7 ms
11,860 KB
testcase_12 AC 129 ms
19,808 KB
testcase_13 AC 107 ms
21,144 KB
testcase_14 AC 214 ms
22,704 KB
testcase_15 AC 193 ms
24,284 KB
testcase_16 AC 176 ms
25,832 KB
testcase_17 AC 306 ms
24,188 KB
testcase_18 AC 184 ms
24,132 KB
testcase_19 AC 392 ms
24,156 KB
testcase_20 AC 335 ms
24,228 KB
testcase_21 AC 273 ms
24,240 KB
testcase_22 AC 274 ms
28,100 KB
testcase_23 AC 270 ms
28,100 KB
testcase_24 AC 316 ms
28,144 KB
testcase_25 AC 355 ms
28,168 KB
testcase_26 AC 375 ms
28,108 KB
testcase_27 AC 66 ms
17,588 KB
testcase_28 AC 99 ms
22,620 KB
testcase_29 AC 138 ms
27,680 KB
testcase_30 AC 142 ms
27,744 KB
testcase_31 AC 67 ms
17,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
  sort(a, a+N);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int N;
int K;
int A[100000];
int val[100000];
int p[100000];
int vs;
map<int,int> mp;
vector<int> vi[100000];
vector<long long> vsm[100000];
void calc(int a, int l, int r, int &cnt, long long &sm){
  int ind;
  int s;
  int t;
  cnt = 0;
  sm = 0;
  if(mp.count(a)==0){
    return;
  }
  ind = mp[a];
  s = lower_bound(vi[ind].begin(), vi[ind].end(), l) - vi[ind].begin();
  t = upper_bound(vi[ind].begin(), vi[ind].end(), r) - vi[ind].begin();
  cnt = t - s;
  sm = vsm[ind][t] - vsm[ind][s];
}
int main(){
  int m;
  wmem = memarr;
  int i;
  int j;
  int k;
  int v;
  int x;
  int y;
  int z;
  int c;
  long long res = 0;
  long long tmp;
  set<int> s;
  set<int>::iterator it1;
  set<int>::iterator it2;
  rd(N);
  rd(K);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(N);i++){
    val[i] = A[i];
    p[i] = i;
  }
  sortA_L(N, val, p);
  k = 0;
  for(i=(0);i<(N);i++){
    if(mp.count(A[i])==0){
      mp[A[i]] = k++;
    }
  }
  for(i=(0);i<(N);i++){
    vi[mp[A[i]]].push_back(i);
  }
  for(i=(0);i<(k);i++){
    tmp = 0;
    vsm[i].push_back(tmp);
    for(j=(0);j<(vi[i].size());j++){
      vsm[i].push_back( tmp += vi[i][j] );
    }
  }
  s.insert(-1);
  s.insert(N);
  for(m=(0);m<(N);m++){
    y = k = p[m];
    v = val[m];
    while(vs < N && val[vs] <= v){
      s.insert(p[vs]);
      vs++;
    }
    it1 = it2 = s.lower_bound(k);
    it1--;
    it2++;
    x = (*it1) + 1;
    z = (*it2) - 1;
    if(y-x > z-y){
      for(i=(y);i<(z+1);i++){
        calc(K - A[i], x, y, c, tmp);
        res += (long long)(i+1) * c - tmp;
      }
    }
    else{
      for(i=(x);i<(y+1);i++){
        calc(K - A[i], y, z, c, tmp);
        res += tmp - (long long)(i-1) * c;
      }
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N, K, A[1d5];
// int val[1d5], p[1d5], vs;
// map<int,int> mp;
// vector<int> vi[1d5];
// vector<ll> vsm[1d5];
// 
// void calc(int a, int l, int r, int &cnt, ll &sm){
//   int ind, s, t;
//   cnt = 0;
//   sm = 0;
//   if(mp.count(a)==0) return;
//   ind = mp[a];
// 
//   s = lower_bound(vi[ind].begin(), vi[ind].end(), l) - vi[ind].begin();
//   t = upper_bound(vi[ind].begin(), vi[ind].end(), r) - vi[ind].begin();
//   cnt = t - s;
//   sm = vsm[ind][t] - vsm[ind][s];
// }
// 
// {
//   int i, j, k, v, x, y, z, c;
//   ll res = 0, tmp;
//   set<int> s;
//   set<int>::iterator it1, it2;
//   rd(N,K,A(N));
// 
//   rep(i,N) val[i] = A[i], p[i] = i;
//   sortA(N, val, p);
// 
//   k = 0;
//   rep(i,N) if(mp.count(A[i])==0) mp[A[i]] = k++;
//   rep(i,N) vi[mp[A[i]]].push_back(i);
//   rep(i,k){
//     tmp = 0;
//     vsm[i].push_back(tmp);
//     rep(j,vi[i].size()) vsm[i].push_back( tmp += vi[i][j] );
//   }
// 
//   s.insert(-1);
//   s.insert(N);
//   rep(m,N){
//     y = k = p[m];
//     v = val[m];
//     while(vs < N && val[vs] <= v){
//       s.insert(p[vs]);
//       vs++;
//     }
//     it1 = it2 = s.lower_bound(k);
//     it1--;
//     it2++;
//     x = (*it1) + 1;
//     z = (*it2) - 1;
// 
// //    wt(v,":",x,y,z);
//     if(y-x > z-y){
//       rep(i,y,z+1){
//         calc(K - A[i], x, y, c, tmp);
//         res += (ll)(i+1) * c - tmp;
//       }
//     } else {
//       rep(i,x,y+1){
//         calc(K - A[i], y, z, c, tmp);
//         res += tmp - (ll)(i-1) * c;
//       }
//     }
//   }
// 
//   wt(res);
// }
0