結果

問題 No.1435 Mmm......
ユーザー LayCurseLayCurse
提出日時 2021-03-19 23:28:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 4,558 bytes
コンパイル時間 3,076 ms
コンパイル使用メモリ 215,708 KB
実行使用メモリ 14,476 KB
最終ジャッジ日時 2023-08-12 04:48:08
合計ジャッジ時間 5,622 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 32 ms
4,380 KB
testcase_07 AC 39 ms
4,612 KB
testcase_08 AC 45 ms
4,984 KB
testcase_09 AC 42 ms
4,660 KB
testcase_10 AC 37 ms
4,468 KB
testcase_11 AC 36 ms
4,676 KB
testcase_12 AC 34 ms
4,528 KB
testcase_13 AC 33 ms
4,412 KB
testcase_14 AC 25 ms
4,380 KB
testcase_15 AC 46 ms
4,812 KB
testcase_16 AC 39 ms
4,616 KB
testcase_17 AC 28 ms
4,380 KB
testcase_18 AC 48 ms
4,932 KB
testcase_19 AC 35 ms
4,524 KB
testcase_20 AC 44 ms
4,920 KB
testcase_21 AC 125 ms
14,476 KB
testcase_22 AC 105 ms
11,096 KB
testcase_23 AC 51 ms
5,164 KB
testcase_24 AC 50 ms
5,140 KB
testcase_25 AC 50 ms
5,148 KB
testcase_26 AC 50 ms
5,124 KB
testcase_27 AC 49 ms
5,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
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');
  }
}
template<class T> struct twoMultisets{
  multiset<T> a;
  multiset<T> b;
  T sa;
  T sb;
  twoMultisets(){
    clear();
  }
  void clear(){
    a.clear();
    b.clear();
    sa = sb = 0;
  }
  void insert(T x){
    if(b.size() == 0 || x < *b.begin()){
      a.insert(x);
      sa += x;
    }
    else{
      b.insert(x);
      sb += x;
    }
  }
  int erase(T x){
    typename multiset<T>::iterator it;
    it = a.find(x);
    if(it != a.end()){
      a.erase(it);
      sa -= x;
      return 1;
    }
    it = b.find(x);
    if(it != b.end()){
      b.erase(it);
      sb -= x;
      return 1;
    }
    return 0;
  }
  int size(void){
    return a.size() + b.size();
  }
  T allsum(void){
    return sa + sb;
  }
  void assign(int K){
    T x;
    typename multiset<T>::iterator it;
    while(a.size() < K){
      x = *b.begin();
      b.erase(b.begin());
      a.insert(x);
      sa += x;
      sb -= x;
    }
    while(a.size() > K){
      it = a.end();
      it--;
      x = *it;
      a.erase(it);
      b.insert(x);
      sa -= x;
      sb += x;
    }
  }
  T Kth(int K){
    assign(K);
    return *b.begin();
  }
  T Ksum(int K){
    assign(K);
    return sa;
  }
  T rKth(int K){
    return Kth(a.size() + b.size() - K - 1);
  }
  T rKsum(int K){
    assign(a.size() + b.size() - K);
    return sb;
  }
  T getMin(void){
    if(a.size()){
      return *a.begin();
    }
    if(b.size()){
      return *b.begin();
    }
    return 0;
  }
  T getMin(T x){
    if(a.size()){
      return *a.begin();
    }
    if(b.size()){
      return *b.begin();
    }
    return x;
  }
  T getMax(void){
    if(b.size()){
      return *b.rbegin();
    }
    if(a.size()){
      return *a.rbegin();
    }
    return 0;
  }
  T getMax(T x){
    if(b.size()){
      return *b.rbegin();
    }
    if(a.size()){
      return *a.rbegin();
    }
    return x;
  }
}
;
int N;
int A[200000];
twoMultisets<int> s;
int main(){
  int i;
  int j = 0;
  int x;
  int y;
  int z;
  long long res = 0;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(N);i++){
    for(;;){
      if(s.size() < 3){
        x = y = z = 0;
      }
      else{
        x = s.Kth(0);
        y = s.Kth(1);
        z = s.getMax();
      }
      if(z > x + y){
        break;
      }
      if(z <= x + y){
        if(j >= N){
          j = N+1;
          break;
        }
        s.insert(A[j++]);
        continue;
      }
    }
    res += j - 2 - i;
    s.erase(A[i]);
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay version 20210227-1

// --- original code ---
// int N, A[2d5];
// twoMultisets<int> s;
// {
//   int i, j = 0, x, y, z;
//   ll res = 0;
//   rd(N,A(N));
//   rep(i,N){
//     for(;;){
//       if(s.size() < 3){
//         x = y = z = 0;
//       } else {
//         x = s.Kth(0);
//         y = s.Kth(1);
//         z = s.getMax();
//       }
//       if(z > x + y) break;
//       if(z <= x + y){
//         if(j >= N) j = N+1, break;
//         s.insert(A[j++]);
//         continue;
//       }
//     }
//     res += j - 2 - i;
//     s.erase(A[i]);
//   }
//   wt(res);
// }
0