結果

問題 No.2067 ±2^k operations
ユーザー 👑 p-adicp-adic
提出日時 2022-09-18 16:23:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 868 ms / 2,000 ms
コード長 2,861 bytes
コンパイル時間 2,168 ms
コンパイル使用メモリ 213,868 KB
実行使用メモリ 113,896 KB
最終ジャッジ日時 2023-08-23 18:10:26
合計ジャッジ時間 13,128 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 74 ms
15,536 KB
testcase_02 AC 71 ms
15,636 KB
testcase_03 AC 72 ms
15,568 KB
testcase_04 AC 72 ms
15,584 KB
testcase_05 AC 72 ms
15,604 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 858 ms
113,884 KB
testcase_08 AC 857 ms
113,816 KB
testcase_09 AC 868 ms
113,736 KB
testcase_10 AC 861 ms
113,868 KB
testcase_11 AC 854 ms
113,796 KB
testcase_12 AC 865 ms
113,780 KB
testcase_13 AC 865 ms
113,820 KB
testcase_14 AC 859 ms
113,792 KB
testcase_15 AC 855 ms
113,788 KB
testcase_16 AC 855 ms
113,896 KB
testcase_17 AC 12 ms
4,860 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 5 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using uint = unsigned int;
using ll = long long;

#define CIN( LL , A ) LL A; cin >> A 
#define GETLINE( A ) string A; getline( cin , A ) 
#define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR ) 
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) 
#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( ll VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) 
#define FOR_ITR( ARRAY , ITR , END ) for( auto ITR = ARRAY .begin() , END = ARRAY .end() ; ITR != END ; ITR ++ ) 
#define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES )
#define QUIT return 0 
#define RETURN( ANSWER ) cout << ( ANSWER ) << "\n"; QUIT 
#define DOUBLE( PRECISION , ANSWER ) cout << fixed << setprecision( PRECISION ) << ( ANSWER ) << "\n"; QUIT 
#define MIN( A , B ) A < B ? A : B;
#define MAX( A , B ) A < B ? B : A;

template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : - a; }

class seq
{

public:
  ll m_n;
  ll m_i;
  inline seq() : m_n() , m_i() {}
  inline seq( const ll& n , const ll& i ) : m_n( n ) , m_i( i ) {}
};

class ord
{
public:
  inline bool operator()( const seq& N0 , const seq& N1 ) { return N0.m_n < N1.m_n; }
};

inline const ll& f( const ll& n ) { static map<ll,ll> answer{}; return answer.count( n ) == 1 ? answer[n] : answer[n] = ( n == 0 ? 0 : n % 2 == 0 ? f( n / 2 ) : f( ( n + 1 ) / 4 ) + 1 ); }

// ( ( max - 1 ) / 4 - ( start + 2 ) / 4 + 1 ) + ( max + 1 ) / 4 - ( start / 4 + 1 + 1 )
// = ( ( max - 1 ) / 4 + ( max + 1 ) / 4 ) - ( ( start + 2 ) / 4 + start / 4 ) + 1
// = ( max + 1 ) / 2 - start / 2
inline const ll& sum_f( const ll& start , const ll& max ) { static map<ll,map<ll,ll> > answer{}; static const ll zero{ 0 }; if( start > max ){ return zero; } else if( start == max ){ return f( start ); } map<ll,ll>& answer_start = answer[start]; return answer_start.count( max ) ? answer_start[max] : answer_start[max] = sum_f( ( start + 1 ) / 2 , max / 2 ) + sum_f( ( start + 2 ) / 4 , start / 4 ) + sum_f( start / 4 + 1 , ( max - 1 ) / 4 ) * 2 + sum_f( ( max + 3 ) / 4 , ( max + 1 ) / 4 ) + ( max + 1 ) / 2 - start / 2; }

int main()
{
  UNTIE;
  CIN( ll , T );
  seq N[10000];
  FOR( i , 0 , T ){
    seq& Ni = N[i];
    cin >> Ni.m_n;
    Ni.m_i = i;
  }
  sort( N , N + T , ord() );
  ll answer[10000];
  ll* p_N_prev;
  ll* p_answer_prev;
  {
    seq& Ni = N[0];
    ll& answer_new = answer[ Ni.m_i ];
    answer_new = sum_f( 1 , Ni.m_n );
    p_N_prev = &( Ni.m_n );
    p_answer_prev = &( answer_new );
  }
  FOR( i , 1 , T ){
    seq& Ni = N[i];
    ll& answer_new = answer[ Ni.m_i ];
    answer_new = *p_answer_prev + sum_f( *p_N_prev + 1 , Ni.m_n );
    p_N_prev = &( Ni.m_n );
    p_answer_prev = &( answer_new );
  }
  FOR( i , 0 , T ){
    cout << answer[i] << "\n";
  }
  return 0;
}

0